Linux Phrasebook
Authors: Granneman S.
Published year: 2007
Pages: 175-177/288
Buy this book on amazon.com >>


Chapter 10. The find Command

In the last chapter, we covered commands that let you search for files ( locate ) and data within files ( grep ). The third command in the powerful triumvirate is find . While locate searches a database for files, which makes it fast but dependent upon a constantly updated database, find searches for files on the fly using criteria that you specify. Since find has to parse through your file structure, it's much slower than locate , but you can do things with find that aren't possible with locate .

Throughout this chapter, we look for files on an external hard drive that contains music mounted at /media/music . You can see that find allows us to slice and dice the files in a variety of ways.



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!




Find Files by Ownership

find - user

In addition to searching for files by name , you can also search for files by owner. Do you want to find the files on the music drive owned by scott ? Use find with the -user option, followed by the user name (or the user number, which you can find in /etc/passwd ):

$ find . -user scott


Whoa! There are way too many results! It might be easier to look for files that are not owned by scott . To do so, put a ! in front of the option you wish to reverse:

Note

In order to conserve space, some of the data you'd normally see with ls -l has been removed.


$ find . ! -user scott
./Outsider/Wing/01_-_Dancing_Queen.mp3
$ ls -l ./Outsider/Wing/01_-_Dancing_Queen.mp3
gus music ./Outsider/Wing/01_-_Dancing_Queen.mp3


Ah... one song by Wing is owned by gus instead of scott . To fix this problem, we need to use chown (covered in Chapter 7, "Ownerships and Permissions"). Keep in mind that you can always use the ! as a NOT operator (which is saying, for example, "find files where the user is not scott ").

Note

Ah, Wing. First introduced to the world at large in an episode of South Park , her website can be found at www.wingtunes.com. Wing's version of "Dancing Queen" is highly recommended, as is her rendition of "I Want to Hold Your Hand."



Linux Phrasebook
Authors: Granneman S.
Published year: 2007
Pages: 175-177/288
Buy this book on amazon.com >>

Similar books on Amazon