More often than not, files do get misplaced on your computer. For finding them, you will need to use some sort of tool and a bunch of rules. In today’s Terminal Tuts series, we’re going to explain how to find files on your Linux systems, by knowing any sort of information about the file and by using the terminal.
The utility provided by GNU in Linux for finding files is the find command. It’s much faster, efficient and precise than any manually installed program, or graphical program. In this article, we are going to assume that the readers are familiar with cd and ls commands.
Let’s start with a few simple commands and parameters first where we will look for files on the basis of its name.
find -name abc
Since we are searching on the basis of name, notice the usage of the -name parameter. In the example, abc is the name of the file you are searching. The search will run in the working directory of your terminal session. Here are some important properties of the -name parameter:
- The name has to be exact. It won’t search files with the given argument as a part of the name.
- It is case sensitive.
- It searches for both files and directories by the name that is provided.
Some modified usages of -name parameter:
Using -iname instead of -name makes the search case insensitive. It means that the search will not focus on whether the filenames are uppercase or lowercase.
Searching using a keyword
To search for a file not by using its exact name but a keyword is simple too. All you have to do is modify the name of the file that you use for the -name attribute. The syntax is as follows:
find -name "*test*"
NOTE: The double quotes are not a mistake. If you don’t put in the quotes while searching for files with a keyword, you will get a syntax error.
In the CLI, ‘*’ means everything. It displays any file names that have anything at all before the test, or after it. Additionally, you can use the -iname parameter instead of -name to make the search even broader.
You can use the -name parameter to find files with a specific extension. For example, if you want to list all Python scripts, you can enter this code:
find -name "*.py"
Because all Python scripts use the .py extension.
Next up, the -type parameter, which stands for the type of the file. The find command provides a number of options for the -type parameter but for most users, only two come in handy. They are the d and f values. The d value means that the user wants only directories listed, and f means that the user wants only the files (no directories) listed. Here’s an example:
Other options available are:
- c : Character (unbuffered) special
- p : Named pipe
- l : Symbolic link
- s : Socket
- d : Door (Solaris)
The find command can also find files based on their size. It uses various options for various size units. Some are:
- c : For bytes.
- k : For KBs
- M : For MBs
- G : For GBs
In the syntax, you have to put a number in front of the symbol that you’re using. For example, if you want to denote 50 MBs, then you have to write 50M.
You also have to put a ‘+’ or ‘-‘ before the number of units. If you want to search for files more than the quantity use ‘+’. If its less than the specified size, use ‘-‘.
Here’s an example where I’m searching for a file more than 400MB. I have named the file 465 because that’s its size.
Range of size
Since you can mention a more than and less than the limit, you can also combine the two to search for files in a certain size range. This requires two -size attributes. For example, if I have to search for files more than 400MB but less than 500MB in size, I can use this:
find -size +400M -size -500M
Here’s an example:
Setting a location
To search for files in a directory, navigating to the directory is not necessary. You can also specify the location of a directory if that’s the only place where you want to search the files. The location has to be written right after the find. So if you want to mention the location, you have to do so before any parameters or values.
For example here, I search in the Downloads directory:
find Downloads/ -name test
Similarly, if you want to search for a file in the whole system, just use / as the location. You will need root access or superuser permissions, hence use sudo before the command. Remember that it will take a bit of time as it is scanning the entire system. Example command:
sudo find / -name abc
That’s all about the significant and most useful options of the find command on Linux systems. All these options may seem a bit difficult to remember at first, but as you start using it on a regular basis, and you’ll be using the Terminal method rather than the GUI method most often! We hope this article helped.
Any queries are welcome in the comment section. Cheers!