Finding all .mp3 files and move to new directory from shell prompt

by on January 7, 2007 · 33 comments· Last updated January 14, 2007

I have mp3 music file all over my file system. I'd like to move them onto specific directory called /mnt/mp3. So how do you find and move all mp3 files to /mnt/mp3 directory?

Simply use find command. It locates all files and then executes a command to move them to /mnt/mp3 (any other directory).

Step # 1: Finding all your .mp3 files

Following will just find all your .mp3 files using find command:

# find / -iname "*.mp3" -print

Where,
=> / - Search / root directory
=> -iname - File name search pattern (case insensitive)
=> -print - Display name of files

Step # 2: Finding and moving all your .mp3 files in one pass

Type the following command to find and move all files to /mnt/mp3 directory:
# find / -iname "*.mp3" -exec mv {} /mnt/mp3 \;
Where,

  • -exec mv {} /mnt/mp3 \;: Execute mv command. The string '{}' is replaced by the file name. \; ends /bin/mv command.

Tip: You just need to move .mp3 files and not directory, use:
# find / -iname "*.mp3" -type f -exec /bin/mv {} /mnt/mp3 \;

Find all directories having name mp3 and move:
# find / -iname "*.mp3" -type d -exec /bin/mv {} /mnt/mp3 \;

For performance you may need to consider using xargs command:
find / -iname "*.mp3" -type f | xargs -I '{}' mv {} /mnt/mp3

You can also write a script that moves files along with directories. This is also useful to move all files to mp3 player that has been mounted on /mnt/mp3 directory.



You should follow me on twitter here or grab rss feed to keep track of new changes.

Featured Articles:

{ 33 comments… read them below or add one }

1 Jeff Schroeder January 8, 2007 at 6:25 pm

find -exec {} forks a process for *every* found file and is not the best way of doing things. xargs is better in that it checks the maximum command length and intelligently breaks up commands. This makes it:
a.) Faster
b.) More efficient
c.) Yet another way to do the same thing

Here is the magic:
find / -iname -type f “*.mp3″ | xargs -I ‘{}’ mv {} /mnt/mp3

Reply

2 nixcraft January 10, 2007 at 6:58 am

Jeff

Thanks for pointing out xargs. I’ve updated the post.

Reply

3 Scott Carlson January 14, 2007 at 12:55 pm

Some of you arguments are out of order.
The correct statements are

find / -iname “*.mp3″ -type d -exec /bin/mv {} /mnt/mp3 \;
find / -iname “*.mp3″ -type f -exec /bin/mv {} /mnt/mp3 \;
find / -iname “*.mp3″ -type f | xargs -I ‘{}’ mv {} /mnt/mp3

Reply

4 nixcraft January 14, 2007 at 11:03 pm

Scott,

…article has been updated

Appreciate your post.

Reply

5 Dhyanesh Vyas June 23, 2007 at 11:17 am

Jai Swaminarayan

You have done a great job
Keep it up
Just Excellent

Reply

6 kevin September 2, 2007 at 8:34 pm

and how can i actually move the files along with their directory? incl Xargs argument..? that would be just perfect!
thnx a lot in advance?

Reply

7 Troy Palacino December 8, 2007 at 10:31 pm

thank you guys so much for this article and the additional posts. i just had to do a file recovery from a hard drive that i accidentally erased when migrating from windows to linux and it restored all my music into approximately 12,000 different directories and i was going through each folder pulling them out cuz there isn’t a app that allowed me to find and move. this was EXACTLY what i needed. Thanx again for the helpful post.

Reply

8 luis January 15, 2008 at 1:34 pm

Really usefull!
thx from spain!

Reply

9 notworking September 27, 2008 at 3:00 pm

xargs: unmatched single quote; by default quotes are special to xargs unless you use the -0 option

Reply

10 nbb January 7, 2009 at 11:43 am

xargs: unmatched single quote; by default quotes are special to xargs unless you use the -0 option

Reply

11 Anup July 17, 2009 at 7:18 am

find / -iname “*.mp3″ -type f -print0 | xargs -0 -I ‘{}’ mv {} /mnt/mp3

This should get rid of “xargs: unmatched single quote; by default quotes are special to xargs unless you use the -0 option”

Reply

12 Ignacio Soto Reveco August 16, 2009 at 9:11 am

hey thanks, from CHILE,

i’ve lost all my information because y made a mistake on installing linux and unfortunate i formatted the disk, and all my data lost, then y install testdisk and y recover 90 percent of all my data, but it comes with serial name, like f123113.txt, f124434.pdf also in directories of 500 files, i had 75 directories, (75*500=a lot of work searching for pdf).

so i reorganized my files by type in different folders

thxs a lot

sorry for poor English…

Reply

13 Isaiah Roberts December 4, 2009 at 2:50 am

Hey, thanks a lot for this! I’ve been studying bash scripting so I could do exactly this! Thanks a lot! btw.. It wasn’t clear to me, does this copy *just* the files, or does it also copy the directories? Thanks a lot again!

Isaiah

Reply

14 Scott Carlson December 4, 2009 at 2:33 pm

@Isaiah

It copies files from all the subdirectories into one target directory.

Reply

15 Isaiah Roberts December 4, 2009 at 3:14 pm

Ah, that’s what I figured. Well, thank you very much! It’s really nice, I’m going to try to find a way to write a script that searches out all mp3s within a directory and it’s subdirectories(say, my “Music” directory), and then copy only the mp3s out, but keep the structure… I’m sure this is not only possible, but fairly easy… sad to say, I’m just learning shell scripting, so I have very little idea how to do it. I’m currently just figuring out the steps I’d need to take to get this working, then I’m going to use my trusty friend “google” to help me translate that into shell scripting. Many thanks! If anyone has any ideas on how to get this working, I’d be eternally grateful! :)

