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

 

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.