Linux Command - find

find - Find files or directories under the given directory tree, recursively.

Syntax

1
find [options] [root_path] [expression]

Option

If no paths are given, the current directory is used.

The -H, -L and -P options control the treatment of symbolic links.

Option Description
-H Do not follow symbolic links, except while processing the command line arguments.
-L Follow symbolic links.
-P Never follow symbolic links.

Expression

The expression is made up of options (which affect overall operation rather than the processing of a specific file, and always return true), tests (which return a true or false value), and actions (which have side effects and return a true or false value), all separated by operators. -and is assumed where the operator is omitted.

Tests

Test description
-name pattern Base of file name matches shell pattern pattern.
-type c files of type c.
-size n[cwbkMG] file uses n units of space
-perm -mode File’s permission bits are exactly mode
-atime n File was last accessed n*24 hours ago.
-cmin n File’s status was last changed n minutes ago.
-cnewer file File’s status was last changed more recently than file was modified.
-ctime n File’s status was last changed n*24 hours ago.
-mtime n File’s data was last modified n*24 hours ago.
-empty File is empty and is either a regular file or a directory.

Action

Action description
-print print the full file name on the standard output, followed by a newline
-print0 print the full file name on the standard output, followed by a null character
-delete Delete files;
-exec command Execute command;

Example

find by name pattern

use -name option to find by name. This is the most common use case.

find .jpg files in ~/Pictures directory hierarchy

1
find ~/Pictures -name "*.jpg"

You can also use pattern. Find a file starts with digit

1
find -name "[0-9]*.log"

find by type

use -type option to find by file type.

File type

  • f - regular file
  • d - directory
  • b - block (buffered) special
  • c - character (unbuffered) special
  • p - named pipe (FIFO)
  • l - symbolic link
  • s - socket
  • D - door (Solaris)

find regular file in current directory hierarchy

1
find -type f

find directory in current directory hierarchy

1
find -type d

find files not matching a pattern

use “not” operator to find a list of files not ending with .png file extension

1
find . -type f -not -name "*.png"

find files at certain level

Use -maxdepth levels -mindepth levels to specify the depth. depth 0 means the command line arguments.

1
find . -maxdepth 1 -type f -name "*.png"

find by size

use -size option to find files by size

find .java files with size greater than 2k in current directory hierarchy

1
find -type f -size 2k -name "*.java"

find by permission

There are three variations.

  • -perm mode File’s permission bits are exactly mode (octal or symbolic).
  • -perm -mode All of the permission bits mode are set for the file.
  • -perm /mode Any of the permission bits mode are set for the file.

find files that is readable, writable for owner, but only readable for the group and other users.

1
find . -perm 644

find files that is readable, writable for owner. group and other users doesn’t matter

1
find . -perm 600

find files that can be read by either owner or group

1
find . -perm /440

find by path

use -path option to find file by path.

find .css files in node_modules folder

1
find . -name "*.css" -path "**/node_modules/**"

excluding path

1
find . -name "*.css" -not -path  "**/node_modules/**"

To exclude multiple paths

1
find . -name "*.css" -not -path  "**/node_modules/**" -not -path "**/.deploy_git/**"

find by user

find file owned by root

1
find . -user root 

find by group

find file owned by root group

1
find . -group root 

find files accessed last week in current directory hierarchy

1
find -atime -7

find files modified in 24 hours in current directory hierarchy

1
find -mtime -1

find files with status change last week in current directory hierarchy

1
find -ctime -7

find empty files in current directory hierarchy

1
find -empty

Reference