studyNote-linux-shell-find-examples

前言:本文的例子均来源于man手册第一章节的find,man 1 find查看

e.g.01

手册原文:

find /tmp -name core -type f -print | xargs /bin/rm -f

Find files named core in or below the directory /tmp and delete them.  Note that this will work incorrectly if there are any filenames containing newlines, single or double quotes, or spaces.

效果:
删除/tmp路径下面名字为core的普通文件,但是文件不能够有换行符、单引号、双引号和空格符,否则会出错。

e.g.02

手册原文:

find /tmp -name core -type f -print0 | xargs -0 /bin/rm -f

Find files named core in or below the directory /tmp and delete them, 
processing filenames in such a way that file or directory names containing single or double quotes, 
spaces or newlines are correctly handled.  
The -name test comes before the -type test in order to avoid having to call stat(2) on every file.

效果:
删除/tmp路径下面名字为core的普通文件,和上面的区别就是文件可以有换行符、单引号、双引号和空格符,因为用字符\0隔开了。

e.g.03

手册原文:

find . -type f -exec file '{}' \;

Runs `file' on every file in or below the current directory.
Notice that the braces are enclosed in single quote marks to protect them from interpretation as shell script punctuation.  
The semicolon is similarly protected by the use of a backslash, though single quotes could have been used in that case also.

效果:
对当前目录下每个文件执行file命令。这里的{}在执行命令的时候会被替换成找到的文件名,这里的;是作为exec选项的参数,不然exec命令就不完整了。(注意: ‘{}’ ;加单引号和转移符号都是为了避免shell错误解释)

e.g.04

手册原文:

find / \( -perm -4000 -fprintf /root/suid.txt '%#m %u %p\n' \) , \
 \( -size +100M -fprintf /root/big.txt '%-10s %p\n' \)

Traverse the filesystem just once, 
listing setuid files and directories into /root/suid.txt and large files into /root/big.txt.

效果:
递归查找根目录下具有SUID(Set User ID)权限的文件,并将结果输出到/root/suid.txt文件中;大于100M的文件,将结果输出到/root/big.txt文件中。然后前者的输出格式是这个%#m %u %p\n,后者输出的格式%-10s %p\n是这个。第一行末尾 \只是一行的连接符,别看错了。

e.g.05

手册原文:

find $HOME -mtime 0

Search for files in your home directory which have been modified in the last twenty-four hours.  
This command works this way because the time since each file was last modified is divided by 24 hours and any remainder is discarded.  
That means that to match -mtime 0, a file will have to have a modification in the past which is less than 24 hours ago.

效果:
查找用户家目录下所有在24小时内修改过的文件。它是这样算的,-mtime 后面的0是由文件当前修改后的时间/24小时,所以0就代表修改不超过一天,1就代表修改不超过2天,依此类推。

e.g.06

手册原文:

find /sbin /usr/sbin -executable \! -readable -print

Search for files which are executable but not readable.

效果:
查找 /sbin 和 /usr/sbin 目录下所有可执行但不可读的文件(针对文件所有者用户)。

e.g.07

手册原文:

find . -perm 664

Search for files which have read and write permission for their owner, and group, but which other users can read but not write to.  
Files which meet these criteria but have other permissions bits set (for example if someone can execute the file) will not be matched.

效果:
查找当前文件夹下面文件权限为664的文件

e.g.08

手册原文:

find . -perm -664

Search for files which have read and write permission for their owner and group, and which other users can read, 
without regard to the presence of any extra permission bits (for example the executable bit).  
This will match a file which has mode 0777, for example.

效果:
查找当前文件夹下面文件权限至少为664的文件,比如说你的权限666也可以

e.g.09

手册原文:

find . -perm /222

Search for files which are writable by somebody (their owner, or their group, or anybody else).

效果:
查找当前文件夹中至少有一组的写权限是有的 的 文件。

e.g.10

手册原文:

find . -perm /220
find . -perm /u+w,g+w
find . -perm /u=w,g=w

All  three  of  these commands do the same thing, 
but the first one uses the octal representation of the file mode, 
and the other two use the symbolic form.
These commands all search for files which are writable by either their owner or their group. 
The files don't have to be writable by both the owner and group to be matched; either will do.

效果:
查找当前文件夹中至少有一组(对于文件所有者和同组的用户)的写权限是有的 的 文件。

e.g.11

手册原文:

find . -perm -220
find . -perm -g+w,u+w

Both these commands do the same thing; search for files which are writable by both their owner and their group.

效果:
查找当前文件夹中所有者和同组都有写权限的文件。

e.g.12

手册原文:

find . -perm -444 -perm /222 \! -perm /111
find . -perm -a+r -perm /a+w \! -perm /a+x

These two commands both search for files that are readable for everybody ( -perm -444 or -perm -a+r), 
have at least one write bit set ( -perm /222 or -perm /a+w) but are not executable for anybody ( ! -perm /111 and ! -perm /a+x respectively).

效果:
查找文件所有者和同组和其他人都有读权限,并且所有者和同组和其他人中至少有一个有写权限,并且所有者和同组和其他人都没有执行权限。

e.g.13

手册原文:

cd /source-dir
find . -name .snapshot -prune -o \( \! -name '*~' -print0 \)|
cpio -pmd0 /dest-dir

This command copies the contents of /source-dir to /dest-dir, 
but omits files and directories named .snapshot (and anything in them).  
It also omits files or directories whose name ends in ~, 
but not their contents.  
The construct -prune -o \( ... -print0 \) is quite common.  
The idea here is  that the  expression before -prune matches things which are to be pruned.  
However, the -prune action itself returns true, 
so the following -o ensures that the right hand side is evaluated only for those directories which didn't get pruned (the contents of the pruned directories are not even visited, so their contents are irrelevant).  
The expression on the right hand side of the -o is in parentheses only for clarity.  
It emphasises that the -print0 action takes place only for things that didn't have -prune applied to them.  
Because the default `and' condition between tests binds more tightly than 
-o, this is the default anyway, but the parentheses help to show what is going on.

