- How to search for files, directories, with certain criteria on Linux terminal
- Linux Search Commands to Find Files
- Finding files with name in Linux Terminal
- Type -Linux Command for Finding files or directories
- Finding files with size
- Finding only in a certain location
- Combining all
- How to Search and Find Files Recursively in Linux
- Finding Files Recursively in Linux
- Conclusion
- About the author
- David Adams
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
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.
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
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’ 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.
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 -name *.extension
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 -size
If you want to find files above or below a certain size, here is the format for that.
Format: find / -size (Used to find files above the given size)
find / -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!
It will find all the files within the local file system, which are more than 3000 MB, but lesser than 6000 MB.
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 — 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.
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 -type f -name -size
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.
How to Search and Find Files Recursively in Linux
This brief tutorial explains how to search and find the files recursively in the Linux operating systems.
After reading this article, you will be able to find any file recursively using the different techniques including a single file search, multiple files search, find files by permissions, and more. This document is optimized for both new and experienced Linux users. All methods are valid for every Linux distribution.
All examples in this tutorial contain screenshots to make it easy for any Linux user to understand and reproduce them.
Finding Files Recursively in Linux
The find command does not need flags to search the files recursively in the current directory. You only need to define the main directory and the file name using the –name option. This command will search the file within the main directory and all subdirectories.
The syntax is simple, as shown in the following:
If you want to find the 27.jpg file within your home directory and subdirectories, run the following command as shown in the following screenshot:
As you can see, the file was found in the /home/linuxhint/Downloads/recur subdirectory.
An alternative to the previous command is the tree command shown in the following example where you search the same file named 27.jpg within the current directory:
As you can see in the previous figure, the format is pretty different. It seems to be more user friendly or nicer as long as you don’t need to copy the full path to paste it.
The following screenshot shows how to use the find command to recursively search more than a file.
The syntax to search multiple files recursively is the following:
Note that there is a –o flag before the second file name. You can add more than one file by adding more –o –name flags. For example, to find 4 files with the same command, use the following syntax:
In the practical example described in the following image, use this command to find a file named 27.jpg and a file whose name begins with “DIAGRAM” but without specifying its extension. Instead, use a wildcard (*) to find any file named DIAGRAM independently of its type.
As you can see in the previous image, both files were found recursively.
The next example describes how to find the files by extension using the find command. In the following figure, you can see how to recursively find all the .jpg files using the wildcard again. The syntax is pretty simple:
Thus, to find all the .jpg files recursively, run the following command:
As shown in the previous image, all the jpg files including their path are listed successfully. You can replace the .jpg extension for any extension that you want to search like .png, .txt, .c and more.
Now, let’s assume that you don’t want to find a file but a directory recursively. All you need to do is to use the same command that was shown in the first example of this tutorial then add the -type d option. The syntax as follows:
In the following practical example, use the previous syntax to find the recur directory.
As you see in the previous figure, the directory named “recur” was found successfully.
You also can find the files by size using the following syntax where is the main directory containing the subdirectories and the is the size of the files that you can list with their full path.
The following example describes how to find the 10 MB size files. You can replace the M defining units in MB with c for bytes, w for two two byte words, k for kibytes and G for gibibytes (note units are case sensitive).
To find the 10 mebibytes files, execute the following command:
All 10M files were properly listed with their paths.
The syntax to find the files based on their permissions is shown in the following:
Let’s assume that you want to identify and list the files with read, write, and executing permissions (777). The command to run is the following:
The last example of this tutorial shows how to find and list the files and directories by size.
As shown, the files are listed by size with proper units. The 0 size directories and files are empty.
Conclusion
Linux versatility and flexibility allows to find the files (and other functions) recursively in many ways. They can easily be executed by all the Linux users independently of his knowledge level, from the new users to the system administrators. All techniques previously described are valid for all the Linux distributions and even to some Unix systems. According to their man pages, some flags may vary in some distributions, but most of them are universal. In case your Linux distribution does not match any of the previously explained commands, you can read the man page. It is highly recommended to the readers to practice the examples to incorporate this knowledge.
Thank you very much for reading this Linux tutorial. Keep following us for more Linux professional tips.
About the author
David Adams
David Adams is a System Admin and writer that is focused on open source technologies, security software, and computer systems.