find

Find files in the specified directory

Supplementary instructions

find command is used to find files in the specified directory. Any string preceding the parameter will be treated as the name of the directory to be searched. If you use this command without setting any parameters, the find command will search for subdirectories and files in the current directory. And all found subdirectories and files will be displayed.

grammar

find(options)(parameters)

Options

-amin<minute>: Search for files or directories that have been accessed at the specified time, the unit is calculated in minutes;
-anewer <reference file or directory>: Find files or directories whose access time is closer to the current access time than the specified file or directory;
-atime<24 hours>: Search for files or directories that have been accessed at the specified time, and the unit is calculated in 24 hours;
-cmin<minutes>: Find files or directories that have been changed at the specified time;
-cnewer <reference file or directory> finds files or directories whose change time is closer to the current time than the change time of the specified file or directory;
-ctime<24 hours>: Search for files or directories that were changed at the specified time, and the unit is calculated in 24 hours;
-daystart: Calculate time starting from today;
-depth: Start searching from the deepest subdirectory under the specified directory;
-empty: Look for files with a file size of 0 Byte, or an empty directory without any subdirectories or files;
-exec<execution command>: Assume that the return value of the find command is True, execute the command;
-false: Set all return values of the find command to False;
-fls<list file>: The effect of this parameter is similar to specifying the "-ls" parameter, but the result will be saved as the specified list file;
-follow: exclude symbolic links;
-fprint<list file>: The effect of this parameter is similar to specifying the "-print" parameter, but the results will be saved in the specified list file;
-fprint0<list file>: The effect of this parameter is similar to specifying the "-print0" parameter, but the result will be saved in the specified list file;
-fprintf<list file><output format>: The effect of this parameter is similar to specifying the "-printf" parameter, but the result will be saved in the specified list file;
-fstype <file system type>: only search for files or directories under this file system type;
-gid<group identification code>: Search for files or directories that match the specified group identification code;
-group<group name>: Search for files or directories that match the specified group name;
-help or --help: online help;
-ilname <template style>: The effect of this parameter is similar to specifying the "-lname" parameter, but the difference in character case is ignored;
-iname <template style>: The effect of this parameter is similar to that of specifying the "-name" parameter, but the difference in character case is ignored;
-inum<inode number>: Find files or directories that match the specified inode number;
-ipath <template style>: The effect of this parameter is similar to that of specifying the "-path" parameter, but the difference in character case is ignored;
-iregex<template style>: The effect of this parameter is similar to that of specifying the "-regexe" parameter, but the difference in character case is ignored;
-links<number of connections>: Find files or directories that match the specified number of hard links;
-lname<template style>: Specify a string as the template style for finding symbolic connections;
-ls: Assuming the return value of the find command is True, list the file or directory names to the standard output;
-maxdepth <directory level>: Set the maximum directory level;
-mindepth <directory level>: Set the minimum directory level;
-mmin<minutes>: Find files or directories that have been changed at the specified time, the unit is calculated in minutes;
-mount: The effect of this parameter is the same as specifying "-xdev";
-mtime<24 hours>: Find files or directories that have been changed at the specified time, the unit is calculated in 24 hours;
-name<template style>: Specify a string as the template style for finding files or directories;
-newer<reference file or directory>: Find files or directories whose change time is closer to the current time than the change time of the specified file or directory;
-nogroup: Find files or directories that do not belong to the local host group identification code;
-noleaf: Do not consider that the directory must have at least two hard links;
-nouser: Find files or directories that do not belong to the local host user ID;
-ok <Execute command>: The effect of this parameter is similar to specifying "-exec", but the user will be asked before executing the command. If the answer is "y" or "Y", the command will be abandoned;
-path <template style>: Specify a string as the template style for searching directories;
-perm<permission value>: Find files or directories that match the specified permission value;
-print: If the return value of the find command is True, list the file or directory name to the standard output. The format is one name for each column, and there is a "./" string before each name;
-print0: If the return value of the find command is True, list the file or directory name to the standard output. The format is that all names are on the same line;
-printf <output format>: If the return value of the find command is True, list the file or directory name to the standard output. The format can be specified by yourself;
-prune: Do not search for strings as a template style for finding files or directories;
-regex<template style>: Specify a string as the template style for finding files or directories;
-size<file size>: Find files that match the specified file size;
-true: Set all return values of the find command to True;
-type<file type>: only search for files that match the specified file type;
-uid<user identification code>: Search for files or directories that match the specified user identification code;
-used<number of days>: Search for files or directories that have been accessed at the specified time after the file or directory was changed, the unit is calculated in days;
-user <owner name>: Find the file or directory with the specified owner name;
-version or --version: display version information;
-xdev: Limit the scope to the preceding file system;
-xtype<file type>: The effect of this parameter is similar to specifying the "-type" parameter, except that it checks for symbolic connections.

Parameters

Starting directory: The starting directory to search for files.

Example

# Search all files in the current directory, the file content contains the content of "140.206.111.111"
find . -type f -name "*" | xargs grep "140.206.111.111"

Match based on files or regular expressions

List all files and folders in the current directory and subdirectories

find.

Find file names ending with .txt in the /home directory

find /home -name "*.txt"

Same as above, but ignore case

find /home -iname "*.txt"

