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 want a command that allows to select specific files based on their extensions to move them to another directory.

share|improve this question
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 W. 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
add comment

4 Answers 4

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
add comment

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

share|improve this answer
add comment

Command:

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

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 –  AgentCool Mar 16 '13 at 1:48
    
@Lucio Thank you very much My Friend. –  AgentCool 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
add comment

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

share|improve this answer
add comment

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.