5 useful Options for Linux Find command

Linux find command is one of the most powerful tools in the Linux system administrators’ arsenal.

The basic syntax for the find command in Linux is:

find [path] [expression]

where path is the starting directory for the find command, and expression is a set of conditions that must be satisfied for a file to be considered a match.

Here are some common options that can be used with the find command:

-name pattern: Matches files and directories whose names match the specified pattern.

-type type: Matches files of the specified type, where type can be f for regular files, d for directories, l for symbolic links, and others.

-size [+|-]size: Matches files whose size is greater than (+) or less than (-) the specified size. The size can be specified in bytes, kilobytes (k), megabytes (M), gigabytes (G), or other units.

-mtime [+|-]days: Matches files whose modification time is greater than (+) or less than (-) the specified number of days.

-exec command {} \;: Executes the specified command on each file that matches the expression. The {} is a placeholder for the file name that find found, and the \; signals the end of the -exec command.

See also: Mastering the Linux Command Line — Your Complete Free Training Guide

 

There are many other options available for the find command, and they can be combined in various ways to perform complex searches and operations on files and directories.

Find all files in the current directory modified more than 7 days ago:

find . -type f -mtime +7

Find all directories in the current directory with names starting with “data”:

find . -type d -name "data*"

Find all files in the current directory with names matching “report*.pdf” or “presentation*.pdf”:

find . -type f \( -name "report*.pdf" -o -name "presentation*.pdf" \)

Find all files in the current directory with a size greater than 1MB:

find . -type f -size +1M

Find all files in the current directory that are owned by the user “john” and group “users”:

find . -type f -user john -group users

OptionDescription
-nameSearch for files with the specified name
-typeSearch for files of a specific type (directory, file, symbolic link, etc.)
-sizeSearch for files with a specific size
-mtimeSearch for files modified within a specific time period
-execExecute a command on each file found
-deleteDelete each file found
-printPrint the file path of each file found
-emptySearch for empty files or directories
-permSearch for files with specific permissions
-userSearch for files owned by a specific user
-groupSearch for files owned by a specific group
-maxdepthSet the maximum depth of directories to search
-mindepthSet the minimum depth of directories to search
-regexSearch for files matching a regular expression
-inameSearch for files with a specified name ignoring case
-notInvert the next expression
-andCombine expressions with a logical “and”
-orCombine expressions with a logical “or”

 

David Cao
David Cao

David is a Cloud & DevOps Enthusiast. He has years of experience as a Linux engineer. He had working experience in AMD, EMC. He likes Linux, Python, bash, and more. He is a technical blogger and a Software Engineer. He enjoys sharing his learning and contributing to open-source.

Articles: 275

Leave a Reply

Your email address will not be published. Required fields are marked *