Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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.. stackoverflow.com/questions/15627446/… sorry for the redundant post! –  lucacerone Feb 6 at 13:14

1 Answer 1

up vote 1 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): stackoverflow.com/questions/2428628/… –  lucacerone Feb 6 at 13:54

Your Answer

 
discard

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.