Guide to Unix/Commands/Finding Files

find

edit

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

Warning: 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.

Links:

whereis

edit

Searches the normal executable and man page locations for a specified file. Does not seem covered by POSIX.

Examples:

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

Links:

which

edit

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/pine

Links:

locate

edit

Finds all filenames that match the specified query. Absent from traditional UNIX, is GNU software and comes standard with Linux.

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

Note that locate uses 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.

Links:

xargs

edit

Turns items in its input stream into arguments of another command to invoke. One common use is in conjunction with find.

Examples:

  • echo a b c | xargs -n1 echo
    • Outputs a, b, and c, each one on a separate line; -n1 specifies that at most 1 input item should be passed to the command at a time.
  • echo a,b,c | xargs -d, -n1 echo
    • In certain versions of GNU xargs, outputs a, b, and c, each one on a separate line. -d specifies the item delimiter.

Links: