Searching for files helps people quickly find important information they need for modification. On Linux, the Find and Locate commands are powerful tools that assist users in efficiently managing and searching the file system.
Both of these commands are used to navigate files based on criteria including file type, time frame, or size of the file and folder.
The main objective of this educational post is to empower you to use the Find and Locate commands to easily search for files on the Linux system.
How to Use Find and Locate to Search for Files on Linux?
The following two methods are the most common ways to navigate files and directories on a Linux system.
How to Use Find to Search Files on Linux System
With the help of these examples, you will see the use of the Find command to filter or search files on Linux.
Displays the Current Working Directory
If you are looking for specific files and directories, the first step is to locate your present working directory:
pwd |
---|
By executing the command, you will see the absolute path of your current working folder. This information will make it easy for you to search for specific files and folders.
Find All Files and Folders
The find command followed by path and expression, will show all files and directories inside the ~/Documents:
find ~/Documents -name "*" |
---|
Search for All Text Files
Use the *.txt expression with the find command to particularly target text files within the specified folder, such as ~/Documents:
find ~/Documents -name "*.txt" |
---|
After running the command, you will see the existing file with the txt extension with the ~/Document directory.
Search Files Modified Within a Particular Time
If you are searching for files and folders modified within a specific time frame, such as the last seven days, execute the find command with the following expression:
find ~/Documents -mtime -7 |
---|
Note: To search for files modified more than seven days, add -mtime +7 with the command.
Search Files and Directories by Size
The find command also empowers you to search files and folders by their size. The following example will view the files/folders that are up to two megabytes (2MBs) in size:
find ~/Documents -size -2M |
---|
The files and directories smaller than 2MBs will be displayed on your terminal screen.
Search Files by Size
You can further refine your search to display only files within ~/Documents by adding -type f to the command:
find ~/Documents -size -2M -type f |
---|
This command will exclude the directories and will display the files only.
Search Files by Case-Insensitive
To ignore case sensitivity while searching for files, use the -iname flag with the command:
find ~/Documents -iname "file*.csv" |
---|
Search Multiple Types of Files
You can search for multiple files using the -o flag with the command. This will show files with both .txt and .doc extensions within the ~/Documents directory:
sudo find ~/Documents -name "*.txt" -o -name "*.doc" |
---|
The command will display files that match the .txt or/and .doc extensions.
How to Use Locate to Search Files on Linux
The following examples will illustrate the use of the Locate package to search files on Linux.
Install locate Package via APT
The locate package is not installed by default in Linux systems but can be installed using the APT package manager. To install the locate complete package, execute the command in your terminal:
sudo apt install plocate -y |
---|
If no errors were encountered during the installation, the package has been installed on your Linux system.
Search for Files in the System
Run the command to locate all files with a PDF extension across the entire system:
sudo locate *.pdf |
---|
If the PDF files exist, the command will display them on your terminal screen.
Search for Files in the Specific Location
Search for files with the .csv extension specifically within the ~/Documents directory:
sudo locate -i ~/Documents/*.csv |
---|
The -i flag ignores case sensitivity while searching for files.
Conclusion
In Linux, the Find and Locate commands are the most common ways to navigate files and folders within the system. These commands empower users to specify various criteria while searching for files.
This educational post provided you with an insight into how to use Find and Locate to search for files on a Linux system.
Leave feedback about this