Isaiah(dreadpirate15)

Reply

16 Scott Carlson December 4, 2009 at 3:30 pm

@Isaiah

I’m sure there is a simpler way, but I can think of doing it in two passes

find / -iname “*.mp3″ -type f -print0 | xargs -0 -I ‘{}’ mkdir -p /mnt/mp3/`basename {}`
find / -iname “*.mp3″ -type f -print0 | xargs -0 -I ‘{}’ mv {} /mnt/mp3/{}

Reply

17 Thanks Scott February 9, 2010 at 6:36 pm

This is very helpful! Thanks for all your help! Quick question: Is there a way to delete the directory where the MP3 came from?

Reply

18 Scott Carlson February 10, 2010 at 12:45 pm

If you just want to clean up empty directories after you’re done, this is a quick command I keep in a script called pruneEmptyDirs:

perl -MFile::Find -e”finddepth(sub{rmdir},’.')”

Reply

19 casperz February 10, 2010 at 7:34 pm

I am new to linux, taking classes, reading, etc…
Can someone explain to me what the “iname” in this command is? I’m not familiar with that
”’find / -iname “*.mp3″ -type f -print0 | xargs -0 -I ‘{}’ mv {} /mnt/mp3/{}”’

Reply

20 casperz February 10, 2010 at 7:37 pm

I want to run mplayer in Fedora, scan my drive for mp3 and relocate to an mp3 directory

Reply

21 Scott Carlson February 11, 2010 at 4:58 pm

The “man” command will be your friend. So “man find” will explain the command and its options.

In this case -iname means do a case-insentive match on the filename, and the “-type f” means only match files not directories or symbolic links.

Reply

22 fatjoe August 4, 2010 at 10:03 am

“You can also write a script that moves files along with directories. This is also useful to move all files to mp3 player that has been mounted on /mnt/mp3 directory.”

Can someone help me with this?

Reply

23 ddarek4 August 1, 2011 at 12:29 am

Hi

After some hours i get functional script….
i don’t know why but this above command don’t work on my debian so i had to spent some time for it…
Here is working code (in this case i find .jpg but it doesn’t matter)

find /mnt/hda1/zdjecia/test1/ -type f -iname ‘*.jpg’ -printf ‘%’h'\’”0″ | xargs -0 -I ‘{}’ mkdir -p /mnt/hda1/test/{} \;
find /mnt/hda1/zdjecia/test1/ -iname “*.jpg” -type f -exec cp {} -rv /mnt/hda1/test{} ‘;’

the first makes folders and the second copying files…
I don’t know why to copy command xargs don’t works… so i used exec
sory for my bad english…

Reply

24 Rajasekar August 6, 2011 at 8:01 am

how to find mp3, jpg using linux command with file size

Reply

25 Rajasekar August 6, 2011 at 8:01 am

how to find mp3 and jpg using linux command with file size.

Reply

26 Andy Newton September 19, 2011 at 10:24 am

You could copy files and folder structure but only copy the actual files of *.mp3 type with rsync too, and this would work locally or via SSH:

locally:

rsync -av –progress –include “*/” –include “*.mp3″ –exclude “*” /some/path/ /mnt/targetpath/

remotely:

rsync -av –progress –include “*/” –include “*.mp3″ –exclude “*” rsync -av –progress –include “*/” –include “*.mp3″ –exclude “*” /some/path/ /mnt/targetpath/ username@remotehost.com:/target/path/

Reply

27 Andy Newton September 19, 2011 at 10:44 am

Sorry copy and paste fail:

Remotely:

rsync -av –progress –include “*/” –include “*.mp3″ –exclude “*” /some/path/ username@remotehost.com:/target/path/

Reply

28 Rafal March 16, 2012 at 11:07 am

Helo,
This text and comments help me with my work so I share what I made.
This is my code :
find /mail/ -type f -ctime +30 -name ‘*ST’ -printf “%h” | xargs -0 -I ‘{}’ mkdir -p /delmails{} && find /mail/ -type f -ctime +30 -name ‘*ST’ -print0 | xargs -0 -I ‘{}’ mv ‘{}’ /delmails‘{}’

i use dovecot with maildirs and imap. This code find deleted mails , older than 30 days, crate folder structure in new location and move there deleted mails.

Reply

29 twengg August 10, 2012 at 7:54 pm

Thanks nixcraft for starting this, and thanks Anup for that correction for the error . saved me lots and am loving linux even more. Thanks dudes!!

Reply

30 muzi December 1, 2012 at 6:08 am

Thanks for this, works well :)

Reply

31 Gowtham April 6, 2013 at 8:01 am

i need a shell script.that should be find the 2 months old files(location ex:/home/gowtham).The file names are saved with the format of date(example for a file name is monthdayyear,04062013,04072013).The script can find only 2 months old files,names are like 04062013 and remaining files are not need in the output.

Thanks
Gowtham

Reply

32 Gowtham April 6, 2013 at 3:10 pm

please provide a solution for the problem……….

Thanks
Gowtham

Reply

33 Rahul Janghel April 30, 2013 at 4:53 pm

Hi Friends,

I am looking for a simple bash command / script which checks source location for 30 days old file and moves to destination maintaining the same directory structure as source had.

its easy to find 30 days old file, but facing difficulty with maintained same directory structure as sources. Pls suggest.

Regards,
Rahul Janghel.

Reply

Leave a Comment

You can use these HTML tags and attributes for your code and commands: <strong> <em> <ol> <li> <u> <ul> <blockquote> <pre> <a href="" title="">
What is 4 + 7 ?
Please leave these two fields as-is:
Solve the simple math so we know that you are a human and not a bot.



Previous post:

Next post: