2

I am trying to list all the file names (the basename, not the full path) below my home directory using find. I tried to do:

find ~/ -type f -exec echo $(basename "{}") ";"

However I end up having a list of full paths, and not only of the basenames as I would like. I now use echo, but ideally the result of $(basename "{}") should be the argument of some other function I want to write.

Why is the basename function not applied correctly?

1

1 Answer 1

5

The shell is interpreting the $(...) before it runs find. If you want the find shell to execute it instead, you'll need to stop bash substituting it. We can do that like so:

find ~/ -type f -exec sh -c 'echo $(basename "{}")' \;

The key here are the single quotes; Bash won't touch anything inside those.

1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .