1

The output of:

find mydir -name foo -exec echo "---$(basename {})---" \;

should be ---foo---, but instead it is ---mydir/foo---

The command basename alone:

basename mydir/foo 
echo "---$(basename mydir/foo)---"

brings respective foo and ---foo---

Replacing basename with other command such as uname the construct $(...) works correct.

SzB
  • 1,027
  • 10
  • 12

2 Answers2

4

Because $(basename {}) gets expanded to {} by bash before running find command, your find command is exactly equivalent to :

find mydir -name foo -exec echo "---{}---" \;

That's why you see ---mydir/foo---

To get the effect you wanted, you can do :

find mydir -name foo -exec bash -c 'echo "---$(basename {})---"' \;
Philippe
  • 20,025
  • 2
  • 23
  • 32
0

Eventually I found a solution by creating a script that calls itself from the find command:

#!/bin/bash
if [ $# -eq 0 ]; then
    find myDir -name foo -exec $(readlink -f $0) "{}" \;
else
    echo "---$(basename $1)---"
fi

It's not as elegant as one line solution (hat tip to Phillipe) but if necessary more expandable

SzB
  • 1,027
  • 10
  • 12