linux find command

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

grammar

find(options)(parameters)

Options

-amin<minutes>: Find files or directories that have been accessed at a specified time, in minutes;
-anewer <reference file or directory>: Find the file or directory whose access time is closer to the current one than the access time of the specified file or directory;
-atime<24 hours>: Find the files or directories that have been accessed at the specified time, 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 a file or directory whose change time is closer to the current file or directory than the change time of the specified file or directory;
-ctime<24 hours>: Find files or directories that have been changed at the specified time, in units of 24 hours;
-daystart: Calculate the time from today;
-depth: Search from the deepest subdirectory in the specified directory;
-expty: Find files with a file size of 0 Byte, or an empty directory without any subdirectories or files in the directory;
- exec <execution command>: If the return value of the find command is True, the command is executed;
-false: Set the return value 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 that of specifying the "-print" parameter, but the result will be saved as the specified list file;
-fprint0<list file>: The effect of this parameter is similar to that of specifying the "-print0" parameter, but the result will be saved as the specified list file;
-fprintf<list file><output format>: The effect of this parameter is similar to that of specifying the "-printf " parameter, but the result will be saved as the specified list file;
-fstype <file system type>: only look for files or directories under this file system type;
-gid<group ID>: Find files or directories that match the specified group ID;
-group <group name>: Find 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 that of specifying the "-lname" parameter, but the difference between upper and lower case characters is ignored;
-iname<template style>: The effect of this parameter is similar to that of specifying the "-name" parameter, but the difference between upper and lower case characters 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 between upper and lower case characters is ignored;
-iregex<template style>: The effect of this parameter is similar to that of specifying the "-regexe" parameter, but the difference between upper and lower case characters is ignored;
-links<number of connections>: Find files or directories that match the specified number of hard links;
-iname<template style>: Specify a string as a template style for finding symbolic links;
-ls: Assuming the return value of the find command is True, list the file or directory name to 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, 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 the file or directory whose change time is closer to the current file or directory than the change time of the specified file or directory;
-nogroup: find files or directories that do not belong to the local host group identifier;
-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 localhost user ID;
-ok <execution 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 execution of the command will be abandoned;
-path<template style>: Specify a string as the template style for finding directories;
-perm<permission value>: Find files or directories that match the specified permission value;
-print: Assuming the return value of the find command is True, list the file or directory name to standard output. The format is one name per column, and each name is preceded by a "./" string;
-print0: Assuming the return value of the find command is True, list the file or directory name to standard output. The format is that all names are on the same line;
-printf <output format>: Assuming the return value of the find command is True, list the file or directory name to standard output. The format can be specified by yourself;
-prune: do not look 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 meet the specified file size;
-true: Set the return value of the find command to True;
-typ<file type>: only find files that match the specified file type;
-uid<user ID>: Find files or directories that match the specified user ID;
-used<days>: Find the files or directories that have been accessed at the specified time after the file or directory has been changed, the unit is calculated in days;
-user<owner name>: the file or directory of the finder and the specified owner name;
-version or --version: Display version information;
-xdev: limit the scope to the first file system;-xtype 
<filetype>: The effect of this parameter is similar to specifying the "-type" parameter, the difference is that it checks for symbolic links.

parameter

Starting Directory: The starting directory to look for files.

example

Match against files or regular expressions

List all files and folders in the current directory and subdirectories

find .

/homeLook for filenames ending in .txt in a directory

find /home -name "*.txt"

Same as above, ignoring case

find /home -iname "*.txt"

Find all files ending in .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\)$"

同上,但忽略大小写

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

否定参数

找出/home下不是以.txt结尾的文件

find /home ! -name "*.txt"

根据文件类型进行搜索

find . -type 类型参数

类型参数列表:

  • 普通文件
  • 符号连接
  • d 目录
  • 字符设备
  • 块设备
  • 套接字
  • Fifo

基于目录深度搜索

向下最大深度限制为3

find . -maxdepth 3 -type f

搜索出深度距离当前目录至少2个子目录的所有文件

find . -mindepth 2 -type f

根据文件时间戳进行搜索

find . -type f 时间戳

UNIX/Linux文件系统每个文件都有三种时间戳:

  • 访问时间(-atime/天,-amin/分钟):用户最近一次访问时间。
  • 修改时间(-mtime/天,-mmin/分钟):文件最后一次修改时间。
  • 变化时间(-ctime/天,-cmin/分钟):文件数据元(例如权限等)最后一次修改时间。

搜索最近七天内被访问过的所有文件

find . -type f -atime -7

搜索恰好在七天前被访问过的所有文件

find . -type f -atime 7

搜索超过七天内被访问过的所有文件

find . -type f -atime +7

搜索访问时间超过10分钟的所有文件

find . -type f -amin +10

找出比file.log修改时间更长的所有文件

find . -type f -newer file.log

根据文件大小进行匹配

find . -type f -size 文件大小单元

文件大小单元:

  • b —— 块(512字节)
  • c —— 字节
  • w —— 字(2字节)
  • k —— 千字节
  • M —— 兆字节
  • G —— 吉字节

搜索大于10KB的文件

find . -type f -size +10k

搜索小于10KB的文件

find . -type f -size -10k

搜索等于10KB的文件

find . -type f -size 10k

删除匹配文件

删除当前目录下所有.txt文件

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

根据文件权限/所有权进行匹配

当前目录下搜索出权限为777的文件

find . -type f -perm 777

找出当前目录下权限不是644的php文件

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

找出当前目录用户tom拥有的所有文件

find . -type f -user tom

找出当前目录用户组sunk拥有的所有文件

find . -type f -group sunk

借助-exec选项与其他命令结合使用

找出当前目录下所有root的文件,并把所有权更改为用户tom

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

上例中,{} 用于与-exec选项结合使用来匹配所有文件,然后会被替换为相应的文件名。

找出自己家目录下所有的.txt文件并删除

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

In the above example, -ok behaves the same as -exec , but it will give a 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 .log files older than 30 days 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 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 accept multiple commands after -exec

-exec ./text.sh {} \;

Search but jump out of the specified directory

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

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

find other tips collection

To list all zero-length files

find . -empty

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325549297&siteId=291194637