Sign up ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

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?

share|improve this question
actually this was on stackoverflow..… sorry for the redundant post! – lucacerone Feb 6 '14 at 13:14

1 Answer 1

up vote 2 down vote accepted

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.

share|improve this answer
thanks! the best explanation I found (I didn't know the difference between single and double quotes, and showing it in debug mode really made the difference for me):… – lucacerone Feb 6 '14 at 13:54

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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