How do I search for file in Bash?
You can use the following commands to search for files in a bash shell:
- 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.
- find command - search for files in a directory hierarchy in real time.
locate command in bash
To search a file called xorg.conf, enter:
/etc/X11/xorg.conf /etc/X11/xorg.conf.backup /etc/X11/xorg.conf.failsafe /home/vivek/Downloads/xorg.conf.txt /usr/share/man/man5/xorg.conf.5.gz
Instead of writing file names on scree, write the number of matching entries only, enter:
locate -c xorg.conf
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:
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:
- Linux: Finding and Locating files with find command part # 1
- Linux / UNIX: Finding and locating files with find command part # 2
- Find command: Exclude / Ignore Files ( Ignore Hidden .dot Files )
- Linux or Unix find and remove files with one find command on fly
- Linux Find Large Files
- How To Find Files by Content Under UNIX
- 30 Cool Open Source Software I Discovered in 2013
- 30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X
- Top 30 Nmap Command Examples For Sys/Network Admins
- 25 PHP Security Best Practices For Sys Admins
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- Linux: 20 Iptables Examples For New SysAdmins
- Top 20 OpenSSH Server Best Security Practices
- Top 20 Nginx WebServer Best Security Practices
- 20 Examples: Make Sure Unix / Linux Configuration Files Are Free From Syntax Errors
- 15 Greatest Open Source Terminal Applications Of 2012
- My 10 UNIX Command Line Mistakes
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- The Novice Guide To Buying A Linux Laptop