当前位置:首页 > 经验 >

find 怎么用(find怎么使用教程)

来源:原点资讯(www.yd166.com)时间:2022-11-01 02:08:58作者:YD166手机阅读>>

一 exec 和args 结合find命令区别

find 和 exec 、 xargs 搭配使用也是有区别的: 1、exec find将匹配到的文件传给exec执行,每找到一次传一次,但是如果文件名很多,导致命令太长,会导致执行失败。 2、xargs 解决一些命令不支持从管道传来的标准输入的参数,xargs作用将标准输入转成命令行参数( xargs [-options] [command]),简单小例子:

# echo不支持从管道输入参数 [root@iZwz90jb8mqajkli0ttrcbZ ~]# echo "abc" | echo # 加上args就可以 [root@iZwz90jb8mqajkli0ttrcbZ ~]# echo "abc" |xargs echo abc 二 常用查找

# 查找当前目录普通文件类型,通过file命令查更详细信息 find ./ -type f -print | xargs file # 查找指定用户abc的文件 find ./ -user abc # 查找修改时间在5天以上(不包括5天),且小于10天的文件 find / -mtime 5 -mtime -10 #查找过去一个小时修改的文件 find / -cmin -60 #查找大于500MB 小于1000MB的文件 find / -size 500M -size -1000M

-mtime指定的参数为天,有三种形式-n, n 和n 含义如下图

find 怎么用,find怎么使用教程(1)

说明: 最右边为当前时, 5 代表大于等于 6 天前的档案名, -5 代表小于等于 5 天内的档案名,5 则是代表 5-6 那一天的档案名。

三 批量收回相应的写权限

find . -perm -7 -print | xargs chmod o-w 四 在当前目录搜索特定字符hello

# 如果只有一层可以简单的: grep hello * # 如果多层要搜索 find ./ \* |xargs grep hello 五 批量改名

批量改名可以用rename或find结合xargs

[root@iZwz90jb8mqajkli0ttrcbZ test]# ls -al 总用量 0 drwxr-xr-x 2 root root 45 12月 1 21:12 . drwxr-xr-x 5 root root 85 12月 1 21:12 .. -rw-r--r-- 1 root root 0 12月 1 21:12 1.log -rw-r--r-- 1 root root 0 12月 1 21:12 2.log -rw-r--r-- 1 root root 0 12月 1 21:12 3.log [root@iZwz90jb8mqajkli0ttrcbZ test]# man rename [root@iZwz90jb8mqajkli0ttrcbZ test]# rename .log .deal *.log [root@iZwz90jb8mqajkli0ttrcbZ test]# ll 总用量 0 -rw-r--r-- 1 root root 0 12月 1 21:12 1.deal -rw-r--r-- 1 root root 0 12月 1 21:12 2.deal -rw-r--r-- 1 root root 0 12月 1 21:12 3.deal [root@iZwz90jb8mqajkli0ttrcbZ test]# echo "1.log 2.log 3.log" |xargs touch [root@iZwz90jb8mqajkli0ttrcbZ test]# ls 1.log 2.log 3.log [root@iZwz90jb8mqajkli0ttrcbZ test]# find ./ -name "*log" | xargs rename .log .deal [root@iZwz90jb8mqajkli0ttrcbZ test]# ll 总用量 0 -rw-r--r-- 1 root root 0 12月 1 21:22 1.deal -rw-r--r-- 1 root root 0 12月 1 21:22 2.deal -rw-r--r-- 1 root root 0 12月 1 21:22 3.deal 六 限制参数行数

xargs 将通过管道过来的标准输入作为参数,但是如果我们的参数很长,比如我们需要删除满足条件的日志文件,如果文件很长,都拼在一起,执行报错,会得到命令太长的错误。 如:

# 创建10000个空文件 echo {0..10000} |xargs touch # 看到了嘛,将所有的输入都变成了file的参数,如果文件非常多就会报参数过多 # -t 选项在命令执行前打印它 [root@iZwz90jb8mqajkli0ttrcbZ test]# find ./ -type f |xargs -t file|more file ./55 ./56 ./57 ./58 ./59 ./60 ./61 ./62 ./63 ./64 ./65 ./66 ./0 ./1 ./2 ./3 ./4 ./5 ./6 ./7 ./8 ./9 ./10 ./11 ./12 ./13 ./14 ./15 ./16 ./17 ./18 ./19 ./20 ./21 ./22 ./23 ./24 ./25 ./26 ./27 ./28 ./29 ./30 ./31 ./32 ./33 ./34 ./35 ./36 ./37 ./38 ./39 ./40 ./41 ./42 ./43 ./44 ./45 ./46 ./47 ./48 ./49 ./50 ./51 ./52 ./53 ./54 ./67 ./68 ./69 ./70 ./71 ./72 ./73 ./74 ./75 ./76 ./77 ./78 ./79 ./80 ./81 ./82 ./83 ./84 ./85 ./86 ./87 ./88 ./89 ./90 ./91 ./92 ./93 ./94 ./95 ./96 ./97 ./98 ./99 ./100 ./101 ./102 ./103 ./104 ./105 ./106 ./107 ./108 ./109 ./110 ./111 ./112 ... # 对于长参数情况,我们可以通过-ln通过n来限制一次执行多少行作为命令的参数 # -n2 限制输入的命令参数 [root@iZwz90jb8mqajkli0ttrcbZ test]# find ./ -type f |xargs -l2 -t file file ./4106 ./4107 ./4106: empty ./4107: empty file ./4108 ./4109 ....

