Searching For Files with find

NOTE: this website is out of date. This is the course web site from a past quarter. If you are a current student taking the course, you should visit the current class web site instead. If the current quarter's website is easily reached, it may be accessible by visiting this link instead. Please be advised that courses' policies change with each new quarter and instructor, and any information on this out-of-date page may not apply to you.

Written by Chris Gregg, with modifications by Nick Troccoli

Click here for a walkthrough video.

If you need to find a file in a particular directory, you can use the find command, which has a somewhat strange syntax:

$ pwd
/afs/.ir/users/c/g/cgregg/cs107/assignments
$ find . -name "hello.c"
./assign0/hello.c
./assign1/hello.c
$

The first argument (. above) is the directory you want to search (i.e., the current directory in the above search). This can be any directory:

$ find assign1 -name "hello.c"
assign1/hello.c
$

The second argument, -name means, "search for the name", and the final argument ("hello.c"), which is normally in quotes, gives the name to search for. This is different from grep, which searches for text within files. find also uses a slightly different wildcard than grep, so you can search for patterns like this (to search for all .c files):

$ find . -name "*.c"
./assign0/hello.c
./assign1/folder1/myProgram.c
./assign1/hello.c
./assign3/loop.c
$

The find command has a vast number of options; take a look at the man pages for more information.