The find Command
The find
command searches for files and directories based on a variety of criteria.
Syntax
The syntax for the find command is as follows:
find [options] <path> <expression>
The <path> argument is the directory that you want to search. The <expression> argument is a logical expression that is used to match the files and directories that you want to find.
Examples
Here are some examples of how to use the find command:
Find all files in the current directory that have the .txt
extension.
find . -name "*.txt"
Find all directories in the current directory that are empty:
find . -type d -empty
Find all files in the current directory that were modified within the last 24 hours:
find . -mtime -1
Find all files in the current directory that are larger than 1MB:
find . -size +1M
Options
The find
command has a large number of options that can be used to modify its behavior. Some of the most common options are:
-name <pattern>: Match files and directories whose names match the specified pattern.
-type <type>: Match files and directories of the specified type. The valid types are f (file), d (directory), l (symbolic link), and s (socket).
-mtime <n>: Match files and directories that were modified within the last n days.
-size <n>: Match files and directories that are larger than or smaller than the specified size in bytes.
-exec <command> {} ;: Execute the specified command on each file or directory that is matched by the expression. The {} placeholder represents the file or directory that was matched.
For more information on the find
command and its options, please see the man page for find
.
Additional Tips
You can use the -o (or) and -a (and) operators to combine expressions. For example, to find all files in the current directory that have the .txt extension and are larger than 1MB, you would use the following command:
find . -name "\*.txt" -size +1M
You can use the -exec option to execute arbitrary commands on the files and directories that are matched by the expression. This makes the find command very powerful and versatile.
You can use the -print option to print the names of the files and directories that are matched by the expression to the standard output. This is useful for debugging purposes.
With gencmd
find
is a powerful command with the right options. Figuring out and remembering the options, though, is tough. gencmd
can be particularly useful with find
.
gencmd find txt files that contain hello
- find . -type f -name “*.txt” -exec grep -i “hello” {} ;
- find . -name “*.txt” -exec grep -i hello {} ;
- find . -type f -name “*.txt” -exec grep -i hello {} ;
- find . -type f -name “*.txt” | xargs grep -i hello
gencmd find files greater than 1MB that was created in the past 3 days
- find /path/to/search -type f -size +1M -mtime -3
- find . -type f -size +1M -mtime -3