UNIX Find A File Command

Q. I've just started to use Sun Solaris UNIX in our Labs. I'd like to know more about finding files from the shell prompt. How do I find a file under UNIX?

A. You need to use find command which is used to search files and directories under UNIX and Linux like operating systems. You can specify criteria while search files. If no criteria is set, it will returns all files below the current working directory. find also supports regex matching and other advanced options.

Examples

Find all perl (*.pl) files in current directory:
$ find . -name '*.pl'
The . represent the current directory and the -name option specifies all pl (perl) files. The quotes avoid the shell expansion and it is necessary when you want to use wild card based search (without quotes the shell would replace *.pl with the list of files in the current directory).

To list only files and avoid all directories

$ find . -type f -name '*.pl'
Above command will only list files and will exclude directories, special files, pipes, symbolic links etc.

Search all directories

Search file called httpd.conf in all directories:
$ find / -type f -name httpd.conf
Generally this is a bad idea to look for files. This can take a considerable amount of time. It is recommended that you specify the directory name. For example look httpd.conf in /usr/local directory:
$ find /usr/local -type f -name httpd.conf

Execute command on all files

Run ls -l command on all *.c files to get extended information :
$ find . -name "*.c" -type f -exec ls -l {} \;
You can run almost all UNIX command on file. For example, modify all permissions of all files to 0700 only in ~/code directory:
$ find ~/code -exec chmod 0700 {} \;
Search for all files owned by a user called payal:
$ find . -user
$ find . -user payal

Read find command man page for detailed information:
$ man find
See our previous articles about finding files:

Want to read Linux tips and tricks, but don't have time to check our blog everyday? Subscribe to our daily email newsletter to make sure you don't miss a single tip/tricks. Subscribe to our weekly newsletter here!

{ 7 comments… read them below or add one }

1 Mich 05.14.08 at 10:46 pm

It would be interesting to know how to find files that contains certain words in the text (inside the file)

:)

2 vivek 05.15.08 at 8:25 am

Mich,

grep 'main()' *.c
grep -r "string" /home/you
find . -exec grep -H "string" '{}' \; -print

HTH

3 yoander 05.15.08 at 1:36 pm

Also you can combine find with xargs in a powerful ways for example:


find ~/code -print0 | xargs -0 chmod -v 0700

the previous commands change mode of every file under
~/code including code directory.

4 nephish 05.16.08 at 1:09 pm

great article, thanks for putting this out there, all these find tips are in my notes now.

5 bhanu 06.24.08 at 11:49 am

find . -name “chapter*” -print | xargs grep “Bhanu”
It will search for the files starting with chapter,
and with in these files it will search for Bhanu string.

6 sophia 11.07.08 at 3:55 am

i would like to ask how to use the find command to locate all the hidden files in a folder. the hidden files are error files.

7 neha 02.16.09 at 5:40 am

nice aricle with good examples

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Tagged as: , , , , , ,

Previous post: Ubuntu Linux Install Firefox 3 Web browser

Next post: Unix Create a Symbolic Link