Finding All .mp3 Files And Move To a New Directory From A Shell Prompt

I have mp3 music files all over my file system. I would like to move them into specific directory called /mnt/mp3. So how do you find and move all mp3 files to /mnt/mp3 directory on Linux or Unix-like system?

Simply use the find command. It locates all files and then executes a command to move each mp3 file to /mnt/mp3 directory.

Warning: Backup all your data before you type the following commands.

Step # 1: Finding all your .mp3 files

The following command will just find all your .mp3 files using the find command:
# find / -iname "*.mp3" -print
Where,

  1. / – Search / root directory
  2. -iname – File name search pattern (case insensitive)
  3. -print – Display name of files on screen

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

Type the following command to find and move all files using mv command 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.

To just move .mp3 files and not directories, 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

Sp to moves all .mp3 files with special characters in its name such as white spaces try:
find / -iname "*.mp3" -type f -print0 | xargs -0 -I '{}' /bin/mv "{}" /mnt/mp3/

Final solution

Above commands will not maintain sub-directory structure. Try replacing mv with rsync to use the same directory structure on the target directory with the -R option:
find / -iname "*.mp3" -type f -print0 | xargs -0 -I '{}' /usr/bin/rsync -avR "{}" /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.

🐧 If you liked this page, please support my work on Patreon or with a donation.
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source & DevOps topics via:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
41 comments… add one
  • Emma Sep 26, 2016 @ 21:31

    So, I ran the following command:
    find /myfolders -iname “*.mp4” -type f -exec /bin/mv {} /newfolder ;
    on a Centos box.
    The command correctly found all the mp4 files in the folder and subdirectories but instead of moving them to a folder, it somehow moved them all into a single file which is now totally unusable. Where was my mistake?

  • Al Jan 22, 2014 @ 4:37

    I highly recommend the article be updated as per Scott Carlson’s comment. Unfortunately, I read his comment after I overwrote a few files.

    @Scott: If you’re still interested, the following link will provide you a way to rename your photos based on exif data: http://www.tuxradar.com/answers/433
    I combined the above, your recommendation, and the script at the above link and was able to rename my files with a unique name without overwriting any existing file.

    • NIX Craft Jan 22, 2014 @ 12:05

      The post has been updated. I have also posted a workaround to copy sub-directory structure. Hope this helps!

  • scott carlson Oct 1, 2013 @ 13:14

    you could use “-n” with the mv, which is “noclobber”, meaning it will not clobber any existing files.

    ….. xargs -0 -I ‘{}’ mv -n {} /mnt/mp3/{} …..

    As always, take backups.

    I hate that photos end up with the same name. You could write a script that renames them based on exif data. so IMG_1234.jpg becomes IMG_1234-Canon_A560-201309011120.jpg Which would be much less likely to conflict.
    Or just to keep them unique, you could use an MD5SUM in the name like IMG_1234-calculatedMD5sum.jpg

  • Ki Oct 1, 2013 @ 4:29

    Hi
    Does this script overwrites files with the same name?
    Is there a way to prevent this? I have several files with the same name, (I’m moving pictures) but I’d like to keep them all. Is it possible?

  • Gary Stafford Jul 7, 2013 @ 14:29

    Realize this is an old post, but this was exactly what I was looking for to solve a problem I had with my script. Thanks!
    find . -maxdepth 1 -type f | xargs -I ‘{}’ sudo mv {} /directory1/directory2

  • gur Jun 28, 2013 @ 3:41

    very bad command. i mix my whole server files not selected directory.

  • Rahul Janghel Apr 30, 2013 @ 16:53

    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.

  • Gowtham Apr 6, 2013 @ 15:10

    please provide a solution for the problem……….

    Thanks
    Gowtham

  • Gowtham Apr 6, 2013 @ 8:01

    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

  • muzi Dec 1, 2012 @ 6:08

    Thanks for this, works well 🙂

  • twengg Aug 10, 2012 @ 19:54

    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!!

  • Rafal Mar 16, 2012 @ 11:07

    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.

  • Andy Newton Sep 19, 2011 @ 10:44

    Sorry copy and paste fail:

    Remotely:

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

  • Andy Newton Sep 19, 2011 @ 10:24

    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/

  • Rajasekar Aug 6, 2011 @ 8:01

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

  • Rajasekar Aug 6, 2011 @ 8:01

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

  • ddarek4 Aug 1, 2011 @ 0:29

    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…

  • fatjoe Aug 4, 2010 @ 10:03

    “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?

  • Scott Carlson Feb 11, 2010 @ 16:58

    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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.