理解了嘛,这个参数非常实用。

八 多进程执行

xargs的命令必须一个个执行,我们可以通过--max-procs 或-P来指定同时运行的进程:

[root@iZwz90jb8mqajkli0ttrcbZ test]# time find ./ -type f |xargs -P 2 -n2 -t file file ./5098 ./5099 file ./5100 ./5101 ./5098: empty ./5099: empty ./5100: empty ./5101: empty ^C real 0m3.924s user 0m1.932s sys 0m3.559s [root@iZwz90jb8mqajkli0ttrcbZ test]# time find ./ -type f |xargs -n2 -t file ... file ./9996 ./9997 ./9996: empty ./9997: empty file ./9998 ./9999 ./9998: empty ./9999: empty file ./10000 ./10000: empty real 0m5.455s user 0m2.080s sys 0m3.471s

明显看出通过-P指定了最多两个进程比默认的1个进程要快。

九 删除带空格或换行的文件

因为args默认是按照空格和换行分隔的,如果文件名本身含有换行或空格符号就无法删除,可以通过下面特殊命令删除。

[root@iZwz90jb8mqajkli0ttrcbZ test]# time find ./ -type f |xargs rm rm: 无法删除'./abc': 没有那个文件或目录 rm: 无法删除'aaa': 没有那个文件或目录 real 0m0.189s user 0m0.023s sys 0m0.165s [root@iZwz90jb8mqajkli0ttrcbZ test]# ls 'abc aaa' [root@iZwz90jb8mqajkli0ttrcbZ test]# find ./ -type f -print0|xargs -0 rm [root@iZwz90jb8mqajkli0ttrcbZ test]# ls [root@iZwz90jb8mqajkli0ttrcbZ test]#

栏目热文

find使用方法图解(find激活方法)

find使用方法图解(find激活方法)

如今说到折叠屏手机,相信很多人都不会感到陌生了。伴随着铰链技术以及供应链体系不断成熟完善,近些年折叠屏的功能体验和价格,...

2022-11-01 02:05:56查看全文 >>

find基础入门教程(find命令入门教学)

find基础入门教程(find命令入门教学)

find 命令入门 find 命令入门 列出当前目录及子目录下的所有文件 列出某个目录及子目录下的所有文件 根据文件名查...

2022-11-01 02:36:31查看全文 >>

find的详细使用方法(find怎么用)

find的详细使用方法(find怎么用)

​本帖将对 OPPO Find N 的「游戏适配全屏模式」「平行视窗支持应用 & 开关」「分屏手势」「调整大小」「游戏场...

2022-11-01 02:21:28查看全文 >>

finder在哪打开(电脑的finder在哪里)

finder在哪打开(电脑的finder在哪里)

当大家打开 MacOS ,首先接触到的就是 Finder 这个工具。不知道各位 macOS 用户在使用 Finder 管...

2022-11-01 02:32:02查看全文 >>

瓶盖手工制作大全(30个瓶盖手工制作大全步骤)

瓶盖手工制作大全(30个瓶盖手工制作大全步骤)

瓶盖儿的正确打开方式废旧瓶盖千万不要扔,开动脑筋,添加创意,对各种各样的瓶盖稍加改造,就可以将其变成孩子们的益智玩具!论...

2022-11-01 01:52:46查看全文 >>

finder在哪里打开(finder在哪打开)

finder在哪里打开(finder在哪打开)

Finder中文名“访达”,应该是Mac中使用最多的程序了,可是对于这么一个需要频繁使用的软件,你真的会用了吗?下面小编...

2022-11-01 02:21:08查看全文 >>

find正确使用方法(find使用技巧大全)

find正确使用方法(find使用技巧大全)

Linux是一个“一切皆文件的系统”,Linux中标识文件不通过后缀。find命令,不指定查找目录的情况下是针对整个文件...

2022-11-01 02:23:18查看全文 >>

find怎么用(find的方式)

find怎么用(find的方式)

概述find命令是linux下一个强大的查找命令。与locate命令相比,它需要遍历磁盘文件,因此查找速度较慢,但正因如...

2022-11-01 02:26:51查看全文 >>

find的用法归纳(find后加名词的用法)

find的用法归纳(find后加名词的用法)

find命令是我们日常工作中比较常用的Linux命令。全面的掌握这个命令可以使很多操作达到事半功倍的效果。如果对find...

2022-11-01 02:35:24查看全文 >>

find扫描软件的使用方法(哪种扫描软件最好用)

find扫描软件的使用方法(哪种扫描软件最好用)

小伙伴们,美美又来推荐干货文章啦~本文为美团研发同学实战经验,主要介绍Android静态扫描工具Lint、CheckSt...

2022-11-01 01:52:03查看全文 >>

文档排行