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

last updated in Categories , , , , , , , ,

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.

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

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Notable Replies

  1. You can use locate command to find any file
    Example: locate httpd

Continue the discussion www.nixcraft.com

2 more replies

Participants

Historical Comment Archive

5 comment

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

  2. 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 ???

    Have a question? Post it on our forum!