Visits: 185
The find
command in Linux is a powerful tool that helps you search for files and directories in your file system based on various criteria like name, size, type, modification date, and more. In this guide, we'll walk you through the basics of using find
with simple examples and explanations.
Introduction to find
The find
command allows you to search for files and directories in a directory hierarchy. It's especially useful when you need to locate files that meet specific criteria or perform actions on those files.
The basic syntax of the find
command is as follows:
find [path] [expression]
find
will search in the current directory by default.To find files by name, use the -name
option followed by the name of the file you are looking for. For example, to find a file named example.txt
, you would use:
find /path/to/search -name "example.txt"
To find files by extension, use a wildcard *
. For example, to find all .txt
files, you would use:
find /path/to/search -name "*.txt"
To find files based on their size, use the -size
option. Sizes can be specified in bytes (c), kilobytes (k), megabytes (M), and gigabytes (G). For example, to find files larger than 1 MB, you would use:
find /path/to/search -size +1M
To find files modified within a certain number of days, use the -mtime
option. For example, to find files modified in the last 7 days, you would use:
find /path/to/search -mtime -7
To find empty files, use the -empty
option. For example:
find /path/to/search -empty
You can combine multiple criteria using logical operators like -and
, -or
, and -not
. For example, to find all .txt
files larger than 1 MB, you would use:
find /path/to/search -name "*.txt" -and -size +1M
The -exec
option allows you to execute a command on each file found. For example, to delete all .tmp
files, you would use:
find /path/to/search -name "*.tmp" -exec rm {} \;
The {}
placeholder represents each file found by find
, and \;
indicates the end of the command to be executed.
The find
command is a versatile tool that can help you locate files and directories based on various criteria. By mastering the basics covered in this guide, you'll be able to perform efficient searches and manage your files more effectively in Linux.
For more advanced uses and options, refer to the find
man page by typing man find
in your terminal.