Easy Tutorial
❮ Linux Comm Eject Linux Comm Poweroff ❯

Linux find Command

Linux Command Manual

The Linux find command is used to search for files within a specified directory. Any string preceding the parameters is considered the directory name to search in. If the command is used without any parameters, find will search for subdirectories and files in the current directory and display all found subdirectories and files.

Syntax

find path -option [-print] [-exec -ok command] {} \;

Parameter Description:

find interprets path and expression based on the following rules: the part before the first - ( ), ! on the command line is the path, and the part after is the expression. If path is an empty string, the current path is used; if expression is an empty string, -print is used as the default expression.

There are dozens of options available in the expression, here we only introduce the most commonly used ones.

-mount, -xdev: Only check files on the same filesystem as the specified directory, avoiding listing files from other filesystems.

-amin n: Accessed within the last n minutes.

-anewer file: Files accessed more recently than file.

-atime n: Accessed within the last n days.

-cmin n: Modified within the last n minutes.

-cnewer file: Files modified more recently than file.

-ctime n: Created within the last n days.

-mtime n: Modified within the last n days.

-empty: Empty files.

-gid n or -group name: gid is n or group name is name.

-ipath p, -path p: Files with a path matching p, ipath ignores case.

-name name, -iname name: Files with a name matching name, iname ignores case.

-size n: File size is n units, b represents 512-byte blocks, c represents characters, k represents kilobytes, w is two bytes.

-type c: Files of type c.

d: Directory

c: Character device file

b: Block device file

p: Named pipe

f: Regular file

l: Symbolic link

s: Socket

-pid n: Files with process id n.

You can use ( ) to separate expressions and use the following operations.

exp1 -and exp2

! expr

-not expr

exp1 -or exp2

exp1, exp2

Examples

List all files with the .c extension in the current directory and its subdirectories:

# find . -name "*.c"

List all files in the current directory and its subdirectories:

# find . -type f

List all files updated within the last 20 days in the current directory and its subdirectories:

# find . -ctime 20

Find regular files in the /var/log directory that were modified more than 7 days ago and ask before deleting them:

# find /var/log -type f -mtime +7 -ok rm {} \;

Find files in the current directory where the owner has read and write permissions and the group and others have read permissions:

# find . -type f -perm 644 -exec ls -l {} \;

Find all zero-length regular files on the system and list their full paths:

# find / -type f -size 0 -exec ls -l {} \;

Linux Command Manual

❮ Linux Comm Eject Linux Comm Poweroff ❯