Sort Many Files in Subdirectories into Directories by File Type
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Something like this should do what you want/need: find . -type f -iname "*.mp3" -exec mv {} /path/to/mp3dir/ \;
The above find command starts looking for files in the directory you are standing (the . [dot]), it will only look for normal files (the -type f part) and then look for files ending with mp3 (-iname "*.mp3"), the iname makes sure that it is case insensitive. The -exec ........ part moves files that are found to the /path/to/mp3dir/ directory.
Just a few questions, though, so I can understand .
What does the {} after mv do, as well as the \;?
Quote:
Originally Posted by druuna
Hi,
Have a look at the find command.
Something like this should do what you want/need: find . -type f -iname "*.mp3" -exec mv {} /path/to/mp3dir/ \;
The above find command starts looking for files in the directory you are standing (the . [dot]), it will only look for normal files (the -type f part) and then look for files ending with mp3 (-iname "*.mp3"), the iname makes sure that it is case insensitive. The -exec ........ part moves files that are found to the /path/to/mp3dir/ directory.
The {} is replaced by the file(s) that are found by the find command
Perhaps more accurately, the {} is a placeholder for each individual filename that matches the rest of the find arguments. So, it will be given iteratively as an argument to the mv command. The difference is somewhat subtle, and in this example it may not matter, however in many other similar applications it does matter.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.