1

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?

4

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.