linux find command to find files and folders [easy to understand]

Posted by rdoylelmt on Mon, 14 Feb 2022 01:50:39 +0100

Hello, I'm architecture Jun, an architect who can write code and recite poetry. Today, let's talk about the linux find command to find files and folders [easy to understand]. I hope it can help you make progress!!!

Search Directory: find / (search range) - name 'search keyword' - type d Search file: find / (search range) - name search keyword - print

Detailed explanation:

The find command is used to find files in the specified directory. Any string that precedes the parameter is treated as the directory name you want to find. If you use this command without setting any parameters, the find command will find subdirectories and files in the current directory. And display all the subdirectories and files found.

grammar

find(option)(parameter)

option

-amin<minute>: Search for files or directories that have been accessed at a specified time, in minutes;
-anewer<Reference documents or directories>: Find the file or directory whose access time is closer to the current file or directory than the access time of the specified file or directory;
-atime<24 Hours>: Search for files or directories that have been accessed at a specified time, in 24 hours;
-cmin<minute>: Find the file or directory that has been changed at the specified time;
-cnewer<Reference documents or directories>Find the file or directory whose change time is closer to the current one than that of the specified file or directory;
-ctime<24 Hours>: Find the file or directory changed at the specified time, in 24 hours;
-daystart: Calculate the time from today;
-depth: Search from the deepest subdirectory under the specified directory;
-expty: Find file size 0 Byte Or an empty directory without any subdirectories or files under the directory;
-exec<Execute instruction>: hypothesis find The return value of the instruction is True,Execute the instruction;
-false: take find The return value of the instruction is set to False;
-fls<List file>: Effect and assignment of this parameter“-ls"The parameters are similar, but the results will be saved as the specified list file; -follow: Exclude symbolic connections; -fprint<List file>: Effect and assignment of this parameter“-print"The parameters are similar, but the results will be saved as the specified list file; -fprint0<List file>: Effect and assignment of this parameter“-print0"The parameters are similar, but the results will be saved as the specified list file; -fprintf<List file><Output format>: Effect and assignment of this parameter“-printf"The parameters are similar, but the results will be saved as the specified list file; -fstype<file system type>: Only find the files or directories under the file system type; -gid<Group ID >: The identification code of the document matching the specified directory or group; -group<group name >: Find files or directories that match the specified group name; -help Or—— help: Online help; -ilname<Template style>: Effect and assignment of this parameter“-lname"Parameters are similar, but the difference of character case is ignored; -iname<Template style>: Effect and assignment of this parameter“-name"Parameters are similar, but the difference of character case is ignored; -inum<inode number>: Find matches specified inode Numbered documents or directories; -ipath<Template style>: Effect and assignment of this parameter“-path"Parameters are similar, but the difference of character case is ignored; -iregex<Template style>: Effect and assignment of this parameter“-regexe"Parameters are similar, but the difference of character case is ignored; -links<Number of connections>: Find files or directories that match the specified number of hard connections; -iname<Template style>: Specify a string as a template style for finding symbolic connections; -ls: hypothesis find The return value of the instruction is Ture,List the file or directory names to the standard output; -maxdepth<Directory hierarchy>: Set the maximum directory level; -mindepth<Directory hierarchy>: Set the minimum directory level; -mmin<minute>: Search for files or directories that have been changed at a specified time, in minutes; -mount: Effect and assignment of this parameter“-xdev"Same; -mtime<24 Hours>: Search for files or directories that have been changed at a specified time, in 24 hours; -name<Template style>: Specify a string as a template style for finding files or directories; -newer<Reference documents or directories>: Find the file or directory whose change time is closer to the current one than that of the specified file or directory; -nogroup: Find out the file or directory that does not belong to the local host group ID; -noleaf: Regardless of the directory, at least two hard connections are required; -nouser: Find out the files or directories that do not belong to the local host user ID; -ok<Execute instruction>: Effect and assignment of this parameter“-exec"Similar, but the user will be asked before executing the instruction. If you answer“ y"Or“ Y",Abandon the execution order; -path<Template style>: Specify the string as the template style for finding the directory; -perm<Permission value>: Find files or directories that meet the specified permission value; -print: hypothesis find The return value of the instruction is Ture,Lists the file or directory names to standard output. The format is a name for each column, and there is a name in front of each name“./"character string; -print0: hypothesis find The return value of the instruction is Ture,Lists the file or directory names to standard output. The format is that all names are on the same line; -printf<Output format>: hypothesis find The return value of the instruction is Ture,Lists the file or directory names to standard output. The format can be specified by yourself; -prune: Do not find strings as template styles for finding files or directories; -regex<Template style>: Specify a string as a template style for finding files or directories; -size<file size>: Find files that match the specified file size; -true: take find The return value of the instruction is set to True; -typ<file type>: Only find files that match the specified file type; -uid<User identification code>: Search for files or directories that match the specified user identification code; -used<days>: The time after the specified directory or file has been accessed is calculated in the time after the specified directory or file has been accessed; -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 antecedent file system; -xtype<file type>: Effect and assignment of this parameter“-type"The parameter is similar, except that it checks for symbolic connections.

