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

  • 1
    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

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.

  • 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/ \; – Prasanth S Mar 17 '13 at 8:09

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


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



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

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

  • Doing from mobile. Could someone put it in the format plz – rɑːdʒɑ Mar 16 '13 at 1: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

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.