Search all files ending with .txt and .pdf in the current directory and subdirectories

find . \( -name "*.txt" -o -name "*.pdf" \)

or

find . -name "*.txt" -o -name "*.pdf"

Match file path or file

find /usr/ -path "*local*"

Match file paths based on regular expressions

find . -regex ".*\(\.txt\|\.pdf\)$"

Same as above, but ignore case

find . -iregex ".*\(\.txt\|\.pdf\)$"

Negative parameters

Find files in /home that do not end with .txt

find /home ! -name "*.txt"

Search based on file type

find . -type type parameter

Type parameter list:

Depth search based on directory

Maximum depth down is limited to 3

find . -maxdepth 3 -type f

Search for all files whose depth is at least 2 subdirectories from the current directory

find . -mindepth 2 -type f

Search based on file timestamp

find . -type f timestamp

Each file in the UNIX/Linux file system has three timestamps:

Search all files that have been accessed in the last seven days

find . -type f -atime -7

Search all files that were accessed exactly seven days ago

find . -type f -atime 7

Search all files that have been accessed more than seven days ago

find . -type f -atime +7

Search all files accessed for more than 10 minutes

find . -type f -amin +10

Find all files that have been modified longer than file.log

find . -type f -newer file.log

Match based on file size

find . -type f -size file size unit

File size unit:

Search for files larger than 10KB

find . -type f -size +10k

Search for files smaller than 10KB

find . -type f -size -10k

Search for files equal to 10KB

find . -type f -size 10k

Delete matching files

Delete all .txt files in the current directory

find . -type f -name "*.txt" -delete

Match based on file permissions/ownership

Search for files with permission 777 in the current directory

find . -type f -perm 777

Find the php files in the current directory that have permissions other than 644

find . -type f -name "*.php" ! -perm 644

Find all files owned by user tom in the current directory

find . -type f -user tom

Find all files owned by user group sunk in the current directory

find . -type f -group sunk

Use with other commands using the -exec option

Find all root files in the current directory and change ownership to user tom

find .-type f -user root -exec chown tom {} \;

In the above example, {} is used in conjunction with the -exec option to match all files and will then be replaced with the corresponding file name.

Find all the .txt files in your home directory and delete them

find $HOME/. -name "*.txt" -ok rm {} \;

In the above example, -ok behaves the same as -exec, but it will prompt whether to perform the corresponding operation.

Find all .txt files in the current directory and concatenate them into the all.txt file

find . -type f -name "*.txt" -exec cat {} \;> /all.txt

Move the .log files 30 days ago to the old directory

find . -type f -mtime +30 -name "*.log" -exec cp {} old \;

Find all .txt files in the current directory and print them out in the form of "File: file name"

find . -type f -name "*.txt" -exec printf "File: %s\n" {} \;

Because multiple commands cannot be used in the -exec parameter in a single-line command, the following method can be used to accept multiple commands after -exec.

-exec ./text.sh {} \;

Search but skip the specified directory

Find all .txt files in the current directory or subdirectory, but skip the subdirectory sk

find . -path "./sk" -prune -o -name "*.txt" -print
⚠️ ./sk cannot be written as ./sk/, otherwise it will have no effect.

ignore both directories

find . \( -path ./sk -o -path ./st \) -prune -o -name "*.txt" -print
⚠️ If you write a relative path, you must add ./

find other tips collection

To list all zero-length files

find . -empty

Other examples

find ~ -name '*jpg' # Find all jpg files in the main directory. The -name parameter allows you to limit the results to files matching a given pattern.
find ~ -iname '*jpg' # -iname is like -name, but case-insensitive
find ~ ( -iname 'jpeg' -o -iname 'jpg' ) # Some images may have a .jpeg extension. Fortunately, we can combine patterns with "or" (denoted as -o).
find ~ \( -iname '*jpeg' -o -iname '*jpg' \) -type f # What if you have some directories ending with jpg? (Why you would name a directory bucketofjpg instead of pictures is beyond the scope of this article.) We modify our command to use the -type parameter to find the files.
find ~ \( -iname '*jpeg' -o -iname '*jpg' \) -type d # Maybe you want to find those oddly named directories so you can rename them later

Been taking a lot of photos lately, so let's narrow it down to files that were changed last week

find ~ \( -iname '*jpeg' -o -iname '*jpg' \) -type f -mtime -7

You can perform time filtering based on file status change time (ctime), modification time (mtime), or access time (atime). These are in terms of days, so if you want more fine-grained control you can express in terms of minutes (cmin, mmin and amin respectively). Unless you know exactly what time you want, you'll probably end up with a number after + (greater than) or - (less than).

But maybe you don't care about your photos. Maybe you don't have enough disk space, so you want to find all huge (let's define it as "larger than 1GB") files in the log directory:

find /var/log -size +1G

Or maybe you want to find all files owned by bcotton in /data:

find /data -owner bcotton

You can also find files based on permissions. Maybe you want to have files in your home directory that are readable by everyone to make sure you don't overshare.

find ~ -perm -o=r

Delete automatically generated files on mac

find ./ -name '__MACOSX' -depth -exec rm -rf {} \;

Count lines of code

find . -name "*.java"|xargs cat|grep -v ^$|wc -l # Count lines of code, exclude blank lines