How to find files using command-line in the Linux Terminal

Learn how to use the 'find' command to search files and folders on your Linux system.

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.

find command

Let’s start with a few simple commands and parameters first where we will look for files on the basis of its name.

-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.

Simple finding command.
Finding ‘test’.

Some modified usages of -name parameter:

-iname

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.

Find -iname
Ignoring case of the name.

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.

Find keyword
Finding files using a keyword.

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.

File format

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.

Find_format
Finding using a specific file format.

-type

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:

Find_type
Finding using a file type.

Other options available are:

  • c : Character (unbuffered) special
  • p : Named pipe
  • l : Symbolic link
  • s : Socket
  • d : Door (Solaris)

-size

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.

Find_size
Finding using a size detail.

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:

Find_size-range
Finding using a size range.

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

Output:

Find_with-location
Finding in a specific directory.

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

Conclusion

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!

Pulkit Chandak
Pulkit Chandak is a Linux enthusiast and has been using and experimenting with open source software, and hardware too since a long time. He is a huge admirer of open source software and wants to ventilate it to all around him. He is interested in reviewing and writing tutorials on Linux and its innumerable distributions. He believes that freedom in software leads to freedom of the mind from the chains of limits.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

STAY CONNECTED

24,937FansLike
150FollowersFollow

LATEST ARTICLES

RECENT COMMENTS

How to create a bootable CentOS Live USB drive on Windows

create centOS Live USB drive
CentOS ISO downloads are available in two different variations - Minimal ISO and DVD ISO. So what are these? In this guide, you will know how to create a CentOS Live USB drive that can also be used to install CentOS.