Linux Find command Usage collation

Source: Internet
Author: User

Find Path-option [-print] [-exec-ok command] {} \;

The directory path that the Path:find command looks for. For example, use. To represent the current directory, and/to represent the system root directory.
-print output of the found file to standard output
-exec command {} \; Command operation of the file to be traced, {} and \; a space between {} identifies the file path or filename to match
-ok and-exec are the same, except to consult the user before operation
===========================option====================
-name filename #查找名为filename的文件
-perm #按执行权限来查找
-user username #按文件属主来查找
-group GroupName #按组来查找
-mtime-n +n #按文件更改时间来查找文件,-n means n days or less, +n means n days ago
-atime-n +n #按文件访问时间来查GIN: 0px ">
-ctime-n +n #按文件创建时间来查找文件,-n means n days or less, +n means n days ago
-nogroup #查无有效属组的文件 that the genus of the file does not exist in the/etc/groups
-nouser #查无有效属主的文件, that is, the owner of the file does not exist in the/etc/passwd
-newer F1!f2 #找文件,-n means n days or less, +n means n days ago
-ctime-n +n #按文件创建时间来查找文件,-n means n days or less, +n means n days ago
-nogroup #查无有效属组的文件 that the genus of the file does not exist in the/etc/groups
-nouser #查无有效属主的文件, that is, the owner of the file does not exist in the/etc/passwd
-newer F1!F2 #查更改时间比f1新但比f2旧的文件
-type b/d/c/p/l/f #查是块设备, directories, character devices, pipelines, symbolic links, plain files
-size N[c] #查长度为n块 [or N-byte] files
-depth #使查找在进入子目录前先行查找完本目录
-fstype #查更改时间比f1新但比f2旧的文件
-type b/d/c/p/l/f #查是块设备, directories, character devices, pipelines, symbolic links, plain files
-size N[c] #查长度为n块 [or N-byte] files
-depth #使查找在进入子目录前先行查找完本目录
-fstype #查位于某一类型文件系统中的文件, these file system types are usually found in the/etc/fstab
-mount #查文件时不跨越文件系统mount点
-follow #如果遇到符号链接文件, just follow the file that the link refers to
-cpio%; #查位于某一类型文件系统中的文件, these file system types are usually found in/etc/fstab
-mount #查文件时不跨越文件系统mount点
-follow #如果遇到符号链接文件, just follow the file that the link refers to
-cpio #对匹配的文件使用cpio命令, back them up to tape devices
-prune #忽略某个目录
========================================================

Some examples are summarized below:

Based on file type:
Find. -type D-print #只列出当前目录所有的子目录
Find. ! -type d-print #只列出当前目录的非子目录 (file)
Find. -type F-print #只列出当前目录所有的文件
Find. -type L-print #只列出当前目录的所有符号链接
Find. -type C-print #只列出当前目录的所有字符设备
Find. -type B-print #只列出当前目录的所有块设备
Find. -type S-print #只列出当前目录的所有套接字
Find. -type P-print #只列出当前目录的所有Fifo

based on path or file name:
Find. -name ap*-o-name may* #查找以ap或may开头的文件
Find. -name "*.txt"-print #在当前目录中中查. txt file and displays
Find. -name "[a-z]*"-print #在当前目录中查以大写字母开头的文件并显示
Find/etc-name "host*"-print #在/etc directory with the file that starts with host and displays
Find. -name "[A-z][a-z][0–9][0–9].txt"-print #查以两个小写字母和两个数字开头的txt文件
Find. ! -name "*.txt"-print #列出所有不以. txt End file name
Find/home/user-path "*ttt*"-print #-path matches the file path and filename as a whole, so a file path or filename containing TTT will be matched to
Find. -name "*.txt"-print #匹配当前目录下所有. txt file and output its path
Find. -iname "example*"-print #匹配当前目录下以example开头的文件名, case-insensitive, and output its path
Find. \ (-name "*.txt"-o-name "*.pdf" \)-print #匹配当前目录下所有. txt files and. pdf files, and outputs their paths
Find. -type f-name "*.swp"-delete #删除当前目录下所有的. swp file
Find. -type f-name "*.C"-exec Cat {} \;>all_c_files.txt #找到所有C文件并拼接起来写入单个文件all_c_files. txt. The output of the Find command is a single data stream, so do not append >>
Find/mnt-name tom.txt-ftype vfat #在/mnt to find files with the name Tom.txt and file system type VFAT
Find/mnt-name Tom.txt! -ftype vfat #在/mnt to find files with name Tom.txt and file system type not VFAT

Based on user file permissions:
Find. -perm 755-print #查当前目录下下权限为755的文件
Find/-group Cat #查找在系统中属于cat组的目录和文件
Find/-nouser #查找在系统中属于作废用户的文件
Find/-user Fred #查找在系统中属于fred这个用户的文件
Find. -type F-user Ubuntu-print #列出当前目录下属于用户ubuntu的文件
Find. -type f-name "*.php" | -perm 644-print #列出当前目录下所有权限为644的php文件
Find/home-uid +501 #列出 A file or directory with a user ID greater than 501 in the/home directory
Find/home-gid 501 #列出 A file or directory with a group ID of 501 inside/home
Find/home-nouser #列出 files or directories that do not belong to local users in/home
Find/home-nogroup #列出 files or directories that are not part of a local group in/home

