Guide to Unix/Commands/Finding Files

From Wikibooks, open books for an open world
< Guide to Unix‎ | Commands
Jump to: navigation, search

find[edit]

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

If you want to ignore the case of the characters, you can use -iname. The following will find files with extensions txt, TXT, Txt, and so on:

$ find / -iname '*.txt' 
/home/rtm/hacking.txt
/home/bok/Documents/MyLetter.TXT
/home/bok/tmp/found.tXt

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

The above example combined two tests - filename (-name) and filetype (-type) - and returned files satisfying both (logical AND).

It's also possible to specify two or more tests, and return files satisfying any of them, with the -o (logical OR) directive. The tests to be ORed must be grouped together between (...) (which must be escaped with \ as parentheses are special to the shell). The following return files with the extensions txt or doc, as well as any file larger than 5MB (megabyte) in size.

$ find / \( -name '*.txt' -o -name '*.doc' -o -size +5M \)
/home/rtm/hacking.txt
/home/bok/report.doc
/home/bok/backup/may.tar.bz2
/home/koppe/BiggerThan5Megs.doc

Where the last file satisfied two tests.

Note: Each directive must be complete, tests cannot be omitted. E.g. -name '*.txt' -o '*.doc' (omitting the 2nd -name) is not valid.

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

whereis[edit]

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

which[edit]

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

locate[edit]

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.

Note that locate use a database of already collected filenames, that is typically updated once every 24 hours. As such, using locate will not correctly show newly created or deleted files/directories.