Ask Ubuntu is a question and answer site for Ubuntu users and developers. 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

I want a command that allows to select specific files based on their extensions to move them to another directory.

share|improve this question
In any algorithm that you could use, you need to have in mind the scope of the path of the search. Because in a Linux system you could have a file called system.a.backup. Is just a mention.. – Lucio Mar 16 '13 at 0:58
You'll need to give us more details. What extension, whether you're trying to recursively move items, etc. are all relevant information here. – Thomas Ward Mar 16 '13 at 1:18
I'm trying to move .pdf files only from a directory to another – microarm15 Mar 16 '13 at 1:25
up vote 3 down vote accepted

You can do this easily from one directory with:

mv /path/to/original/directory/*.pdf /path/to/new/directory

The command for a recursive move can be a little more complex. This should do the trick though:

find /original/directory/ | grep '\.pdf' | xargs -I {} mv {} /path/to/new/directory/

With the second command /original/directory will be search recursively for .pdf files.

share|improve this answer
Are you sure that the second command works? Because I can't make it work. – Lucio Mar 16 '13 at 2:32
@Lucio extra -0 on xargs option was definitely the problem. I removed it so it should work now. Thanks for pointing that out. – dward Mar 16 '13 at 2:50
I guess this will also work: find /original/directory/ -iname *.pdf -exec mv {} /path/to/new/directory/ \; – S Prasanth Mar 17 '13 at 8:09

why not cp /path/to/dir/where/pdf/are/*.pdf /path/to/where/ever

share|improve this answer


for file in { ls *. pdf} 
mv "$file" /destination /here 

So here before doing this you must change into your directory where you have files.

share|improve this answer
Doing from mobile. Could someone put it in the format plz – Raja Mar 16 '13 at 1:48
@Lucio Thank you very much My Friend. – Raja Mar 16 '13 at 2:48
Plus the actual script, you can add another one with -R as a parameter in the ls command, adding the recursive feature. Anyway, your script is the cleanest and more easy to understand. – Lucio Mar 16 '13 at 2:56

Another method using recursive globs (available in zsh and bash): mv /path/to/original/directory/**/*.pdf /path/to/new/directory/

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.