How to search for files, directories, with certain criteria on Linux terminal

While working with Linux, there is always a way to find out your files, lest you forget, where you kept them last time. Searching for files, on GUI is not a big deal, but what about searching files, when you are working on the Terminal, or command line or Linux! There is no doubt, most of the essential tasks on Linux, can easily be carried out with the help of the command line or Terminal on Linux, and searching for a file or directory, is not an exception to that. Yes, you can search like a pro, with the help of the Linux Terminal. But how! Just like other tasks, you just need to know the command, and the format to search for the files on your computer.

The command to search for files and directories on Linux is ‘Find’. Yes, just a synonym of search. But, using the command is not as easy as the name, and you must remember of the format, or the command, or more importantly, the syntax of the command, to ditch GUI, when you are going to search for your files and directories. If you are a Windows user, and also search for files on Linux GUI, you can set certain criteria at the time of searching for the files and directories, to make the process of searching, a lot easier, and efficient. In case of command line searching, as well, you can assign the number of characters in the filename, the format of the file, the size of the file, and a plethora of other options, to make the process of searching, a charm for you.

Let’s not find out how to use the ‘find’ command for searching files and directories on Linux.

 

Linux Search Commands to Find Files

 find a file in Linux via Linux Search commands

 

Finding files with name in Linux Terminal

Let’s first try to use the ‘find’ command to search for files, with a certain name, and for that, you will have to use the following Linux command.

Format: find / -name <filename.ext>

  • Example: find / -name textfile1.txt

This command will start searching for the file with the name ‘textfile1.txt’, on the complete local file system. But, if you are a Windows user, the difference is that, in Linux, the name of the files are case sensitive, and if you use ‘-name’ to find the files, the case should match.

But what to do, if I don’t remember, whether the filename was in the upper case or lower case! Linux also has a solution for you. Just use ‘-iname’, instead of ‘-name’, and the Linux will start searching for the files, ignoring the case.

Format: find / -iname <filename.txt>

  • Example: find / -iname textfile1.txt

Files with the name textfile1.txt will be searched, and the characters within the filename can be either in uppercase or lowercase.

 

Type -Linux Command for Finding files or directories 

Now, you might be stuck with searching for a file, with the certain file type, but don’t know the name of the file. Don’t worry, Linux has a solution for you, as well!

You can find files, normal files, directories, and symbolic links, with Linux. There is just a small command, which you should remember, just like the way, you search for files, with a name.

Format: find / -type <f/d/l/c/b>

Here,

‘f’ refers to normal files,

‘d’ to directories,

‘l’ to symbolic links,

‘b’ to all the block devices, and

‘c’ refers to character devices.

If you are using Linux on a computer, the first two will be enough for you.

  • Example: find / -type c

This file will show the list of all the files, within the local file system.

But that is definitely not something, which you want. You might be searching for a file or directory, of a certain format, and in that case, you should combine ‘type’ and ‘name’ together.

Format: find / -type <f/d/l/c/b> -name *.extension

  • Example: find / -type f -name *.txt

This will start searching for all the files with the extension ‘.txt’, within your local file system. You can also keep the name section first, and the type section after that. It will not result in any change of the output.

 

Finding files with size

Another option is available, just like searching for Linux and Windows. You can even search for files, which are having a particular size. You don’t need to be exact, as you can even search for files, which is within a range. But, how to do that.

Format: find / -size <Lower Range> -size <Upper Range>

If you want to find files above or below a certain size, here is the format for that.

Format: find / -size <+Size> (Used to find files above the given size)

            find / -size <-Size> (Used to find files below the given size)

Here the file size can be in multiple units, where

‘c’ refer to bytes,

‘K’ refer to Kilobytes,

‘M’ to Megabytes,

‘G’ to Gigabytes.

You will hardly need to find files in other units!

  • Example: find / -size +3000M -size -6000M

It will find all the files within the local file system, which are more than 3000 MB, but lesser than 6000 MB.

  • Example: find / -size -100M

It will help you to find files, which are less than 100 MB in size.

 

Finding only in a certain location

Now, you might be knowing, where exactly, a file may be located. In such cases, you can specify the location to make the process of finding, a lot more efficient, and fast. Before moving on to that lets research with the ‘find’ command.

The structure of ‘find’ command goes like this.

find <path> -<criteria> arguments (In the above examples, we used ‘/’ as the path, for a specific reason)

Here, the ‘/’ refers to the complete file system, and if you replace the ‘/’ with the desired URL, you can start searching files only within that location.

  • Example: find /mnt/d/ -size +1000M

It will start searching for all the files in the directory ‘d’ in ‘mnt’, where the file size is more than 1000 MB.

 

Combining all

Now, let’s find a practical scenario, where you want to search for a certain type of file, within the desired location, which lies within a certain size range. You can just combine all the above criteria, in a single ‘find’ Linux command.

Format: find <path> -type f -name <*.extension> -size <arguments>

  • Example: find /mnt/e/ -type f -name *.mp4 -size +100M -size -1000M

It will start searching for all the files with the extension ‘.mp4’ with the directory ‘e’ of ‘mnt’, where the size of the files are less than 1000 MB, but are more than 100 MB.

Now, you will be a master of finding your files on Linux, without facing any troubles. It is not the end though. You might explore a lot of other things related to the ‘find’ command, by typing ‘find -help’, on your Linux terminal. But, I am sure, the information provided here will surely fulfill your needs in most of the cases, for your everyday requirements.

Hope the small information was helpful for you. If you have something more to know, do not forget to let me know about it in the comment section down below.