Searching For Files with find

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.