How to Search Files Using Terminal on Linux
On Linux, performing a file search default using a GUI is not the best way you can find a file.
You can search for files on the GNU/Linux operating system via Terminal by using several tools which is available. Searching files through the Linux Terminal will be faster than using tools GUI because it won’t take up a lot of resources.
There are some tools that you can use to search for files on Linux using the Terminal, including using tools “locate” which searches files by databaseand using the “Find” tool.
In contrast to the “locate” command which is a bit difficult for ordinary users to use, the “find” command can be used easily to search for files.
How to Search Files from the Terminal on Linux Using the Find . Command
Although easier to use, this command also has slower performance compared to the “locate” command. This is because this command actually searches files and directories directly on disc your storage.
Find is suitable for searching files or a directory but you can’t remember the name perfectly.
Because this command can search for files with certain specific criteria such as from user or user groups specific files, previously modified or accessed files, files with specific size ranges, hidden files, files executable, files with access read only, and files with other specified permissions.
The best part is that you can also freely combine these criteria in a single command using this “find” command.
Searching for Files in Specific Directories
First, as a basis, you can search for files using Terminal in Linux in certain specific directories or areas. If you are looking for a file and know where it might be, you can use this method.
In this way, the file search process will be faster because there is no need to scan the entire directory with a large size. But the speed of this search process also depends on the size of the directory.
Open a terminal on your Linux and then enter the directory where the file will probably be by using the “cd” command as follows:
cd /nama_direktori/
If you want to search within your home directory, go to your home directory using the “cd ~” command and if you search the entire system file, use the “cd /” command.
For example, if you want to search for an image file in your “Downloads” directory. You only know that the file has a name with the word “sudoway”, but you don’t know the exact name of the file.
First go to your Downloads directory with the command “cd Downloads” and enter the following command in Terminal to search for it:
sudo find . -name *sudoway*
Then all the files in that directory will appear with the name “sudoway”.
The dot (.) in the command indicates that the “find” command only searches the directory where you are currently.
If you want to find it in the Home directory, replace the flag with tilde (~) and if you want to search the entire directory, replace the sign with a forward slash (/), don’t forget to use “sudo” if you search the entire directory.
Searching for Unknown File Names
Parameter “-name” in the above command previously worked to find files with case sensitive. This means that if you search for a file with the name “sudoway”, you will not find a file with the name “SudoWay”, because parameter it is case sensitive.
To search for a file that you don’t know the name of clearly, you can replace parameter “-name” with parameter “-iname”, as follows:
sudo find . -iname *sudoway*
By using parameter With this, you can perform a search file search without the need to pay attention to sensitive letters. So this can be useful when you don’t know the exact name of the file you are looking for.
Searching for Files With a Specific Size Range
Next you can search for a file with a specific size, for example you are looking for the same image file, the only thing you remember from the file is that the file size is less than 10 MB. You can search for it using the following command:
find . -size -10M
If you know that the file is larger than 4 MB, you can use the “find . -size +4M”. You can also combine parameter this with parameter previously.
Suppose you know that the file is smaller than 10 MB and larger than 4 MB. You can use the following command to search for it:
find . -size -10M -and -size +4M
Finding Recently Accessed or Modified Files
Finally, you can also search for files that you last accessed, you can specify a time range to find a file that you last accessed in a certain time range.
Say you’re looking for a file and the only thing you know is that you last accessed it about 3 minutes ago. To find it, you can use the following command:
find . -amin -3
If the file you’re looking for was last accessed about half an hour ago, you search for it by changing parameter Becomes “-amen -30” and if you accessed it two hours ago you could change parameter it becomes “ -amen -120” and so on.
Meanwhile, if you accessed it two days ago, you can use the following command:
find . -time -2
This will show you all the files you accessed during the last two days in the location of the directory you are looking for. Please adjust the parameter “2” in the command above to the time range you want to find.
Closing
For a more efficient and maximum search process, if you are looking for a file and know some of the criteria from the method above, you can combine these criteria for a more efficient search process.
Please use this method if you think this method is faster and more efficient than doing a search using search default in the GUI, or you can still do a search from the GUI if you think the search process using the GUI is much easier.