Finding All Your MP3 Files

Problem

You have MP3 audio files scattered all over your filesystem. You’d like to move them all into a single location so that you can organize them and then copy them onto a music player.

Solution

The find utility can locate all of those files and then execute a command to move them where you want. For example:

$ find . -name '*.mp3' -print -exec mv '{}' ~/songs ;

Discussion

The syntax for the find utility is unlike other Unix tools. It doesn’t use options in the typical way, with dash and single-letter collections up front followed by several words of arguments. Rather, the options look like short words, and are ordered in a logical sequence describing the logic of which files are to be found, and what to do with them, if anything, when they are found. These word-like options are often called predicates.

A find command’s first arguments are the directory or directories in which to search. A typical use is simply (.) for the current directory. But you can provide a whole list of directories, or even search the entire filesystem (permissions allowing) by specifying the root of the filesystem (/) as the starting point.

In our example the first option (the -name predicate) specifies the pattern we will search for. Its syntax is like the bash pattern matching syntax, so *.mp3 will match all filenames that end in the characters “.mp3”. Any file that matches this pattern is considered to return true and will thus continue to the next predicate of the command.

Think of it this way: find will climb around on the filesystem and each filename that it finds it will present to this gauntlet of conditions that must be run. Any condition that is true is passed. Encounter a false and that filename’s turn is immediately over, and the next filename is processed.

Now the -print condition is easy. It is always true and it has the side effect of printing the name to standard output. So any file that has made it this far in the sequence of conditions will have its name printed.

The -exec is a bit odd. Any filename making it this far will become part of a command that is executed. The remainder of the line, up to the ;, is the command to be executed. The {} is replaced by the name of the file that was found. So in our example, if find encounters a file named mhsr.mp3 in the ./music/jazz subdirectory, then the command that will be executed will be:

mv ./music/jazz/mhsr.mp3 ~/songs

The command will be issued for each file that matches the pattern. If lots and lots of matching files are found, lots and lots of commands will be issued. Sometimes this is too demanding of system resources and it can be a better idea to use find just to find the files and print the filenames into a datafile and issue fewer commands by consolidating arguments several to a line. (But with machines getting faster all the time, this is less and less of an issue. It might even be something worthwhile for your dual core or quad core processor to do.)

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.51.246