Basically everything on Linux is a file. But before you can edit the file, you should be able to find it on your system.
About Linux File Search
In this article, I will briefly describe two commands with useful examples for finding files using the terminal. Two teams are find team and find team, The difference between the two commands is that find performs a real-time search, and locate uses an indexed database to search. This means that the locate command is usually faster, but requires that the searched file be in its index database, and this database is usually created at night, so more recent files will not be found with the locate command. I use Debian 10 in my environment. However, the commands should be the same on other Linux distributions.
If you want to search for files by their contents rather than by file name, take a look at the grep command.
Search for files and directories with the find command
Search for a file in the current directory
If you want to find a file using the find command, do one of the following on your terminal.
This will search for the file in the current directory you are working on.
Search for a file in another directory
Now, if you want to find the file in a specific directory. The complete command should look like this:
Suppose you want to find a file named “test.txt” in Documents, the full command should be as follows.
Find files by extension
Now, if you want to find all the text files in your current or specific directory, the corresponding commands should look like this.
Suppose you want to find all text files along the Documents / Karim path, the full command should look like this.
Find files by name
Alternatively, you can use the -name switch if you want to find the file by name.
Suppose you want to find a file called test1.txt in Documents / Karim. A complete team should look like this.
find Documents/Karim -name test1.txt
If you want to find a specific file in the current directory you are working on. Put it down. on the way, as shown in the example.
find . -name test1.txt
Ignore case when searching for files
If you want to find the file and ignore case, use the -iname switch. A complete team should look like this.
find . -iname test.txt
Search for files by file type, for example, symlinks
To find a file of a specific type, use the -type option. A complete team should look like this.
with denotes the type of file, and they are as follows.
b block (buffered) special
with special character (unbuffered)
P named pipe
e regular file
L symbolic link
D door (solaris)
Suppose you want to search for regular files in Documents / Karim, run the following command.
find Documents/Karim -type f
If you want to search for regular files in your current directory. A complete team should look like this.
find . -type f
If you want to search for files with multiple extensions, use c characters separated by commas.
Suppose you want to find all the usual empty files in your current directory.
find . -type f -empty
Suppose you want to find all empty directories in the current directory, use the -d and -empty options in the search command as follows.
find . -type d -empty
Find files by size
If you want to find files of a certain size, you can use the -size option. You can use the following suffix with their exact size.
b: 512 byte block
Suppose you want to find all the files in your current directory that are exactly 50 bytes. You must run the following command.
find . -size 50c
Suppose you want to find all the files in the current directory that are longer than 50 or less than 50 bytes respectively, you need to run one of the following commands.
find . -size +50c
find . -size -50c
Find files by owner (user)
If you want to find a file belonging to a specific user, you can use the -user option. The command syntax should be as follows.
Suppose you want to find a file in your current directory owned by karim. The team should look like this.
find . -user karim
Search for files using the Locate command
The second is the locate command, which you can use to search for files and directories on your system.
First of all, you need to install the locate utility on your Debian 10 computer. Log in as the root user and run the following command on your terminal. Press Y on your keyboard when asked to confirm.
apt-get install locate
Wait for the operation to complete.
Locate is a faster command, and it relies on a file system database. It is updated once a day, but if you want to update it manually, run the following command on your terminal with root privileges.
To find a file using the simple locate command in the current directory, follow these steps on your terminal.
Suppose my file name is test.txt. A complete team should look like this.
You can use the -i option to ignore case-sensitive file names.
locate -i test.txt
Search and search commands are useful when searching for files. It is up to you to decide which team you use most often. They can be expanded with other commands using pipe, wc, sort and grep, etc.
Two commands to easily find files and directories in Debian 10