Hack46.Find All Your Media Files


Hack 46. Find All Your Media Files

Use standard command-line tools to track down all of the media files scattered around your computer.

Even with all the great ID3 tagging tools and other programs, we have to organize our media files; sometimes they just get put in strange places. When your collection becomes so scattered that you can't track down files you want, it's time for some spring cleaning. This hack tells you how to track down all the media files on your system, no matter where they are hiding.

2.35.1. The Fast Way

The quickest way to locate your media files is with, well, the locate command. locate uses a database that stores information about where all the files are on a system. On most Linux systems, this database is updated nightly. The up side of locate is that you get information quickly because it searches the database instead of the filesystem. The down side is that the data is up to a day old, so any media files added to the system since the last database update won't show up.

So, to locate all of the MP3 files on a system, type:

 $ locate -i *.mp3  

The -i option tells locate to ignore case, so this will find all files that end in .MP3, .mp3, or even .Mp3. Of course, you might want to locate not only all your MP3 files, but also all your OGG Vorbis and WAV files. While you could do this with individual commands, locate supports regular expressions, so you can combine it all into a single command:

 $ locate -i -r '\.(mp3|ogg|wav)$' 

You can use this same idea to track down all of the video files on your system as well. Just replace the mp3, ogg, or other audio file extensions with video file extensions like avi, mpg, and so on.


Now that you have a complete list of files, you can store that to a file for use in a shell script or tar command later.

2.35.2. The Slow Way

locate is fast and works fine, as long as you don't need data that's up to date. If you just added some media files to your system and you want to make sure they are part of the list, you will need to use find. find doesn't use a preexisting database for its informationit scans the filesystem directly. As a result it can take some time to find files, particularly if you run this against the entire root filesystem. So, to search the entire filesystem for all MP3 files using find, type:

 $ find / -iname "*.mp3"  

The -iname argument tells find to be case insensitive. Be prepared for this command to chug along and take some time, particularly on a large filesystem. To perform the same command, only on multiple extensions, you need to use the regular expressions pattern matching of find:

 $ find / -iregex '.*\.\(mp3\|ogg\|wav\)$' 

Holy leaning toothpicks, Batman! Yes, all those slashes are necessary to escape the regular expression. Notice I replaced -iname with -iregex to specify that the pattern was a case insensitive regular expression.

Now, one nice thing about find is that it makes it easy to search within a particular directory, so if you only wanted to know about all the MP3 files within your home directory, just change the path find searches in:

 $ find ~ -iname "*.mp3" 

Change ~ to the directory path you want to search. Of course, with all these examples, the output goes directly to the screen. That can be difficult to read through if you have a lot of files, so you probably want to redirect the output to a file you can read through later. To redirect the output of the last command into a file called MP3files in your home directory, type:

 $ find ~ -iname "*.mp3" >  ~/MP3files  

Another powerful ability of find is to execute commands on each file it finds using the -exec argument. With the -exec argument you could, for instance, move or remove all the MP3 files on a system while leaving other files intact. To use the -exec argument, type -exec as the last argument to find, and then follow it with the command you want to run with "{}" instead of the filename. When you are finished with the command, use \; to tell find where the command ends. find will run that command on every file it finds and fill in "{}" with the path to that file. So, to move every MP3 file in a directory (here ~/mp3/) to the /tmp directory, you would type:

 $ find ~/mp3/ -iname '*.mp3' -exec mv  "{}" /tmp / \; 

Be very careful when using the -exec argument to find, especially when performing destructive operations. Test the command first with echo or a similar command to be sure you are acting on the files you intend to. I won't name any names, but a good friend of mine accidentally deleted all the files in his home directory except for MP3s due to mistyping a find command.





Linux Multimedia Hacks
Linux Multimedia Hacks
ISBN: 596100760
EAN: N/A
Year: 2005
Pages: 156

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net