parameter

Starting Directory: find the starting directory of the file.

example

Match based on file or regular expression

List all files and folders in the current directory and subdirectories

find .

Find it in the / home directory txt file name

This code is by Java Architect must see network-Structure Sorting
find /home -name "*.txt"

Same as above, but case is ignored

find /home -iname "*.txt"

Find all in the current directory and subdirectories txt and pdf ending file

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

or

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

Match file path or file

find /usr/ -path "*local*"

Matching file path based on regular expression

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

Same as above, but case is ignored

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

Negative parameter

Find out which is not under / home txt file

find /home ! -name "*.txt"

Search by file type

find . -type Type parameter

Type parameter list:

  • f) ordinary documents
  • l) symbolic connection
  • d) directory
  • c) character device
  • b) block equipment
  • s} socket
  • p Fifo

Directory based deep search

The maximum depth down is limited to 3

find . -maxdepth 3 -type f

Search all files with a depth of at least 2 subdirectories from the current directory

find . -mindepth 2 -type f

Search based on file timestamp

find . -type f time stamp

UNIX/Linux file systems each file has three timestamps:

  • Access time (- atime / day, - amin / minute): the last access time of the user.
  • Modification time (- mtime / day, - mmin / minute): the last modification time of the file.
  • Change time (- ctime / day, - cmin / minute): the last modification time of file data element (such as permission, etc.).

Search all files accessed in the last seven days

find . -type f -atime -7

Search for all files that were accessed just seven days ago

find . -type f -atime 7

Search all files accessed in more than seven days

find . -type f -atime +7

Search for all files that have been accessed for more than 10 minutes

find . -type f -amin +10

Find the ratio file . log all files that take longer to modify

find . -type f -newer file.log

Match based on file size

find . -type f -size File size unit

File size unit:

  • b -- block (512 bytes)
  • c -- byte
  • w -- word (2 bytes)
  • k -- kilobyte
  • M -- megabyte
  • G -- Gigabyte

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 under the current directory txt file

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

Match based on file permissions / ownership

The file with permission of 777 is searched in the current directory

find . -type f -perm 777

Find out that the permission in the current directory is not 644 php file

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

Find all the files owned by the current directory user tom

find . -type f -user tom

Find all files owned by the current directory user group sunk

find . -type f -group sunk

Use the - exec option in conjunction with other commands

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

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

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

Find everything in your home directory txt file and delete

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 in the current directory txt file and write them to all txt file

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

It will be 30 days ago The log file is moved to the old directory

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

Find all in the current directory txt File and print it 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 of a single line command, the following method can accept multiple commands after - exec

-exec ./text.sh {} \;

Search but jump out of the specified directory

Find all under the current directory or subdirectory txt file, but skip subdirectory sk

find . -path "./sk" -prune -o -name "*.txt" -print

find other skills

To list all files with zero length

find . -empty