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.

How can I find just the *.mp3 files with a recursive command like ls -R *.mp3 in a directory that contains several sub-directories and at the end copy these files in a directory that I choose.

Thanks for your support.

share|improve this question
add comment

2 Answers

The command is:

find /path/to/directory -name "*.mp3" -exec cp {} /some/other/dir/ \;

Alternative:

find /path/to/dir/ -name '*.mp3' | xargs cp -t /target/

Example:

alex@MaD-pc:~/test$ ls
1  2  3
alex@MaD-pc:~/test$ ls 1 2 3
1:
1.txt  2.mp3  3.txt

2:
4.txt  5.mp3  6.txt

3:
alex@MaD-pc:~/test$ find . -name "*.mp3" -exec cp {} 3/ \;
alex@MaD-pc:~/test$ ls 3
2.mp3  5.mp3

For more information:

man find
share|improve this answer
    
Judges would also accept: find /path/to/dir/ -name '*.c' | xargs cp -t /target/ –  Oli Aug 25 '12 at 8:51
    
I am one of the judges and I deny this solution 8-) Thanks for the comment, I will add it :) –  hakermania Aug 25 '12 at 8:53
    
xargs is normally a lot quicker. I've just run a quick test here finding a load of c files and and run ls -l on them. It's 0m2.420s for xargs and 0m25.494s for -exec... Clearly in this case the cp operation is going to take most of the time so it probably doesn't matter but it's worth keeping note of. –  Oli Aug 25 '12 at 8:59
add comment

There is also another way to do it which I think will suit your purpose perfectly. You can combine find with a while loop and not even need to use either of exec or xargs at all. If, for example you wanted to copy your mp3s from your download folder to your music folder, you would use the following script, which I have used many times.

You can modify it how you want by changing the directories that find searches and places the resulting files in; if no directory is stated, find will search the entire home folder. You can also change cp to mv or other commands. It is pretty fast, as I have just tested it with 3945 .jpg files! Copy it into a text editor, save it and then make it executable by running chmod +x myscript.

#!/bin/bash
# a script to recursively find and copy files to a desired location
find ~/Downloads -type f -iname '*.mp3' -print0 |
while IFS= read -r -d '' f; 
do cp -- "$f" ~/Music ;
done

At this noted Bash wiki it is shown how useful it is to combine the while loop and read commands to process the output of the find command; and the way I have done it makes sure that the script will not break if it comes across file names with spaces or other unexpected or special characters.

For more general information on the find command, enter in the terminal man find or see the Ubuntu manpages online. For a great introduction to the use of find, see this article as well.

share|improve this answer
    
isn't find ~/Downloads -type f -iname '*.mp3' | while read mp3file; do cp "$mp3file" ~/Music; done simpler? –  steabert Aug 25 '12 at 10:49
    
My method represents best practice and prevents the script from breaking, as noted at the link I quoted. –  user76204 Aug 25 '12 at 10:54
    
ah, I see, that takes care of handling special characters, thanks for the link :) –  steabert Aug 25 '12 at 11:02
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.