Unix Commands for Finding Files

Commands:

1. find 
2. whereis 
3. which 
4. locate 

[edit] find
find searches a given path for a file or folder. The syntax is: find [path...] [expression...]

Examples: On some of the latest Unix-like OS's, the -print option is a default and can be omitted. The following command searches for the file 'grub.conf' starting at the root ('/') directory.

$ find / -name grub.conf
/etc/grub.conf
If you are not the administrator of the computer, you get error messages for all the directories you are not allowed to read. In this case do it like this for a bash shell:

$ find / -name grub.conf 2>/dev/null
/etc/grub.conf
Or like this for a csh/tcsh:

$ find / -name grub.conf >& /dev/null
/etc/grub.conf
The following command will search for all directories named 'local'.

$ find / -name local -type d
/usr/X11R6/lib/X11/fonts/local
/usr/local
/var/cache/man/local
/var/local
Tips: Using 'exec' option executes certain commands for each file found by find:

$ find . -name '*bak' -exec rm -i {} \;
rm: remove regular empty file `./file1.bak'? y
rm: remove regular empty file `./file2.bak'? y
rm: remove regular empty file `./file3.bak'? y
Using 'ok' has same effect but it will prompt for every file:

$ find . -name '*~' -ok rm {} \;
< rm ... ./RMAIL~ > ? y
 When using "-exec" or "-ok", a semicolon must be used to indicate the end of the arguments (to "rm" in the example). Because semicolon is special to the shell, it must be quoted. The above examples quote the semicolon with a backslash. 

[edit] whereis
whereis searches the normal executable and man page locations for a specified file.

Examples:

$ whereis ls 
ls: /bin/ls /usr/bin/ls /usr/man/man1/ls.1.gz /usr/share/man/man1/ls.1.gz

[edit] which
which searches the locations in your PATH variable for a specified file. If you know a program is in your path (i.e you can run it) this is faster than whereis.

$ which pine
/usr/bin/pineddd

[edit] locate
locate finds all filenames that match the specified query.

Examples:

$ locate make.conf
/etc/make.conf
/etc/make.conf.orig
/etc/make.conf.example
/usr/qt/3/mkspecs/linux-g++/qmake.conf
/usr/share/man/man5/make.conf.5.gz

locate however, is a GNU software and the command is not a standard in traditional UNIX systems like Solaris.  The locate command comes standard with Linux based systems.

Have a Unix Problem
Do you have a UNIX Question?

Unix Books :-
UNIX Programming, Certification, System Administration, Performance Tuning Reference Books

Return to : - Unix System Administration Hints and Tips

(c) www.gotothings.com All material on this site is Copyright.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
All product names are trademarks of their respective companies.
The site www.gotothings.com is in no way affiliated with or endorsed by any company listed at this site.
Any unauthorised copying or mirroring is prohibited.