next up previous contents index
Next: Information About the System Up: Linux Shell Environment Previous: Expressing Permissions in Binary   Contents   Index


Finding Files

If you are looking for a file there are a couple of ways of doing it. The easiest way is to use the locate command:

bash$ locate useradd
/usr/share/man/man8/useradd.8.gz
/usr/share/man/pl/man8/useradd.8.gz
/usr/sbin/useradd

This sometimes gives you more information than you really wanted. If you were looking for the useradd command to add users, then you found it, but you also found a couple of manual pages. If there were a lot of files with useradd in the names then you would have a long list. The next command you could try is which:

bash$ which useradd
/usr/sbin/useradd

This helps you to know where the file is, but it only works for executable files in your path. If you were looking for a text file then you would be better off with locate. What if you wanted to look for a file that was owned by someone, but you did not know what the name was? You could use the command find:

bash$ find /home/ -user bob
find: /home/dave: Permission denied
/home/bob
/home/bob/.bash_logout
/home/bob/.bash_profile
/home/bob/.bashrc
/home/bob/.canna
/home/bob/.kde
/home/bob/.kde/Autostart
/home/bob/.kde/Autostart/Autorun.desktop
/home/bob/.kde/Autostart/.directory
/home/bob/Desktop
/home/bob/Desktop/kontrol-panel
/home/bob/Desktop/.directory
/home/bob/Desktop/Linux Documentation
/home/bob/Desktop/www.redhat.com
/home/bob/Desktop/Printer
/home/bob/.emacs
/home/bob/.screenrc
/home/bob/.wl
/home/bob/.xauth
/home/bob/.bash_history

This found all of the files that Bob owned that were in /home or any of its subdirectories. You will probably notice that the first line was not a file. This line is an error messages. You can get rid of the error messages by using a redirection of standard error to the /dev/null device:

bash$ find /home/ -user bob 2> /dev/null
/home/bob
/home/bob/.bash_logout
/home/bob/.bash_profile
/home/bob/.bashrc
/home/bob/.canna
/home/bob/.kde
/home/bob/.kde/Autostart
/home/bob/.kde/Autostart/Autorun.desktop
/home/bob/.kde/Autostart/.directory
/home/bob/Desktop
/home/bob/Desktop/kontrol-panel
/home/bob/Desktop/.directory
/home/bob/Desktop/Linux Documentation
/home/bob/Desktop/www.redhat.com
/home/bob/Desktop/Printer
/home/bob/.emacs
/home/bob/.screenrc
/home/bob/.wl
/home/bob/.xauth
/home/bob/.bash_history

This is a very powerful command, but it takes a little getting used to. If you know what directory a file is in and you know what you are looking for you might want to check out the powers of grep. Grep searches the contents of each file and matches strings. Here is a a line that you could use to search for the word ``bob'' in files in the /etc/ directory. Notice that we will again redirect standard error to the /dev/null device:

bash$ grep bob /etc/* 2> /dev/null
/etc/group:bob:x:502:
Binary file /etc/ld.so.cache matches
/etc/passwd:bob:x:502:502::/home/bob:/bin/bash
/etc/termcap:bobcat|sbobcat|HP 9000 model 300 console:\
/etc/termcap:#	* Added `bobcat' and `gator' HP consoles and the Nu machine entries
/etc/termcap:#	  hpgeneric, hpansi, hpsub, hp236, hp700-wy, bobcat, dku7003, adm11,

Well, that is a lot of information. It lists a few files and the line that has the three letter string ``bob''. You can search the immediate subdirectories of /etc/ in almost the same way:

bash$ grep bob /etc/*/* 2> /dev/null

Unfortunately, there is nothing there. Hopefully these four commands will help in finding files.


next up previous contents index
Next: Information About the System Up: Linux Shell Environment Previous: Expressing Permissions in Binary   Contents   Index
Joseph Colton 2002-09-24