当前位置: 首页 > news >正文

studyNote-linux-shell-find-examples

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

e.g.01

手册原文:

find /tmp -name core -type f -print | xargs /bin/rm -fFind 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 -fFind 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 0Search 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 -printSearch for files which are executable but not readable.

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

e.g.07

手册原文:

find . -perm 664Search 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 -664Search 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 /222Search 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=wAll  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+wBoth 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+xThese 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-dirThis 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 -pruneGiven 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/.gitIn 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,lSearch 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 )写法方便移植。

http://www.lryc.cn/news/293736.html

相关文章:

  • 使用 Python 进行自然语言处理第 3 部分:使用 Python 进行文本预处理
  • Python新春烟花盛宴
  • 【QT+QGIS跨平台编译】之二十:【xerces+Qt跨平台编译】(一套代码、一套框架,跨平台编译)
  • 18.通过telepresence调试部署在Kubernetes上的微服务
  • QT 范例阅读:系统托盘 The System Tray Icon example
  • OpenAI Gym 高级教程——深度强化学习库的高级用法
  • K8sGPT 会彻底改变你对 Kubernetes 的认知
  • 计组学习笔记2024/2/4
  • 25种Google的搜索技巧
  • 769933-15-5,Biotin aniline,可以合成多种有机化合物和聚合物
  • 回归预测 | Matlab实现POA-CNN-LSTM-Attention鹈鹕算法优化卷积长短期记忆网络注意力多变量回归预测(SE注意力机制)
  • B站视频在电商中的应用:如何利用item_get_video API提高转化率
  • 【Linux】统信服务器操作系统V20 1060a-AMD64 Vmware安装
  • c++类继承
  • Git 指令
  • JAVA中的多态参数
  • Ubuntu Linux 下安装和卸载cmake 3.28.2版本
  • 【C++】类和对象3:默认成员函数之析构函数
  • 2024美赛C题完整解题教程及代码 网球运动的势头
  • 二、人工智能之提示工程(Prompt Engineering)
  • 【leetcode题解C++】98.验证二叉搜索树 and 701.二叉搜索树中的插入操作
  • 【Vue.js设计与实现】第二篇:响应系统-阅读笔记(持续更新)
  • 微信小程序之本地生活案例的实现
  • 智能决策的艺术:探索商业分析的最佳工具和方法
  • C#(C Sharp)学习笔记_前言及Visual Studio Code配置C#运行环境【一】
  • 政安晨的AI笔记——Bard大模型最新提示词创作绘画分析
  • 基础算法bfs -剪枝问题
  • 在Meteor Lake上测试基于Stable Diffusion的AI应用
  • 情人节心动礼物:共度情人节美好时刻的礼物推荐
  • 远程手机搭建Termux环境,并通过ssh连接Termux