效果:
查找/source-dir目录下文件的时候忽略以这个.snapshot结尾的文件,然后不要输出以~结尾的文件并以\0隔开,然后将这些输出通过管道符号,拷贝到根目录下面的/dest-dir文件夹里面去。

cpio: 是一个用于归档和备份的命令行工具。cpio通常与find命令结合使用,以复制目录结构。
-p: 表示保留文件的原始权限、所有者和时间戳信息。
-m: 表示创建必要的目录结构。
-d0: 表示使用空字符作为分隔符读取输入文件名,与上面匹配。

e.g.14

手册原文:

find repo/ \( -exec test -d '{}'/.svn \; -or \
-exec test -d {}/.git \; -or -exec test -d {}/CVS \; \) \
-print -prune

Given the following directory of projects and their associated SCM administrative directories, perform an efficient search for the projects' roots:

repo/project1/CVS
repo/gnu/project2/.svn
repo/gnu/project3/.svn
repo/gnu/project3/src/.svn
repo/project4/.git

In this example, -prune prevents unnecessary descent into directories that have already been discovered (for example we do not search project3/src because we already found project3/.svn), 
but ensures sibling directories (project2 and project3) are found.

效果:
这个find命令的作用是在repo/目录及其子目录下查找使用特定版本控制系统(CVS、SVN 或 Git)管理的项目根目录。

  • find repo/: 从repo/目录开始搜索。

  • -exec test -d '{}'/.svn \;: 对于每一个找到的文件或目录,执行test -d命令检查该路径下的.svn目录是否存在。如果存在,则表示该项目使用了Subversion(SVN)作为版本控制系统。

  • -or -exec test -d {}/.git \;: 类似地,检查当前路径下是否包含.git目录,如果存在,则说明该项目使用了Git作为版本控制系统。

  • -or -exec test -d {}/CVS \;: 同样地,检查当前路径下是否包含CVS目录,存在则表示该项目使用了Concurrent Versions System (CVS) 作为版本控制系统。

  • -print: 当满足上述条件之一时,打印出匹配的项目根目录路径。

  • -prune: 如果在当前路径下找到了SCM管理目录(即.svn.gitCVS),那么就不再继续深入到该路径的子目录中进行搜索。这样可以避免对已经找到的项目根目录的子目录重复搜索,提高了搜索效率,并确保了同一层级含有不同版本控制系统管理的项目都能被发现。

对于给出的例子,在执行完此命令后,将输出以下内容:

repo/project1/CVS
repo/gnu/project2/.svn
repo/gnu/project3/.svn
repo/project4/.git

这些路径代表的就是使用对应版本控制系统管理的项目根目录。

e.g.15

手册原文:

find /tmp -type f,d,l

Search for files, directories, and symbolic links in the directory /tmp passing these types as a comma-separated list (GNU extension), 
which is otherwise equivalent to the longer, yet more portable:

find /tmp \( -type f -o -type d -o -type l \)

效果:
在/tmp路径下面查找普通文件、目录、链接文件这几个文件类型的文件,find /tmp ( -type f -o -type d -o -type l )写法方便移植。

你可能感兴趣的:(linux)