Find Files by Name


find -name

find is basically used to look for files by name, or part of a name (hence the -name option). By default, find is automatically recursive and searches down through a directory structure. Let's look for all MP3 files sung by the unique group the Shaggs on the music drive:

$ cd /media/music $ find . -name Shaggs ./Outsider/Shaggs 


What? This can't be correct! The find command found the folder, but not the songs. Why? Because we didn't use any wildcards, find looked for files specifically named "Shaggs." There is only one item with that precise name: the folder that contains the songs. (Since a folder is a special kind of file, it's counted!)

We need to use wildcards, but in order to prevent the shell from interpreting the wildcards in ways we don't intend, we need to surround what we're searching for with quotation marks. Let's try the search again with our new improvements:

$ find . -name "*Shaggs*" ./Outsider/Shaggs ./Outsider/Shaggs/Gimme_Dat_Ting_(Live).mp3 ./Outsider/Shaggs/My_Pal_Foot_Foot.ogg ./Outsider/Shaggs/I_Love.mp3 ./Outsider/Shaggs/Things_I_Wonder.ogg 


We surrounded the wildcards with quotation marks; lo and behold, we found the folder and the files.

Note

Another option to find that you've been using without realizing it is -print. The -print option tells find to list the results of its search on the terminal. The -print option is on by default, so you don't need to include it when you run find.


Another important aspect of find is that the format of your results is dependent upon the path searched. Previously, we used a relative path, so our results were given to us as relative paths. What would happen if we used an absolute pathone that begins with a /instead?

$ find / -name "*Shaggs*" /music/Outsider/Shaggs /music/Outsider/Shaggs/Gimme_Dat_Ting_(Live).mp3 /music/Outsider/Shaggs/My_Pal_Foot_Foot.ogg /music/Outsider/Shaggs/I_Love.mp3 /music/Outsider/Shaggs/Things_I_Wonder.ogg 


If you search using a relative path, your results use a relative path; if you search using an absolute path, your results use an absolute path. We'll see other uses of this principle later in the chapter. For now, just keep this important idea in mind.

Note

To find out more about the Shaggs, see www.allmusic.com/cg/amg.dll?p=amg&sql=11:qyk9kett7q7q, or just search www.allmusic.com for "Shaggs." You haven't lived until you've played "My Pal Foot Foot" at your next party!




Linux Phrasebook
Linux Phrasebook
ISBN: 0672328380
EAN: 2147483647
Year: 2007
Pages: 288

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