Skip to content

Instantly share code, notes, and snippets.

@gartenfeld
Created January 19, 2015 20:36
Show Gist options
  • Save gartenfeld/4f7c4995faa3ff29ce99 to your computer and use it in GitHub Desktop.
Save gartenfeld/4f7c4995faa3ff29ce99 to your computer and use it in GitHub Desktop.
find /dir/ -iname "*.mp3" -print
# /dir/ - Search directory
# -iname - File name search pattern (case insensitive)
# -print - Display name of files on screen
find / -iname "*.mp3" -exec mv {} /new_dir/mp3 \;
# -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
# 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/
# 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/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment