find 命令简单总结(1)

来源:互联网 发布:剑灵saber捏脸数据 编辑:程序博客网 时间:2024/05/21 11:27
find 可以算是 linux 非常重要的命令了,用法也非常复杂。
作为 Linux管理员 ,这个命令也是经常用到,所以就总结一下,同时也分享给一些刚刚学习Linux的朋友们。
用法介绍
find <指定目录> <指定条件> <指定动作>
  -<指定目录>: 所要搜索的目录及其所有子目录。默认为当前目录。
  - <指定条件>: 所要搜索的文件的特征。
  - <指定动作>: 对搜索结果进行特定的处理,显示到标准输出,默认为print

说一些 常用的选项
1 -name 可以指定 文件名称,进行查找。
查找当前目录(以及子目录) 以.txt 结尾的文件
find ./ -name "*.txt"
2   -type 可以指定文件类型
         f   文件            d  目录
           b  块设备           c   字符设备
           l    符合链接文件           p  命令管道文件           s  套接字文件
其中 f d 应该是用的比较多的。
查找 当前目录 下 的目录文件

当然也可以 换一种显示方式,通过xargs 这个 参数 4个换一行。(后面会详细说)

查看普通文件:
[root@myCentOS ~]# find /root/test2 -type f
/root/test2/3.txt
/root/test2/1.txt
/root/test2/2.txt
3 -mtime 通过 修改文件的修改时间来查找文件 ,是非常有用的。 格式有很多。
这种 格式 有几种 :(默认的单位是
-atime +n/-n +n 表示 大于n 天, -n 小于n 天的时间
-ctime +n/-n +n 表示 大于n 天, -n 小于n 天的时间
-mtime +n/-n +n 表示 大于n 天, -n 小于n 天的时间
关于这几个时间 我就不多解释了, atime 访问时间,
ctime ,change time , 写入文件,修改权限,链接,
mtime , modified time 写入文件是随文件内容的更改而更改。
当然 也可以按照 分钟来查找
 -amin  +n/-n 时间为分钟,与atime 相对应
-mmin +n/-n
-cmin +n/-n
举个例子
查找当前目录 10天内 修改的文件
find . -mtime -10
find . -mtime -10
举个例子:
例1.查找 10 内没有修改的文件
[root@myCentOS ~]# find . -mtime -10|xargs -n 5
. ./Cprograme ./Cprograme/hello.i ./Cprograme/hello ./Cprograme/hello.c
./Cprograme/hello.s ./Cprograme/hello.o ./hello ./.lesshst ./hello.c
./8.txt ./shell ./shell/1.sh ./.bash_history ./.gitconfig
./.ssh ./.ssh/id_rsa ./.ssh/id_rsa.pub ./.ssh/known_hosts ./.viminfo
./finddir

例2 搜索当前目录中,所有过去10分钟中更新过的普通文件
cd finddir/
touch {1..10}.txt
[root@myCentOS finddir]# find . -mmin -10
.
./3.txt
./5.txt
./1.txt
./7.txt
./8.txt
./10.txt
./6.txt
./4.txt
./9.txt
./2.txt
例子3 用 ctime
现在 编辑 2.txt
find . -cmin -2 查找 2min 内 改变的 文件
[root@myCentOS finddir]# find . -cmin -2
.
./2.txt
4    -size
文件的大小, k M G
-size -10k 小于10 k 的文件
-size +10k 大于 10k 的文件
-size -1M 小于1M 的文件
-size +1M 大于1M 的文件
举例: 在 vim 1.txt 写点东西。
1) 查找当前目录下 大于10k 的文件。
[root@myCentOS finddir]# find . -size +10k
./1.txt
来看一下 这个文件 大小:
find . -size +10k | xargs ls -lh
-rw-r--r-- 1 root root 22K 9月 10 20:59 ./1.txt
[root@myCentOS finddir]# find . -size -10k
.
./3.txt
./5.txt
./7.txt
./8.txt
./10.txt
./6.txt
./4.txt
./9.txt
./2.txt
来看一下,ls 下的文件 大小
[root@myCentOS finddir]# ls -lh
总用量 28K
-rw-r--r-- 1 root root 0 9月 10 20:41 10.txt
-rw-r--r-- 1 root root 22K 9月 10 20:59 1.txt
-rw-r--r-- 1 root root 41 9月 10 20:50 2.txt
-rw-r--r-- 1 root root 0 9月 10 20:41 3.txt
-rw-r--r-- 1 root root 0 9月 10 20:41 4.txt
-rw-r--r-- 1 root root 0 9月 10 20:41 5.txt
-rw-r--r-- 1 root root 0 9月 10 20:41 6.txt
-rw-r--r-- 1 root root 0 9月 10 20:41 7.txt
-rw-r--r-- 1 root root 0 9月 10 20:41 8.txt
-rw-r--r-- 1 root root 0 9月 10 20:41 9.txt
换一个目录 cd /root/
2) 查找 大于1M 的文件,
[root@myCentOS ~]# find . -size +1M
./backup/2.sql
[root@myCentOS ~]# ls -lh backup/2.sql
-rw-r--r-- 1 root root 2.3M 8月 26 21:52 backup/2.sql
5 -maxdepth 基于目的深度搜索,
现在 测试一下
比如说 -maxdepth 1 1表示只在当前目录查找, 2 表示向下两级
比如在当前目录下查找 txt 文件
[root@myCentOS ~]# find . -maxdepth 1 -name "*.txt" |xargs -n 4
./3.txt ./5.txt ./a.txt ./tmp.txt
./1.txt ./7.txt ./11.txt ./8.txt
./6.txt ./4.txt ./12.txt ./test.txt
./2.txt
mkdir -p 111/222/333/444/555
cd /root/111/222/333/444/555
vim 1.txt
cd /root/
find -maxdepth 6 -name "*.txt" |grep 1.txt


6 -iname 忽略大小写
用文件名查找文件,忽略大小写
[root@myCentOS finddir]# ls
10.txt 2.txt 4.txt 6.txt 8.txt test.txt
1.txt 3.txt 5.txt 7.txt 9.txt TEST.TXT
[root@myCentOS finddir]# find . -iname "test.txt"
./TEST.TXT
./test.txt
[root@myCentOS finddir]# find . -iname "TEST.txt"
./TEST.TXT
./test.txt
7 -a -o -not 条件限制
-a:表示且
-o:表示或
-not:表示非
举个例子
1)查找当前目录下 以.sql 结尾,且 文件大小大于2M的文件
[root@myCentOS ~]# find . -name "*.sql" -a -size +2M
./backup/2.sql
[root@myCentOS ~]# du -sh ./backup/2.sql
2.3M ./backup/2.sql
2) 查找当前目录下 以.sql 结尾,或者 大于2M 的文件
[root@myCentOS ~]# find . -name "*.sql" -o -size +2M
./12.sql
./1.sql
./backup/1.sql
./backup/4.sql
./backup/3.sql
./backup/2.sql
./11.sql
3)查找 /root/finddir 下 不以 txt 结尾的文件。
cd /root/finddir
[root@myCentOS finddir]# ls
10.txt 1.sh 2.txt 4.txt 6.txt 8.txt et nu TEST.TXT
1.c 1.txt 3.txt 5.txt 7.txt 9.txt test.txt
[root@myCentOS finddir]# find . -not -name "*.txt"
.
./TEST.TXT
./et nu
./1.c
./1.sh
当然 find 命令还有很多选项<指定条件>,这里只是介绍了比较常用的选项
find 的指定动作

find <指定目录> <指定条件> <指定动作>

find的处理动作可以是:
      -print   默认为输出
       -ls          显示查找到的文件的详细信息,相当于'ls -l'
-exec COMMAND {} \;
-ok COMMAND {} \;
       -exec    COMMAND {}  \;     其中COMMAND中有对查找到的文件进行操作时,用{}来替代查找到的文件,\;表示使用-exec的结束符,是固定格式
