Tips Linux Explorers   All Things Linux Forum   Great Linux Links   LinuxClues.com   Hometown    



THE FIND COMMAND


Sometimes there are tips that should have been posted long ago, but because they are less simple to explain, I never did find the time to set them up properly.
This one I found hanging around in my Pre-Tip directory for a long time, and today it is time to finally pick it up.

The "find" command is a command we use next to other commands like "locate" ( or "slocate" ) and "whereis" but it has a good few extra options we can use with it.
We can use "find" with a wild card ( * ), but it works best when we know the name of the file or directory we are looking for. ( NOTE: * does not replace a dot, so you can do "lilo.*" but not "lilo*" to find lilo.conf )

My advice is, read the text and experiment a bit to see how it works for you.



Okay, let's go for it, first step:

CODE
$ find -type f -name dummy

This is the basic way to use "find". If no path is given, it looks in the present directory and its subdirectories. Then we use the "-type f" to tell find it we are looking for a file ( f ) directory ( d ) or link ( l ) after that "-name dummy" to tell it we are looking for a file with the name "dummy" ( with wildcard "-name *ummy?" ).
You will see it will find the answer:

QUOTE=Screen
./.kde/share/config/dummy



One step up:

CODE
$ find / -type f -name dummy 2>/dev/null

This time "find" not only searches the local directory, because the first argument we give it has to look in "/" ( the complete filesystem . . . . so that takes a while ) and because we give the command as "normal user" and not as root it would lead to a lot of "permission denied" messages if we did not add the last bit "2>/dev/null" ( See: The black hole )
On my Mandrake install the answer to this command is:

QUOTE=Screen
/home/bruno/.kde/share/config/dummy
/usr/share/mdk/kde/root-interface/dummy



Another step up:

CODE
$ find / -type f -name dummy 2>/dev/null -exec cat {} \;

This time it does all the previous, but at the end of the command you see "-exec cat {} \;" . . . this means "-exec" execute the next command "cat" on the results found from the find command "{}" . . . . the "\;" means that the exec command ends there.
Again on my system the result is:

QUOTE=Screen
[$Version]
update_info=kio_help.upd:kde3_2,favicons.upd:kde3_2



Now the final step up:

CODE
$ find / -type f -name dummy 2>/dev/null -exec cat {} \; >tesst.txt

Also, this time it has all the things listed above, but I added ">tesst.txt, which tells it to ">" write them in a file called "tesst.txt" instead. ( And not put the output on the screen )

So if you do not know where your lilo.conf file is located, but you want a copy of it for reading in your home directory, here is what you do:

CODE
$ find / -type f -name lilo.conf 2>/dev/null -exec cat {} \; >lilo.txt

And you will find a file called lilo.txt in your /home directory



And because I know you want to know more, here is the FUN step:

CODE
$ find /home -type f -name "*.sxw" -atime -3 -user bruno

Here we "find" in "/home" all "-type f files with the "-name" "*.sxw" so all OpenOffice sxw documents that "-atime -3" have been accessed in the last 3 days ( "-atime +3" would be: have NOT been accessed ) and that are owned by the "-user bruno" ( So basically: has anyone been messing with my files in the last 3 days ?? )
Additional tip: if you look for *.sxw, type "*.sxw" same goes for "*.txt", "*.doc" etc.etc.



Well, that is it for today, if you want to know even more, have a look at: man find



Bruno


-- Dec 14 2004 ( Revised Dec 10 2005 ) --


Tips Linux Explorers   All Things Linux Forum   Great Linux Links   LinuxClues.com   Hometown