Based on directory or file size:
Find. -type F-empty #查找大小为0的文件或空目录
Find. -type f-size +1000000c-print #查长度大于1Mb的文件
Find. -type f-size 100c-print # Check file length 100c
Find. -type f-size +10-print #查长度超过期作废10块的文件 (1 block = 512 bytes)
Find. -type f-size +2k #搜索当前目录下大于2KB的文件
Find. -type f-size-2k #搜索当前目录下小于2KB的文件
In addition to kilobytes (k), there are blocks (b 512 bytes), bytes (c), Word (w), megabytes (M), and gigabytes (G)

based on operation time:
Find/-type f-amin-10 #查找在系统中最后10分钟访问的文件
Find/-type f-atime-2 #查找在系统中最后48小时访问的文件
Find/-type F-empty #查找在系统中为空的文件或者文件夹
Find/-type f-mmin-5 #查找在系统中最后5分钟里修改过的文件
Find/-type f-mtime-1 #查找在系统中最后24小时里修改过的文件
Find/home-type f-mtime-2 #在/Home Check the files that have been changed in the last two days
Find/home-type f-atime-1 #在/home search files accessed within 1 days
Find/home-type f-mmin +60 #在/Home Check the files that were changed 60 minutes ago
Find/home-type f-amin +30 #在/Home Check the files that were accessed last 30 minutes ago
Find. -type f-atime-7-print #列出当前目录在最近7天内被访问过的所有文件
Find. -type f-atime 7-print #列出当前目录恰好在第七天前被访问过的所有文件
Find. -type f-atime +7-print #列出当前目录访问时间超过七天的所有文件
Find. -type f-newer file.txt-print #找出当前目录比file. txt for all files that have been modified for a longer time, using-newer, you can specify a timestamp reference file for comparison
Find/home-type f-anewer tmp.txt #在/Home Check access time is closer than Tmp.txt file or directory
Find/home-used-2 #列出文件或目录被改动过之后, files or directories accessed within 2nd
Find./-type f-mtime-1-exec ls-l {} \ #查询当前目录下当天修改过的文件的详细信息
Find./-type f-mtime-1-ok ls-l {} \ #查询当前目录下当天修改过的文件的详细信息并询问是否显示
(-atime is based on access time;-mtime is based on modified time;-ctime is based on time of change.) Three units of the parameter are days)
(-amin is based on access time;-mmin is based on modified time;-cmin is based on time of change.) The units of these three parameters are minutes)

find and-exec parameters:
Find.-perm-007-exec ls-l {} \;      #查所有用户都可读写执行的文件同-perm 777
Find-type f-user root-exec chown ubuntu {} \;      #将当前目录下所有root的文件改为属于ubuntu, where {} is replaced with each matching file name, {} for matching, and-exec with
Find-type f-name "*.c"-exec Cat {} \;>all_c_files.txt   #找到所有C文件并拼接起来写入单个文件all_c_files. txt. The output of the Find command is a single data stream, so do not use >> to append
Find.-type f-atime +10-name "*.txt"-exec cp {}/data \;     #找到10天前访问            . txt file and copy it to the/data directory
find./log02*-exec mv {}/data/game \;   #找到当前目录下以log02开头的文件并将其移动到 The/data/game directory
Find-type f-mtime +5-exec-ok rm {} \ #在当前目录中查找更改时间在5日以前的 Files and delete them
find.-type f-name "*.txt"-exec printf "Text file:%s\n" {} \;   #-exec combined with printf output information
(the-exec parameter of find can be connected Any other command, depending on what the requirements are)

find and xargs combination:
Find.-type f-name "*.txt"-print0 | xargs-0 rm-f   ;   #匹配并删除所有的. txt file, xargs-0 as input delimiter
Find/data-type f-name "*.c"-print0 | xargs-0 wc-l     #统计/da The number of rows in the TA directory for all c files
Find./-name "Test*.log" | xargs grep "\[aaaa\" "| wc–l    #通过第一个管道find查找当前目录下所有以test开头的log The contents of the file and send the content to the second pipe xargs grep, filtering and finding all rows containing "[AAAA]", and then sending the WC command to count the number of rows
Find./-name "Test*.log" | xargs grep "\[aaaa\]" > Re.txt   #同上例, redirect the found row to the Re.txt file
Find./-name "Test*.log" | xargs grep "\[aaaa\" "| grep" \ "svr\": 1 ">re . log #同上例 When multiple fields need to be matched: for example, Match [AAAA] and "SVR": 12 fields
Find./-name "Test*.log" | xargs grep "\[aaaa\" | grep "\" svr\ ": 1" | grep "\[2016-12-27 10:01:59\]" > Re.log   #同上例, multiple match a Time field in [y-m-d h:i:s] Format
(the Xargs command should immediately follow the pipe operator. Use standard input as the primary source data stream. )

Other:
Find. -iregex ". *\ (\.py\|\.sh\) $" #-iregex ignores the case of the regular expression here is the case of ignoring the suffix
Find. -maxdepth 2-type f-print #遍历的最大深度距离此目录最多为2层子目录, list all common Files
Find. -mindepth 2-type F-print #遍历的深度距离当前目录至少两个子目录, listing all files
Find/data \ (-name ". Git"-prune \)-o \ (-type f-print \) #在/data directory to search all files, skip. Git subdirectories when searching. \ (-name ". Git"-prune \) is used here to exclude the. Git directory
Find/etc-name "passwd*"-exec grep "Cnscn" {} \ #看是否存在cnscn用户
Find. -type f-name april* fprint file #在当前目录下查找以april开始的文件, and outputs the result to file
Find. -links +2 #查硬连接数大于2的文件或目录

Linux Find command Usage collation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.