UNIX Find A File Command

by on May 14, 2008 · 19 comments· LAST UPDATED August 31, 2008

in , ,

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:



If you would like to be kept up to date with our posts, you can follow us on Twitter, Facebook, Google+, or even by subscribing to our RSS Feed.


{ 19 comments… read them below or add one }

1 Mich May 14, 2008 at 10:46 pm

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

:)

Reply

2 Patrick May 2, 2012 at 12:56 pm

grep -i -n ‘word’ *
* = where to search.

Reply

3 vivek May 15, 2008 at 8:25 am

Mich,

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

HTH

Reply

4 yoander May 15, 2008 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.

Reply

5 nephish May 16, 2008 at 1:09 pm

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

Reply

6 bhanu June 24, 2008 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.

Reply

7 sophia November 7, 2008 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.

Reply

8 neha February 16, 2009 at 5:40 am

nice aricle with good examples

Reply

9 Anonymous July 7, 2010 at 11:45 am

how to see all commands in unix ?

Reply

10 Ahmad Aadil April 5, 2011 at 9:11 am

Press tab two times.

Reply

11 Howard June 3, 2011 at 10:05 pm

Regarding executing commands on files found by the find command, is it possible to execute the cat command on files found by the find command? If so, it would be great if you could provide some examples of how this could be done.

I want to use the find command to find all regular files in a directory used to store scripts and then execute either a “cat -n” or plain “cat” command to print the contents of each of the files (i.e., scripts) which were found in the directory.

Thanks. Howard

Reply

12 Vivek Gite June 4, 2011 at 1:11 am
find /path/to/dir -type f -name "*" -type f -exec cat -n {} \;

Reply

13 BB October 3, 2011 at 8:01 pm

I would like to find a “file” or “link” by a certain name. Is that possible with find ?

For example, if I have the following file in the current directory, can “find” locate
the file by either “pop” or “pullit” ?

lrwxrwxrwx 1 bb other 6 Oct 3 19:55 pop -> pullit

Thanks,
BB

Reply

14 AJ March 21, 2012 at 6:43 pm

What command can be used to find all files that contain a given string in any sub-directory of the path you are in? I tried
grep -lri ‘string’ ./
but it is only finding the string in files within that directory, not the sub-directories.
Thanks in advance.

Reply

15 AJ March 21, 2012 at 6:52 pm

looks like this may work:
find . -exec grep -rli “string” ‘{}’ \; -print

Reply

16 izzat sahil April 22, 2012 at 9:02 am

dear all,
Where can i find all the important commands and main concepts of linux.

thanx.

Reply

17 sagar May 1, 2012 at 4:06 pm

how to find all files with file names strting wid e?

Reply

18 Maneesh sharma May 25, 2012 at 11:14 am

Use ACK instead of find or grep and your life will be lot more better. for more details visit http://betterthangrep.com/

Reply

19 abhinav February 2, 2013 at 7:01 am

i want to find permission of perticular files tell about that

Reply

Leave a Comment

You can use these HTML tags and attributes for your code and commands: <strong> <em> <ol> <li> <u> <ul> <kbd> <blockquote> <pre> <a href="" title="">

Tagged as: , , , , , ,

Previous Faq:

Next Faq: