When you are working with the bundles of files in the computer a problem will occur if you put these files somewhere in your system. To find these files GUI provides the best way to search it, however, if you do not remember the file name and just remember the small portion of text written within the file then the only way to search the file is from the terminal of Linux/Ubuntu operating system. This article gives you the proper and authentic information about the searching of files on your Linux/Ubuntu operating system.
How to Find a File Location in Linux/Ubuntu?
Three main commands are used in the Linux/Ubuntu operating system terminal to find the file location.
- Use “find” command to locate the file
- Use “locate” command to locate the files
- Use “grep” command to locate the files
- Use GUI method to search the files
Method 1: Use “find” Command to Locate the File
The “find” is a very powerful command to search for the files and directories in your Linux/Ubuntu operating system. It searches the files on the basis of location, with the specific directory, by using the wildcard, on the basis of permission, date of creation, and date of modification. The simple command to be executed for this purpose is given as.
find path/to/file -iname filename
In the above command
- “find” is used to search the file.
- “path/to/file” is the path of the file where it is located.
- “-iname” is case insensitive, meaning it gives the output if you type a small letter for the capital name file.
- “filename” is the name of the file you are searching for.
The output result of the above command is given below. In this result simply use the “find” command and give the directory of file with “-iname”.
Find files in Specific Directory
To find the file in the specific directory the command to be executed in the Linux/Ubuntu terminal is given as.
find /path/to/directory
In the above command “/path/to/directory” the path to the desired directory. The output result of the above command clearly shows that there are ten files in the downloads directory. The result prints the path with each file and file name with extension.
Find Files in the Current Directory
To search for the files in the current directory, the command to be executed in the Linux/Ubuntu terminal is given as.
find
The below-given result is obtained by the above command. This is a very simple way to watch the files in the current directory.
Find the File with the name in the Current Directory
To search the files with the specific name in the current directory the command to be run the Linux/Ubuntu terminal is given as.
find . -iname filename
The below figure clearly shows that using the “find” command to search for the “Test1.odt” file.
Find Files with Wildcard in Directory
To search for the files with the specific extension the command to be executed is given as
find /path/to/file/ -iname *odt*
In the above command “find” gives the output of all the files with “.odt” extension. The output result for the above command is given below showing that the “.odt” wildcard is searched with the “find” command.
Find the empty files
To search the files which are empty the command to be run in the Linux/Ubuntu terminal is given as.
find /path/to/directory/ -empty
In the above command the “find” command with the “-empty” flag gives the output of the files and directory with no data. The below figure shows the output for the above-mentioned command.
Find files based on the Date
To search the files which have been modified within the last two days the command to be run in the Linux/Ubuntu terminal is given as.
find . -mtime 2
In the above command “-mtime 2” search the files which has been modified within last two days.
To search for the files that have been examined within the last two days, the command to be executed in the terminal is given as.
find . -atime 2
In the above command “-atime 2” search the files which has been examined within the last two days.
To search for the files that have been changed within the last two days, the command to be executed in the terminal is given as.
find . -ctime 2
In the above command “-ctime 2” search the files which has been changed within last two days.
Find files with specific size
Now to find the file with the specific size execute the below given command in the terminal.
find . -size 8k
In the above command “-size” is to search the file with the “8k” size.
Find files based on Permission
To search the files with the specific permission the command to be executed in the terminal is given as.
find /path/to/file -type f -perm 644
In the above command flag “-type” includes the “f” for the files and “-perm” gives the required permission. There are four files in the “My_files” directory with permission 644 as printed in the result below.
Method 2: Use “locate” Command to Locate the Files
The most effective and powerful way to search the files is by using the “locate” command. The “locate” command works on the basis of a database and it is a very fast command. It does not require any path or other condition to search the files because it stores the data on the basis of the process run behind the background of this command. This command is not installed on your operating system therefore install it first. Open the terminal by using the shortcut key “Ctrl+Shift+T” from your keyboard and run the below-given command.
sudo apt install locate
In the above command
- “sdo” command is used to attain the root rights in the Linux terminal.
- “apt”u command is used to access the advanced packages to install and remove files in the Linux terminal.
- “Install locate” is used to install the locate command.
Find a single file by “locate” Command
To search for the single file in your operating system the command to be run in the terminal is given as.
locate -i filename
The below figure shows the output for the above command. “-i” flag with the “locate” command is used to ignore the difference.
Find Multiple files by “locate” Command
Now to search the multiple files execute the below given command in the Linux terminal.
locate -i file1 file2
In the above command “file1” and “file2” are the two files which are going to be searched by locate command. Two files are searched by the “locate” command and the results show the files with their path and destination in the figure below.
Find Files with Specific Wildcard
To search the files with the specific extension the command to be executed in the terminal is given as
locate -i *.py
In the above command “*” force the locate command to give the result for the “.py” file. The output result for the above command is given below showing that the “.py” file is searched with the “find” command.
Method 3: Use “grep” Command to Locate the Files
The “grep” command is not directly used to search the files in your operating system. It is also not used to search the files with file names. But this command is used to show the name of the file that carries the specific text.
Find a File Using “grep” Command
Now find the file that contains the text “Linux-user” in a directory by executing the below-given command in the Linux/Ubuntu terminal.
grep -r -i "Linux-user" /home/tahir/My_files
The below result shows that the file “Testfile.txt” contains the text search by the above command. In the above command “-i” is used to ignore the differences and “-r” is used for the specific directory.
Find Multiple Files Using “grep” Command
Now find the file that contains the text “Linux-user” and “Newuser” in a directory by executing the below-given command in the Linux/Ubuntu terminal.
grep -r -i "Linux-user\|Newuser" /home/tahir/My_files
Use GUI Method to Search the Files
This is the easiest way to search the file by name only. The step involved to search the files by using the GUI method is given as.
Step 1: Form your Linux/Ubuntu opening system click on the “Activities” from the menu bar.
Now the search bar appears on the screen as shown in below figure.
Step 2: Now in the search bar type the name of file you want to search for, the file will appear with its path.
Conclusion
To find the files in your Linux/Ubuntu operating system there are three main commands used such as “find”, “locate”, and “grep”. The “find” command is very important because we can use this command in many different ways but it is slow working due to it requiring utilities. The “locate” command is a powerful and fast-working command because it works on the basis of stored memory and does not require utilities. The “grep” command is not directly used to search the file, it searches the files on the basis of the text they contain.