The find
command is one of the most important and frequently used tools in the Linux command-line. It allows users to search for files and directories based on various criteria such as file name, size, type, and modification time. In this article, we will explore the various options and usage of the find
command in Linux and provide examples to help you understand how to use it effectively.
Basic usage of the find
command:
The basic syntax of the find
command is as follows:
find path -options search_criteria
Where path
is the directory where the search should begin, options
are the options that specify the search criteria, and search_criteria
is the actual search criteria. For example, to search for all files in the /home/user
directory, the command would be:
find /home/user -name *
This command will search for all files in the /home/user
directory and its subdirectories.
Find Command Options:
The find
command in Linux has many options that can be used to search for files and directories. Below is a list of some of the most commonly used options and a brief explanation of what they do:
-name
: This option is used to search for files based on their name. The argument for this option is a pattern to match the file name. Wildcards can also be used.-iname
: This option is similar to-name
but it’s case-insensitive.-size
: This option is used to search for files based on their size. The argument for this option is a number followed by ac
for bytes,k
for kilobytes,M
for megabytes, andG
for gigabytes.-type
: This option is used to search for files based on their type. The argument for this option can bef
for regular files,d
for directories,l
for symbolic links.-mtime
: This option is used to search for files based on their modification time. The argument for this option is a number, where+n
means more than n days ago,-n
means less than n days ago andn
means exactly n days ago.-perm
: This option is used to search for files based on their permissions. The argument for this option is the permission mode in octal format.-user
: This option is used to search for files based on their owner. The argument for this option is the name of the user or the user ID.-group
: This option is used to search for files based on their group owner. The argument for this option is the name of the group or the group ID.-path
: This option is used to search for files based on their path. The argument for this option is a pattern to match the file path.-newer
: This option is used to search for files that have been modified more recently than the specified file. The argument for this option is the name of the reference file.-exec
: This option is used to execute a command on the files that are found. The argument for this option is the command to be executed, followed by;
.-delete
: This option is used to delete the files that are found.-prune
: This option is used to exclude a particular directory and its subdirectories from the search.-print
: This option prints the results of the search.-ok
: This option is similar to-exec
, but it prompts the user for confirmation before executing the command.
These are just a few examples of the many options available with the find
command. It’s important to note that many of these options can be combined to create more complex search criteria, and that more options are available, you can use man find
on your terminal to see the manual of the command and all the available options.
How to find files based on their name:
The -name
option can be used to search for files based on their name. The following command searches for all files with the name test.txt
in the /home/user
directory and its subdirectories:
find /home/user -name test.txt
You can also use wildcards to search for files with similar names. For example, the following command searches for all files with the extension .txt
in the /home/user
directory and its subdirectories:
find /home/user -name *.txt
How to find files based on their size:
The -size
option can be used to search for files based on their size. The following command searches for all files that are exactly 100 bytes in size in the /home/user
directory and its subdirectories:
find /home/user -size 100c
You can also use +
or -
before the size to search for files that are larger or smaller than a certain size. For example, the following command searches for all files that are larger than 1MB in the /home/user
directory and its subdirectories:
find /home/user -size +1M
The argument for this option is a number followed by a c
for bytes, k
for kilobytes, M
for megabytes, and G
for gigabytes.
How to find files based on their type:
The -type
option can be used to search for files based on their type. The following command searches for all directories in the /home/user
directory and its subdirectories:
find /home/user -type d
You can also use f
to search for files, and l
to search for symbolic links. The argument for this option can be f
for regular files, d
for directories, l
for symbolic links.
How to find files by modification date:
The -mtime
option can be used to search for files based on their modification time. The following command searches for all files that were modified within the last 24 hours in the /home/user
directory and its subdirectories:
find /home/user -mtime -1
The -mtime
option accepts a number as an argument, where +n
means more than n days ago, -n
means less than n days ago and n
means exactly n days ago.
How to find files by permissions:
The -perm
option can be used to search for files based on their permissions. The following command searches for all files that have the permission 777
in the /home/user
directory and its subdirectories:
find /home/user -perm 777
You can also use -perm -777
to search for files that have any of the permissions specified in the argument and -perm +777
to search for files that have all of the permissions specified in the argument.
How to find files by owner:
The -user
option can be used to search for files based on their owner. The following command searches for all files that are owned by the user root
in the /home/user
directory and its subdirectories:
find /home/user -user root
How to Find and delete files:
The find
command can also be used to delete files by using the -delete
option. The following command searches for all files that have the .tmp
extension in the /home/user
directory and its subdirectories and deletes them:
find /home/user -name *.tmp -delete
It is important to use this option with caution as deleted files cannot be recovered.
How to find files by extension:
The -name
option can be used to search for files based on their extension. The following command searches for all files that have the .txt
extension in the /home/user
directory and its subdirectories:
find /home/user -name *.txt
It’s important to note that the find
command is case sensitive by default, so you will need to use the -iname
option if you want to make the search case-insensitive.
Conclusion:
The find
command is a powerful and versatile tool that allows you to search for files and directories in a file system. It can be used to search for files based on various criteria such as file name, size, type, and modification time. By understanding the options and usage of the find
command, you can effectively search for and manage files in your Linux system.
We hope that this article has helped you to navigate Linux directories with greater ease and efficiency. If you found it useful, please share with others and leave us your feedback.
If our tutorials helped you, please consider buying us a coffee. We appreciate your support!
Thank you for your support.