This article is part of the Linux command series and explains various examples of find commands in Linux to find files and directories.
Although most file managers bring built-in search features, the find
command is one of the best terminal tools for casual users to system administrators. Using its wide range of parameters, you can make it useful for your workflow. It helps you to search for files and directories/folders based on permission, modify date, creation date, size and more. You can also use find with other terminal commands.
In this article, I will walk you through some of the basics of the find command.
Table of Contents
find Command Syntax
The following is the basic syntax of the find command:
find (options) (path) (expression)
- The
options
used for mentioning the symbolic links, debugging options, and optimization methods. - The
path
switch defines the starting directory or directories where find will search the files. - The
expression
helps you to specify options, search patterns, and actions separated by operators.
To search for files in a directory, the user invoking the find command needs to have read permissions on that directory.
Let’s take a look at the following example:
find /home -name ".txt"
- The search happens at the path /home
- The -name option tells to search on the file names with matching .txt
Find Files by Name using command line
The most popular use case of find is searching a file with its name. To find a file with its name, use the -name
option with the file name or with a wildcard pattern.
The following command searches for a file with the name my_doc.txt at /home/debugpoint directory
find /home/debugpoint -type f -name my_doc.txt
If you want a case-sensitive search, use the iname
option.
find /home/debugpoint -type f -iname my_doc.txt
Find Files by Extension
It’s sometimes easier to find files with a pattern. To do that, you can use the same above command but with the wild card * character. Remember to enclose the pattern with a single quote.
For example, the following command search for all files whose extension is txt.
find /home/debugpoint -type f -name '*.txt'
Interestingly, you can also use the -not operator in the above command to find out all the files except those ending with .txt using the below command.
find /home/debugpoint -type f -not -name ‘*.txt’
Find Files by Type
The type option that I used in the above command with descriptor -f is for searching regular file types. In Linux, everything is technically files. So, there are other following options which you can use to search for other types of files.
Here’s a quick list.
f: a regular file
d: directory
l: symbolic link
c: character devices
b: block devices
p: named pipes (FIFO)
s: socket
For example, if you want to find all the directories use the descriptor d with -type. The following command lists all the directories in the path /home/debugpoint.
find /home/debugpoint -type d
Find Files by Size
To find files based on the file size, pass the -size parameter along with the size criteria. You can use the following suffixes to specify the file size:
To search for files based on their size, use the -size
parameter along with size criteria. You can specify the size using the following suffixes:
b: 512-byte blocks (default) c: bytes w: two-byte words k: Kilobytes M: Megabytes G: Gigabytes
The following command searches all files of precisely 1 MB inside the /home directory:
find /home -type f -size 1M
The find
command allows you to search for files based on their size. In the example below, we search for all less than 1MB files in the current working directory. Notice the -
symbol before the size value:
find . -type f -size -1M
To find all the files more than 1MB, use the + sign.
find . -type f -size +1M
You can adjust the size and unit to suit your needs.
Additionally, you can also search for a file within a range of sizes. The following command searches all files with more than 50 MB and less than 100 MB.
find . -type f -size +50M -size 100M
Find Files by Modification Date
The find command also helps you to find files based on their modification or changed time. The following command searches for files in your download directory which were modified 60 or more days ago.
find /home/Downloads -mtime +60 -daystart
You can also use the same method for “less than” conditions. For instance, the following command searches for files modified in the last 30 days.
find /home/Downloads -mtime -30 -daystart
Find Files by Permissions
To use the find
command in Linux to find files based on their permission, you can use the -perm
option. This option allows you to specify the permission mode that you want to search for. For example, to find all files in the current directory that have read and write permission for the owner, you could use the following command:
find . -perm -600
You can also use the -perm
option to search for files with specific permission for the owner, group, and other users. For example, to find all files in the current directory that have read, write, and execute permission for the owner and read and execute permission for the group and other users, you could use the following command:
find . -perm -755
You can also use the -perm
option in combination with other options to search for files based on multiple criteria. For example, to find all files in the current directory that have read and write permission for the owner and are larger than 100KB in size, you could use the following command:
find . -perm -600 -size +100k
Find Files by Owner
The find command also has the option to find files which you own or any other user account. For instance, the following command lists the files which are owned by Linux user debugpoint.
find / -user debugpoint
Find and Delete Files
The find command has a -delete attribute, which lets you delete the files matching the search criteria directly. For example, if I want to delete all the .ISO files in the /home/Downloads directory, then the following command would do that for me.
find /home/Downloads -name *.iso -delete
Remember to add the -delete option at the end of the command. So the find command first finds the files and delete them. If you give the -delete at the beginning, then find will delete the files first.
Use this -delete operator with extreme caution because it is recursive from the directory you mentioned. However, find can only delete the empty directories.
Conclusion
There are many other options that you can use with the find
command to search for files based on different criteria. For more information, you can refer to the find
command manual by running the following command:
man find
Finally, I discussed some of the basics of find commands to find files in Linux, including directories. You can combine the above options to get a more robust find option.
Do let me know in the comment box if you have any questions.
This article is part of the Linux command learning series.