Finding files and directories in Linux is a very difficult task compared to Windows operating system. Especially if you are working on the server operating system without a Desktop interface. There are several ways to search files and directories in Linux. The simple and easiest way is to use the Linux terminal to search or locate files.
The find and locate are the most popular command-line tool used to search files and directories in Linux. The find command allows you to search for files and directories based on an expression. This way you can search files and directories based on their size, date, type, and ownership.
In this post, we will show you how to search files on the Linux terminal using the find and locate commands.
Prerequisites
- A Linux VPS with root access enabled, or a user with sudo privileges.
Log in and Update Packages
First, we’re going to need to log into our server using SSH. You can do that by entering this command:
ssh root@IP_Address -p Port_Number
Remember to replace root
with your username if you are not using the root user. Change IP_Address
and Port_Number
according to your server’s IP address and SSH port number.
Once you are logged in, you should update all of your packages to their latest available versions.
apt-get update -y apt-get upgrade -y
Once all the packages are up-to-date, restart your server to apply the configuration changes.
Search Files by Name
The find command allows you to search a specific file by its name. You can use the find command with -name option followed by the file name that you want to search.
For example, to search a file named file1.txt
in the /etc
directory, run the following command:
find /etc -type f -name file1.txt
If you want to ignore the case during the file search, use the -i
option as shown below:
find /etc -type f -iname file1.txt
You can use the following option if you want to search for a specific file type:
- f – regular file
- d – directory
- l – symbolic link
- c – character devices
- b – block devices
Search Files by Extension
You can use the asterisk *
symbol before any extension to find all files that end with specific extensions including, .txt, .png, .pdf, .mp4, etc.
For example, to search all files with extensions .png
in the /opt
directory, run the following command:
find /opt -type f -name '*.png'
This command will search all PNG
files located inside /opt
directory.
Search Files by Size
You can search a file based on their size using the -size
option followed by size criteria.
For example, to search all files less than 2 MB inside /home
directory, run the following command:
find /home -type f -size -2M
You can use the +
symbol before the size if you want to search files greater than 2 MB
:
find /home -type f +size -2M
You can also specify the size range during the file search.
For example, to search all files between 2 MB
and 10 MB
, run the following command:
find /home -type f -size +2M -size 10M
Use the following options to specify the size in KB, MB, GB, and more.
- c – bytes
- k – Kilobytes
- M – Megabytes
- G – Gigabytes
- b – 512-byte
Search Files by Modification Time
The find command can search a file based on its modification, access, and change time.
For example, to search PHP files inside /home
directory that have been modified in the last 10
days, run the following command:
find /home -name "*.php" -mtime 10
You can use the + symbol if you want to find all files that have been modified more than 10 days.
find /home -name "*.php" -mtime +10
Search Files by Permissions
You can use the find command with the -perm
option to search a file based on their permissions.
For example, to search all files inside the /etc
directory with permissions of exactly 764
, run the following command:
find /etc -perm 764
Search Files Using the Locate Command
The locate is another command-line tool that allows you to search for a file in Linux. The locate command is much faster than other tools because it uses a database file to perform the search instead of searching your local hard disks.
For example, to search a file named rc.local
, run the following command:
locate rc.local
If you want to count the number of search keywords is matched, use the -c
option.
locate -c rc.local
Conclusion
In the above post, we explained how to search for a file using the find and locate command in Linux. I hope you have now enough understanding of how to search a file in your Linux system using the terminal.
PS. If you liked this post, on how to search files on the Linux terminal, please share it with your friends on the social networks using the buttons on the left or simply leave a reply below. Thanks.
Hey Linux Host Support!.
Just a small suggestion. Under the “Search Files by Name” section there are two commands:
find /etc -type -f -name file1.txt
find /etc -type -f -iname file1.txt
There’s a typo, they should not have the hyphen “-” before the “f”:
find /etc -type f -name file1.txt
find /etc -type f -iname file1.txt
Thanks for the article, I always come back here from time to time :).
Hi Hugo,
Many thanks, this is corrected now.
Great Article, Useful and Important Information. Thanks for this.
Great and useful content. Thank you!
Really helpful.
Magnificently done. Executed to perfection and will help people in need for sure. Thank you and god bless.