Search For Files In Bash

How do I search for file in Bash?

You can use the following commands to search for files in a bash shell:

  1. locate command – find files by name. It reads one or more databases created by updatedb and writes file names matching at least one of the PATTERNs to the screen, one per line. This may not contains file created within last 12-24 hrs.
  2. find command – search for files in a directory hierarchy in real time.

locate command in bash

To search a file called xorg.conf, enter:
locate xorg.conf
Sample outputs:


Instead of writing file names on scree, write the number of matching entries only, enter:
locate -c xorg.conf
Sample outputs:


Ignore case matching (i.e. match foo.txt, FOO.TXT, foo.Txt and so on):
locate -i filename
Only find and limit search to one file at a time:
locate -n 1 filename
Only find and limit search to three files at a time:
locate -n 3 filename
To search for a file named exactly NAME (not *NAME*), use
locate -b '\FILENAME'
Find information about current databased created by the updatedb command:
locate -S
Sample outputs:

Database /var/lib/mlocate/mlocate.db:
	35,411 directories
	2,79,320 files
	1,96,50,749 bytes in file names
	77,85,226 bytes used to store database

find command in bash

The basic syntax is as follows:
find /path/to/dir -name "filename"
In this example, find httpd.conf file in /etc directory:
find /etc -name "httpd.conf"
To find all headers file *.h in /nas/projects directory, enter:
find /nas/projects -name "*.h"
Please see our previous FAQs about find command which covers many find command examples:

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

🐧 6 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf ncdu pydf
File Managementcat cp mkdir tree
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
6 comments… add one
  • ~ Aug 16, 2012 @ 10:16

    neither of these are helpful for me At All.

    1. locate isn’t going to work. my whole system is less than 12 hours old.

    2. what’s the point of using “find”, if i have to specify exactly where the file is in the first place?

    • 🐧 nixCraft Aug 16, 2012 @ 22:42

      If you don’t know the path, just specify / (to search entire system):

      find / -type f -iname "filename-here"
      find / -type f -iname "foo*.doc"
      • ~ Beer Oct 6, 2015 @ 20:36

        Very helpful. Thanks!

        • Victor Ude Mar 3, 2017 @ 4:19

          This comment is the best comment.

  • Cippo1987 Aug 4, 2016 @ 21:02

    I never completely understood the behavior of both commands.
    user@PC:~$ locate /home/user/Documents/*.pdf
    user@PC:~$ locate Gre*.pdf

    Why it does not find Greents.pdf ? I updated the database, and the file is definitely older than 24h. Is that related to some sort of permission on the folder?

  • MazterGee Mar 30, 2017 @ 12:43

    Thanks for posting,,

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