inconsistent results for basename?

Solution 1:

Took some digging but the answer is available in Unix & Linux:

  • Process substitution (the $(...) part) occurs before find gets even executed
  • So before find even starts, $(basename "{}" gets executed returning {}, turning your command into find . -type f -exec echo "{}" \;
  • Now find starts and just echos each file found.

The output of dirname just looked right because dirname {} (or actually dirname ANY-FILE) always returns . (see the part after Either the dir is "/" or there are no slashes in dirname.c).

As an alternative you can use

find . -type f -exec sh -c 'echo "$(basename $1) ++ $(dirname $1)"' _ {} \;