Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How to get the file name while using find command? When I use find command to search particular type of files from a directory and subdirectories, I get the whole path of that file, how do I get the file name only? The problem I am facing is when I am moving that found file to archive directory, it says "there is no such directory" and it is because of whole path I am getting while searching for the file.

share|improve this question
I think there's something wrong with your logic. Can you give a use case where this is used, before you select an answer? – Andrew M. Sep 21 '10 at 17:55
well I am looking for a way through go through a directory and find a file with particular name , copy that file to remove server and then move that file to archive directory with time stamp. I am using 'find' for looping over files in that particular directory. – yogsma Sep 21 '10 at 18:09
up vote 5 down vote accepted

The basename command will give you just the name of the file and not the full path, which you can execute on your found file like this:

find . -name filename -exec basename {} \;
share|improve this answer

Use find ... -printf '%f\n'

Or, to solve your stated problem:

find ... -name FOO -print0 | xargs --no-run-if-empty --null mv --target-directory=TARGET

share|improve this answer

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.