UNIX Find A File Command

I am a new Sun Solaris UNIX user in our Lab. I would like to know more about finding files from the shell prompt. How do I find a file under UNIX operating systems using bash or ksh shell?

You need to use find command which is used to search files and directories under Linux and Unix 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. The find command also supports regex matching and other advanced options.
Tutorial details
Difficulty level Easy
Root privileges No
Requirements find/locate
Est. reading time 5m

Another option is locate command. It find files by name. The locate command reads databases prepared by updatedb and displays file names matching at least one of the PATTERNs to screen.


The syntax is:
find /dir/to/search -name "file-to-search"
find /dir/to/search -name "file-to-search" -print
find /dir/to/search -name "file-to-search" -ls
find /dir/to/search -name "regex" -print

Examples – find command

To 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

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

Fig.01: Find command in action

Fig.01: Find command in action

The -type -f option only selects files. To select only dirs pass the -type d:
$ find /home/vivek/ -type d -name 'project*'
This will find directory names such as project, projects, projects-perl and so on.

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
You may need to run it as root:
$ sudo find / -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 {} \;
$ find . -name "*.c" -type f -ls
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 <userid>
$ find . -user payal

Search file for specific sizes

Search for 650 megabytes or above size file in ~/Downloads/ dir:
$ find ~/Downloads/ -size +650M
## gigabytes ##
$ find ~/Downloads/ -size +1G
## kilobytes ##
$ find ~/Downloads/ -size +1024k

Read find command man page for detailed information:
$ man find

Say hello to locate command

To search for a file named exactly foo (not *foo*), type:
$ locate -b '\foo'
Just search for file name matching yum.conf, type:
$ locate yum.conf
To search for a file named exactly yum.conf (not *yum.conf* anywhere in / path), type:
$ locate -b '\yum.conf'
Sample outputs:

Fig.02: locate command in action on Linux based system.

Fig.02: locate command in action on Linux based system.

Recommended readings:

See our previous articles about finding files:

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 27 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersncdu pydf
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
27 comments… add one
  • Mich May 14, 2008 @ 22:46

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


    • Patrick May 2, 2012 @ 12:56

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

  • 🐧 nixCraft May 15, 2008 @ 8:25


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


  • yoander May 15, 2008 @ 13:36

    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.

  • nephish May 16, 2008 @ 13:09

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

  • bhanu Jun 24, 2008 @ 11:49

    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.

  • sophia Nov 7, 2008 @ 3:55

    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.

  • neha Feb 16, 2009 @ 5:40

    nice aricle with good examples

  • Anonymous Jul 7, 2010 @ 11:45

    how to see all commands in unix ?

    • Gopal Mar 24, 2015 @ 9:23

      1). ll
      2). ls
      3). ls -lrt

  • Ahmad Aadil Apr 5, 2011 @ 9:11

    Press tab two times.

  • Howard Jun 3, 2011 @ 22:05

    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

    • 🐧 nixCraft Jun 4, 2011 @ 1:11
      find /path/to/dir -type f -name "*" -type f -exec cat -n {} \;
  • Nikhil Sep 21, 2011 @ 22:56

    I want to remove the records which contains the blank data from specific column of file e.g.
    if we consider the file format :
    Sr.No Name Marks1 Marks2 grade
    1 Nik 50 0 B
    2 Jua 25 75 B
    3 Neeta 30 0 C
    4 jem 0 15 C

    As per in above format i want to remove the records which contains value ‘0’ in Colum ‘Marks2’ .Can anyone please advice unix command for above case?
    Thanks in advance

  • BB Oct 3, 2011 @ 20:01

    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


  • AJ Mar 21, 2012 @ 18:43

    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.

    • AJ Mar 21, 2012 @ 18:52

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

  • izzat sahil Apr 22, 2012 @ 9:02

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


  • sagar May 1, 2012 @ 16:06

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

  • Maneesh sharma May 25, 2012 @ 11:14

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

  • abhinav Feb 2, 2013 @ 7:01

    i want to find permission of perticular files tell about that

  • Lokesh Aug 5, 2013 @ 16:00

    How to find inode number for particular file.

  • bob Jun 13, 2014 @ 14:35

    What exactly is the difference between yum.conf and (*yum.conf* anywhere in / path)?


  • Revathy G Nov 29, 2016 @ 6:52

    How to find the file using grep command which is inside subdirectories?

  • chinna durai Feb 22, 2017 @ 2:23

    Hi Guys,

    How to find all files and put the result into a file? any idea about this.

    find ./user -type f -name "*" -exec ls -lrt {} \;

    I want to put the above result into a file. is it possible?

    • 🐧 Vivek Gite Feb 22, 2017 @ 6:32

      find ./user -type f -name "*" -exec ls -lrt {} \; > output.txt
      find /path/to/dir/ -type f -ls > output.txt

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum