Linux is well known for its command-line operations to perform various tasks, ranging from basic file management to complex-level network administration. The major file management tasks include creating, moving, finding, etc.
When there are thousands of files stored in your system, at that time, the manual search is quite a time-wasting to carry out. Here comes Linux with its finding files operations. It offers multiple commands to find files by name using the command line.
In this post, we will list some commands alongside their practical demonstration to find a file by name. All the commands are tested on Ubuntu 22.04 in this guide.
Method 1: Find a File by Name Using the find Command
The find command in Linux searches for files and directories within a specified location based on various criteria. The basic syntax for the find command is as follows:
find [path] [expression]
- [path] specifies the starting directory for the search.
- [expression] specifies the search criteria, including options such as -name, -type, -size, and more.
Let’s practice its usage through examples.
Example 1: Find File By Name
Run the below command from the Ubuntu 22.04 terminal to find a file by name.
find -name test.txt
The above output result can be seen that there is a single test.txt file in the Home/Music folder.
Example 2: Find Multiple Files By Their Names
If you know the file’s absolute path, the below-given command will be used to find a file. Keep in mind, don’t forget to mention the file name in single or double quotes, i.e. ‘test ‘, “test“:
find /home/linuxuser/Music ‘test’
The command found three test files with different extensions, as shown above.
Example 3: Search Files in the Current Directory
The following find command with “.” can also be used to find file(s). The “.”’ will find file(s) in the current path:
find . -name test.txt
As can be seen in the above screenshot, that test.txt file is found in the Home/Music path.
Example 4: Find Files By Their Name With All the Extensions
You can search any file if you don’t know the file extension. The following example is given for your reference:
find -name 'test.*'
The output result in the above screenshot shows three test files with different extensions.
Method 2: Find a File by Name Using the locate Command
The locate command in Linux is used to locate files and directories based on their name quickly. It uses a pre-built database to provide results faster than the find command, which searches the file system in real-time.
Prerequisites: Install locate Command Support
Before running the locate command, you must install the locate package files and dependencies. The locate command is associated with the plocate utility, which can be installed on various Linux distributions using the commands:
sudo apt install plocate -y
For CentOS/RHEL(yum) and Fedora(dnf):
sudo yum install placate sudo dnf install plocate
Now have a look at the syntax of the locate command.
locate [option] pattern
Here, the option refers to any additional flags or options that you can use to modify the command’s behavior. Some common options include:
- -i: Ignore the case when matching patterns.
- -c: Print a count of matching files instead of the file names.
- -l: Limit the number of results returned.
- -n: Set the maximum age (in days) of the database used by locate.
Example 1: Find the File Using the Exact Name
The below locate command will find a file by name and extension.
The screenshot presented above shows that there is a test.txt file in the current path.
Example 2: Find the File Having the Same Names and Different Extensions
If you want to find the file name by name, run the following $locate command from the terminal. In this command, “-b” indicates to search the file by name only.
locate -b test.*
It can be easily seen in the above screenshots that there are multiple files with the “test” names but with different extensions such as “.doc,” “.jpg,” “.txt,” etc.
To verify the files, go to the absolute path of any file with the “cd” command:
And then use the ls command in this directory:
It can be verified, as per the above example, the Music folder has three files with the same name but different extensions.
Linux offers a variety of commands to perform file management tasks efficiently through the command line interface. The find command can search for files and directories within a specified location based on various criteria. The locate command quickly locates files and directories on the system based on their names using a pre-built database. Users can easily find and manage files within their Linux system by understanding and utilizing these commands.
Keep visiting Linux-Genie for more Linux tips.