Basically, everything in Linux is a file. But before you are able to edit a file, you must be able to locate it in your system.
About Linux file search
In this article, I briefly describe two commands in detail with useful examples to search for files with the terminal. The two commands are the find command and the locate command. The difference between the two commands is that find performs a search in real-time and locate uses an indexed database for the search. This means that the locate command is usually faster, but it assumes that the file being searched for is in its index database, and that database is usually created at night, so newer files are not found by the locate command. I am using Debian 11 in my environment. But the commands should be the same on other Linux distributions.
If you want to search for files by their content instead of the file name, have a look at the grep command instead.
Searching for Files and Directories using the find Command
Search file in the current directory
If you want to find a file using the find command, execute one of the following on your terminal.
find <filname>
suppose,
find test.txt
This will search the file in the current directory you are working on.
Search file in another directory
Now, if you want to locate the file in a specific directory. The complete command should look like,
find <path of directory/filename>
Suppose you want to search a file named ‘test.txt’ in Documents, the complete command should be as follows.
find Documents/test.txt
Find files by file extension
Now if you want to find all text files in your current or specific directory, the respective commands should look as follows.
find *.txt
find <path of directory/*.txt>
Suppose you want to search all text files at the path of Documents/Karim, the complete command should look like.
find Documents/Karim/*.txt
Find files by name
Alternatively, you can use -name switch when you want to search a file by name.
find <path> -name <name of file you want to search>
Suppose, you want to search a file named test1.txt at Documents/Karim. The complete command should look like.
find Documents/Karim -name test1.txt
If you want to search a specific file in the current directory you are working on. Put . at the path as shown in the example.
find . -name test1.txt
Ignore case when searching for files
If you want to search a file and want to ignore the case, use -iname switch. The complete command should look as follows.
find . -iname test.txt
Find files by file type, e.g. symlinks
To search for a specific file type, use -type option. The complete command should look like the following.
find <path> -type <c>
- c denotes the type of file and they are following.
- b block (buffered) special
- c character (unbuffered) special
- d directory
- p named pipe
- f regular file
- l symbolic link
- D door (Solaris)
Suppose you want to search regular files at Documents/Karim, execute the following command.
find Documents/Karim -type f
If you want to search for regular files in your current directory. The complete command should look like the following.
find . -type f
If you want to search files with multiple extensions, use the c characters separated by commas.
Let’s say you want to find all the regular empty files in your current directory.
find . -type f -empty
Suppose you want to find all the empty directories in your current directory, use the -d and -empty options in a find command as follows.
find . -type d -empty
Find files by size
If you want to find files with a specific size, you can use the -size parameter. You can use the following suffix with their exact size.
- c: bytes
- k: Kilobytes
- M: Megabytes
- G: Gigabytes
- b: 512 bytes block
Suppose you want to find all files in your current directory that are exactly 50 bytes. You have to execute the following command.
find . -size 50c
Suppose you want to find all files in your current directory that are more than 50 bytes or less than 50 bytes respectively, you have to execute one of the following commands.
find . -size +50c
find . -size -50c
Find files by owner (user)
If you want to search for a file owned by a specific user, you can use the -user option. The syntax of the command should be as follows.
find <path> -user <username>
Suppose you want to search a file in your current directory owned by vitux. The command should look as follows.
find . -user vitux
Finding Files Using a Locate Command
Second is the locate command you can use to search files and directories in your system.
First of all, you have to install the locate utility in your Debian 11 machine. Login with root and execute the following command on your terminal. Press Y from your keyboard when you are asked for confirmation.
apt-get install locate
Wait for an operation to complete.
Locate is a quicker command and it relies on the database of the file system. It is updated once a day but if you want to update it manually, run the following command on your terminal with root privileges.
updatedb
To search a file with the locate command in your current directory, execute the following on your terminal.
locate <filename>
Suppose my file name is test.txt. The complete command should look like the following.
locate test.txt
You can use the -i option to ignore the file name case.
locate -i <filename>
or
locate -i test.txt
Both locate and find commands are helpful in searching the files. It’s up to you which of the command you mostly use. They can be extended with other commands by using pipe, wc, sort and grep, etc.