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"
$ sudo find / -type f -iname "*.mp4"
Understanding find command options
- -type f : Only search for files.
- -type d : Only search for directories or folders.
- -name "file" : File to search. It is a base of file name (the path with the leading directories removed) matches shell pattern pattern.
- -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:
To ignore case of file i.e. ignore case distinctions when matching patterns , run:
locate -i "*.txt"
locate -i "*.mp4"
If you do not get any output run the updatedb command as root user:
$ 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.
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: