Linux – How to find file on system

You need use the find command to search for files in a directory hierarchy under Linux OS.

Find command syntax to find a file on my system

find Search-Directory-Path -name file-name-to-search
## only search for files ##
find Search-Directory-Path -type f -name file-name-to-search
## only search for directories/folders ##
find Search-Directory-Path -type d -name dir-name-to-search

Examples to find a file on my Linux system

To find a file named zyz in /home/user/ or /home/vivek/ directory type the command:
$ find /home/user/ -name xyz
$ find /home/vivek/ -name xyz

To find a file called passwd in / (search entire system) directory type the command:
$ sudo find / -name passwd

How to search entire hard drive for a file when using Linux

Say you want to find out a file named “birthday-party.mp4” but you forgot the path where it was saved:
$ sudo find / -type f -name "birthday-party.mp4"
If you don’t remember file name, try searching using a file extension such as *.png or *.mp4:
$ sudo find / -type f -name "*.mp4"
OR
$ sudo find / -type f -iname "*.mp4"

Understanding find command options

  1. -type f : Only search for files.
  2. -type d : Only search for directories or folders.
  3. -name "file" : File to search. It is a base of file name (the path with the leading directories removed) matches shell pattern pattern.
  4. -iname "file" : Same as -name except file names are not case sensitive. For example, ‘backup.txt’ will match BACKUP.txt, backup.txt, BackUp.Txt and so on.

Using locate command to find a file on my system

You can find files by name using the locate command. The syntax is:
locate resume.pdf
locate updated.txt

To ignore case of file i.e. ignore case distinctions when matching patterns , run:
locate -i "*.txt"
locate -i "*.mp4"

Linux command find a file on my system

If you do not get any output run the updatedb command as root user:
# updatedb
OR
$ sudo updatedb
The updatedb command creates or updates a database used by locate command.

Please note that the updatedb command is usually run daily by cron to update the default database. See previous faq for more info.