Linux: How can I find a file on my system?

I have a remote server located in the data center. I login through SSH command line to manage my Linux server. How do I find a file called xyz on my Linux server system? How do I find a file on my system?

You need use the find command. It search for files in a directory hierarchy under Linux and all other UNIX like operating systems. Use find command to find a file from shell prompt when using the Terminal or ssh based session.

ADVERTISEMENTS

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.

Conclusion

You just learned how to locate a file using locate command. You also learned the how to search the entire hard drive for a file when using Linux with the help of find command. I suggest that you read the man page of find online here or by typing the man command at the shell prompt:
man find

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallCentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig 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 VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
2 comments… add one
  • Hoff123 Feb 11, 2013 @ 19:38

    Awesome! Thank you so much. This will definitely come in handy sometime(and right now…when I googled how to do it…lol) :).

  • none Aug 6, 2013 @ 21:08

    To find a file called zyz in /home/user directory type the command:
    $ find /home/user -name xyz
    you mean to say To find a file called xyz ???

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.