How to Find Files in Linux?
The find command in Linux is powerful tool that allows to quickly and easily scan through the file system to find files and directories. In this article, we will discuss how to find files in Latex.
In this article, we will be learning how to use the find command from within a Linux terminal. The find command is a powerful tool that allows us to quickly and easily scan through our file system to find files and directories that meet certain criteria. We can even perform actions on those results that we get back.
So let’s go ahead and look at some examples. So right now, I’m currently in a test directory where I’ve created some sample files and directories as shown below:
Now if we wanted to find all the files and directories starting in this current directory then we can just use the below command:
find .
In the above command, the dot signifies the current directory. Now if we run this, then we can see that we get all of the files and directories below the current directory that we searched:
Find All Files Within a Directory
To find all the files within a specific directory, we can make use of the following syntax.
Syntax
find <directory_name>
Here now if we wanted to find all of the files and directories within a specific directory then we can just replace that dot with a directory name. So if we wanted to find all the files and folders within the test1 directory then we could just use the below command:
find test1
Now if we run that, then we can see that it returned all of the directories and files within that test1 directory.
Best-suited Linux courses for you
Learn Linux with these high-rated online courses
Find files by Type:
The following syntaxes can be used to find files, directories, symbolic links, character devices, and block devices while using the find command based on their type.
Syntax:
find <path> -type [d][f][l][c][b]
Here,
- d refers to only directory types.
- f refers to only file types.
- l refers to only symbolic link types.
- c refers to only character device types.
- b refers to only block device types.
Now let’s start filtering out some of the results. So let’s say that we wanted to find only the directories and exclude the files. Now to do that we can simply use the below command:
find . -type d
This command will find all the directories and no files. As we can see in the below image, we got a result of all the directories below our current directory.
Now if we wanted to find all of the files and no directories then we can just replace that d with an f as shown below:
find . -type f
Now if we run this command, then you’ll see that it returns all of the files and no directories.
Find Files by Name:
To find files using their name, we make use of the -name and -iname options as shown below:
- Syntax: For case-sensitive name search.
find <path> -name "file_name"
- Syntax: For case insensitive name search.
find <path> -iname "file_name"
Now let’s say that we wanted to find a file with a specific name. As we know that there’s a file in our directory somewhere called test_3.py, but suppose we can’t remember exactly where that is. So let’s go ahead and search within the current directory for this file using the below command:
find . -name test_3.py
The above command will search in all the subdirectories as well. So you can see when we search for that exact filename that it returned this one result within this test1 directory:
Sometimes you don’t know the entire file name exactly, what if we knew the file started with the word test but we couldn’t remember the rest of it? So if we wanted to find all the files that started with the word test, we can make use of an asterisk(*) as a wild card. So instead of searching for the specific file name here, we are just going to mention test and then that asterisk there to do a wild-card as shown below:
find . -name "test*"
Now if we run that, we can see that it returns all of the files that start with the word test.
Now there are some files that start with the word test that it didn’t return and that’s because some of them have capital letters and the – name option is case sensitive. Now if we want it to be case insensitive then instead of using the -name option we can use the -iname option.
So if we replace the -name option with -iname, we can see that all case-insensitive results are also shown.
find . -iname "test"
Output:
Find Files by Extensions:
The asterisk(*) can also be used to find files with certain extensions. To use it for the purpose of finding files with certain extensions, use the below syntax.
Syntax:
find . -name "*.extension"
So let’s say that we wanted to find all Python files from our current directory then we could just use the below command:
find . -name "*.py"
It did return the Python files within some of these subdirectories.
Find File by Metadata:
We can also use the file metadata to find specific files. For that, we make use of the following syntax.
Syntax:
find <path> f -[mmin][mtime][amin][atime][cmin][ctime] value
Here,
- mmin refers to modified minutes of the files.
- mtime refers to modified days of the files.
- amin refers to access minutes of the files.
- atime refers to the access days of the file.
- cmin refers to changed minutes of the files.
- ctime refers to the changed days of the file.
Using the above syntax, we can also filter files and directories based on their metadata and that can be extremely useful. So let’s say that we wanted to find all the files modified in the last ten minutes. So we can use the below command for the same:
find . -type f -mmin -10
The –mmin – 10 signifies the last 10 minutes. For this we will create a file named “new_file.txt” using the below command:
touch "new_file.txt" touch "new_file2.txt"
Now let’s run the above find command:
When we use this minus sign, it’s saying to find files that were modified less than 10 minutes ago and we could use the plus sign to find files that were modified more than 10 minutes ago. The command goes as shown below:
find . -type f -mmin +10
Here we can see that it returns a lot more files here because all those were modified more than 10 minutes ago.
Find File by Size:
So another useful option is to be able to search by file size. To find files based on their size we can make use of the following syntax.
Syntax:
find <path> -type f <file_size>[c][k][M][G][b]
Here,
- c is used to indicate file size in bytes.
- k is used to indicate file size in Kilobytes.
- M is used to indicate file size in Megabytes.
- G is used to indicate file size in Gigabytes.
- b is used to indicate file size in 512-byte blocks.
So let’s say that we had some files eating up our disk space and we didn’t know exactly where those were. Here we could run a search and try to find all the files over a certain size. So let’s say that we wanted to find all the files that are over 5 bytes. So in order to do that we are going to find everything under our current directory and then there is going to be the size option and we want to find over 5 bytes. The command would look like the below:
find . -size +5c
So, if we run that we can see that, it returns the results with files with a size greater than 5 bytes:
Conclusion:
So far we have covered the following aspect of the find command in Linux:
- Using the find command in Linux.
- Finding files within a directory in Linux.
- Finding files by Type in Linux.
- Finding files by Name in Linux.
- Finding files by Extensions in Linux.
- Finding files by Metadata in Linux.
- Finding files by Size in Linux.
This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio