Linux is an open-source unix-like operating system based on the Linux kernel. Sometimes a common problem arises for linux users when the users cannot locate a file. We can use the GUI to find the file’s location very easily. But in some cases, we have to refer to the command line.
This write-up will determine how to find a file’s location in Linux
How to find a File’s location in Linux?
We can find the file’s location in Linux using the below-listed methods:
- Using Find Command
- Using Locate Command
- Using Grep Command
Let’s discuss them one by one.
Method 1: Using Find Command to Find a File’s Location in Linux
Find is a very flexible command that searches for files and folders in the local hard drive. It is a very strong but slow tool. Find helps you to locate a command even if you don’t remember the file name.
The syntax of the find command is listed below:
$ find /path/to/folder
Let’s see an example:
It can be seen that all the files in the Downloads folder are displayed.
We can also find a single file inside the directory using the following command:
$ find /Path/to/file filename
For instance:
By looking at the above snapshot we can see that the location of sample3.txt file is displayed.
Let’s search for the files in the current directory using the find command.
To search for files in a current directory, use the below-listed command:
$ find .
Let’s first move within the Downloads directory using the following command:
$ cd Downloads
It can be seen that all the files inside the Downloads folder are shown
Method 2: Using Locate Command To Find a File’s Location in Linux
Another command to locate or search files in Linux is known as the Locate command. It is much more efficient than the find command but it doesn’t provide as many features as the find command. It searches for files and folders in its own database. To work this command properly, the database should be updated on a regular basis.
The syntax of the locate command is listed below:
$ locate -i filename
To use locate, you have to first install it using the following command:
$ sudo apt get-install locate
Provide the password to proceed further.
Locate is installed successfully as shown above
Now we have to update the database using the command:
$ sudo updatedb
The output of the above code is as follows:
Let’s try to find the location of some files using the locate command:
The command displayed the complete location of the Wallpaper2.jpg file.
Now let’s find all the images in our Database
The snapshot shows all the images that exist in our Ubuntu.
Method 3: Using Grep Command To Find a File’s Location in Linux
The basic purpose of the grep command is not to search files but to search the text. It can be used to display the name of the files that include a certain string that matches our query.
The syntax of the grep command is shown below:
$ grep [options] [pattern] [/path/to/file]
Where options refer to general-search controlling activities and pattern means the string that we want to search.
Let’s create a new file first in our system.Use the echo command to create a file.
The syntax is shown below:
$ echo "text" filename
A new file is created as shown above.
Now we can search for the words in the file using the following command:
$ grep "word" filename
Now let’s search for the filenames using the common words.
$ grep -l "word" file*
file* means searching all the files present in your system.
The above command displays the name of the files that contain the common word “Ubuntu”.
Conclusion
We can find a file’s location in Linux using three commands. The find command, the locate command, and the grep command. The find command searches for files and folders in the local hard drive. The locate command searches for the file in its own database whereas the grep command searches for the text in the files present in the system. This guide illustrated how to find a file’s location in Linux.