exec与ok的区别:ok会提供交互式,让你确认。而exec则不需要;

下面举个例子:
还是在finddir 下面:
1) 查找以.sh 结尾的文件,之后交给ls 命令, 详细打印 文件的属性。
[root@myCentOS finddir]# find -name "*.sh"
./1.sh
[root@myCentOS finddir]# find -name "*.sh" -ls
131167 4 -rw-r--r-- 1 root root 1 9月 10 22:32 ./1.sh

2) 来测试 -ok 来测试 是否询问删除。
[root@myCentOS data]# ls /root/finddir/data/
10.txt 1.txt 2.txt 3.txt 4.txt 5.txt 6.txt 7.txt 8.txt 9.txt
find /root/finddir/data/ -name "*.txt" -ok rm {} \;
< rm ... /root/finddir/data/3.txt > ? n
< rm ... /root/finddir/data/5.txt > ? n
< rm ... /root/finddir/data/1.txt > ? n
< rm ... /root/finddir/data/7.txt > ? n
< rm ... /root/finddir/data/8.txt > ? n
< rm ... /root/finddir/data/10.txt > ? y
< rm ... /root/finddir/data/6.txt > ? n
< rm ... /root/finddir/data/4.txt > ? n
< rm ... /root/finddir/data/9.txt > ? n
< rm ... /root/finddir/data/2.txt > ? n
上面 询问我是否 删除10.txt 我输入了 'y' 代表删除,ls 看看。
[root@myCentOS data]# ls
1.txt 2.txt 3.txt 4.txt 5.txt 6.txt 7.txt 8.txt 9.txt


3) -exec 来试一试会不会提示,
[root@myCentOS data]# find /root/finddir/data/ -name "*.txt" -exec rm {} \;
[root@myCentOS data]# ls
[root@myCentOS data]#
已经没有文件了, 并且没有提示, 所以这种也是比较危险的,谨慎操作吧。
总结: exec与ok的区别:ok会提供交互式,让你确认。而exec则不需要;

当然 find 如果 要和 -exec 或者 |xargs 功能就强大多了。
1)
find . -name "*.txt" -exec rm {} \;

当然 如果你不放心的话, 可以用 -ok
[root@myCentOS data]# find . -name "*.txt" -ok rm {} \;
< rm ... ./3.txt > ? y
< rm ... ./5.txt > ? y
< rm ... ./1.txt > ? y
< rm ... ./7.txt > ? n
< rm ... ./8.txt > ? n
< rm ... ./10.txt > ? n
< rm ... ./6.txt > ? n
< rm ... ./4.txt > ? n
< rm ... ./9.txt > ? n
< rm ... ./2.txt > ? n
[root@myCentOS data]# ls
10.txt 2.txt 4.txt 6.txt 7.txt 8.txt 9.txt
这样可以支持 删除前进行询问,相对来说好一点。 y /n
2)可以批量重命名
find . -name "*.txt" -exec mv {} {}.bak \;

find . -name "*.txt" -type f -ok mv {} {}.bak\;
< mv ... ./3.txt > ? n
< mv ... ./5.txt > ? n
< mv ... ./1.txt > ? n
< mv ... ./7.txt > ? n
< mv ... ./8.txt > ? n
< mv ... ./10.txt > ? n
< mv ... ./6.txt > ? y
< mv ... ./4.txt > ? n
< mv ... ./9.txt > ? n
< mv ... ./2.txt > ? n
当然 也可以用 xargs 实现上面的功能
1)批量删除
find . -name "*.txt" |xargs rm

2)批量重名名
find . -name "*.txt" |xargs -i mv {} {}.bak
find . -name "*.bak" |xargs -I{} mv {} {}.bak

分享快乐,留住感动。你的点赞,就是我写作的最大的动力.
--于 20170405 23:15 阿常呓语


0 0
原创粉丝点击