关于find命令 的使用


1.参考博客
https://www.cnblogs.com/yorkyang/p/6294894.html

2.liunx 的/tmp目录的作用
/tmp是临时存储系统的缓存文件,是公用的临时文件存储点

find 使用正则表达式

  1. 创建两个文件
    [root@localhost /]# cd /opt/
    [root@localhost opt]# ls
    rz.log
    [root@localhost opt]# touch a.sh
    [root@localhost opt]# touch b.sh
    [root@localhost opt]# ls
    a.sh b.sh rz.log
  2. 查看文件
    [root@localhost ~]# find /opt/ -name “[ab].sh”
    /opt/a.sh
    /opt/b.sh
  3. 删除文件
    [root@localhost ~]# find /opt/ -name “[ab].sh” -ok rm -rf {} ;
    < rm … /opt/a.sh > ? y
    < rm … /opt/b.sh > ? y
    [root@localhost ~]# ls /opt/
    rz.log

区分大写小写,查找文件 加-iname 不区分大小写

[root@localhost ~]# find /opt/ -name “[ab].sh”
/opt/a.sh
/opt/b.sh
[root@localhost ~]# find /opt/ -iname “[ab].sh”
/opt/A.sh
/opt/a.sh
/opt/b.sh
[root@localhost ~]# find /opt/ -iname “[ab].sh” -exec rm -rf {} ;

使用通配符

[root@localhost ~]# find /etc -name “passwd
/etc/passwd
/etc/passwd-
/etc/pam.d/passwd
/etc/security/opasswd

*是通配所有的 通配任意字符,可以寻找到目录层

[root@localhost ~]# find /etc/ -name “passwd?”
/etc/passwd-

?是通配单个的任意字符

2. stat 查看文件的时间信息

[root@localhost opt]# stat rz.log
文件:“rz.log”
大小:5716 块:16 IO 块:4096 普通文件
设备:fd00h/64768d Inode:16777744 硬链接:1
权限:(0644/-rw-r–r--) Uid:( 0/ root) Gid:( 0/ root)
最近访问:2018-12-11 22:48:31.745098474 +0800
最近更改:2018-12-11 22:47:23.688100102 +0800
最近改动:2018-12-11 22:47:42.626099649 +0800
创建时间:-

2.1 find 可以结合时间进行查找文件

#-atime

       #-mtime

       #-ctime

       #-amin

       #-mmin

       #-cmin

所以这里atime,mtime,ctime就是分别对应的“最近一次访问时间”“最近一次内容修改时间”“最近一次属性修改时间”,这里的atime的单位指的是“天”,amin的单位是分钟

五天内没有访问过的文件

[root@localhost home]# find /home/ -atime +5

五天内访问过的文件

[root@localhost home]# find /home/ -atime -5

其余都是以此类推吧
参考博客

http://blog.chinaunix.net/uid-24648486-id-2998767
find命令 — 查找指定时间内修改过的文件

https://blog.csdn.net/oWangChen1234567/article/details/78640561
find 查找中的众多案例
https://blog.csdn.net/qq_25992179/article/details/88386248

3.find还可以使用多组条件查询

3.1 -a 连接两个不同的条件(两个条件必须同时满足)

案例演示
  1. 创建测试文件并查询文件
    [root@localhost opt]# find /opt/ -iname “*.sh” -a -user root
    /opt/A.sh
    /opt/a.sh
    /opt/b.sh
  2. 改变一个文件的所属组和所属着
    [root@localhost opt]# chown tang:tang a.sh
    [root@localhost opt]# ll
    总用量 8
    -rw-r–r-- 1 tang tang 0 12月 21 17:36 a.sh
    -rw-r–r-- 1 root root 0 12月 21 17:36 A.sh
    -rw-r–r-- 1 root root 0 12月 21 17:36 b.sh
    -rw-r–r-- 1 root root 5716 12月 11 22:47 rz.log
  3. 再一次查找文件
    [root@localhost opt]# find /opt/ -iname “*.sh” -a -user root
    /opt/A.sh
    /opt/b.sh

-o 连接两个不同的条件(两个条件满足其一即可)

案例演示

[root@localhost opt]# find /opt/ -iname “*.sh” -o -user root
/opt/
/opt/rz.log
/opt/A.sh
/opt/a.sh
/opt/b.sh

-not 是取反的意思

案例演示

[root@localhost opt]# find /opt/ -not -user root
/opt/a.sh

4.find还可以按照用户权限来查找文件
4.1查找系统没有所属组和所属者的文件

[root@localhost /]# find / -nogroup -a -nouser
find: ‘/proc/13267/task/13267/fd/6’: 没有那个文件或目录
find: ‘/proc/13267/task/13267/fdinfo/6’: 没有那个文件或目录
find: ‘/proc/13267/fd/6’: 没有那个文件或目录
find: ‘/proc/13267/fdinfo/6’: 没有那个文件或目录
//(这样的文件通常是很危险的,作为系统工程师的我们应该及时清除掉)

5.[查找完执行的action]
     # -print                                 //默认情况下的动作

     # -ls                                     //查找到后用ls 显示出来

     # -ok  [commend]                //查找后执行命令的时候询问用户是否要执行

    # -exec [commend]              //查找后执行命令的时候不询问用户,直接执行
案例演示(批量修改权限)
  1. 没有修改权限前
    [root@localhost opt]# ll
    总用量 8
    -rw-r–r-- 1 tang tang 0 12月 21 17:36 a.sh
    -rw-r–r-- 1 root root 0 12月 21 17:36 A.sh
    -rw-r–r-- 1 root root 0 12月 21 18:08 awk
    -rw-r–r-- 1 root root 0 12月 21 17:36 b.sh
    drwxr-xr-x 2 root root 6 12月 21 18:10 king
    -rw-r–r-- 1 root root 5716 12月 11 22:47 rz.log
  2. 批量修改权限
    [root@localhost opt]# find /opt/ -iname “*.sh” -exec chmod u+x {} ;
  3. 修改权限后
    [root@localhost opt]# ls -l *.sh
    -rwxr–r-- 1 tang tang 0 12月 21 17:36 a.sh
    -rwxr–r-- 1 root root 0 12月 21 17:36 A.sh
    -rwxr–r-- 1 root root 0 12月 21 17:36 b.sh
6.按文件类型进行查找文件

-type

                  f     // 普通文件

                  d     //目录文件

                  l     //链接文件

                  b     //块设备文件

                  c     //字符设备文件

                  p     //管道文件

                  s     //socket文件
案例演示

[root@localhost home]# find /home/ -type d
[root@localhost home]# find /home/ -type s

在这里插入图片描述

find /opt/aspire/product/mmcs/files/mdo/success -name “CSVCGET_0001*” -a -name "20190106_001" -ok mv /opt/aspire/product/mmcs/tang {};
这命令那里错了
mmcs@gd-mbcsap:/opt/aspire/product/mmcs/tang $ zip -r tang.gz ./

7.利用find命令进行查找隐藏文件

[root@localhost ~]# find /root -iname “.*” -print
/root/.bash_logout
/root/.bash_profile
/root/.cshrc
/root/.tcshrc
/root/.bash_history
/root/.cache
/root/.config
/root/.bashrc
/root/.viminfo

8.这个命令我之前没有用过,有时间研究一下

find /opt/aspire/product/modps/dump/ |grep .log -mtime +10 |xargs rm -f
find /opt/aspire/product/modps/redis/backup/ -mtime +1 |grep .gz |xargs rm -f
参考博文:
https://www.cnblogs.com/mafeng/p/6766800.html

9.现实生产环境,使用的find 命令查找

  • 1.新建一个目录:

  • 2.用find命令进行过滤 并把响应的文件复制到新目录中
    find . -type f -mtime -1 -exec cp {} /opt/aspire/product/mmcs/tang ;

  • 3.进入到新目录中,查看目录的大少,通过一天的量来,来计算一年的量
    du -sh . (这种方法可以用来计算日志目录,给日志目录进行定容)

10.针对上一个问题,计划使用一下用awk 直接统计出来,如果有的话马上公布出来

[root@king shell]# find .  -name "*.sh"  | ll
总用量 36
-rw-r--r--  1 root root 428 1月  25 16:47 2.txt
-rwxr-xr-x  1 root root 186 1月  10 14:36 cjyh.sh
-rwxr-xr-x. 1 root root  31 1月   9 00:38 fw.sh
-rwxr-xr-x. 1 root root 136 1月   7 22:40 ip.sh
-rwxr-xr-x. 1 root root 343 1月   7 23:07 js.sh
-rwxr-xr-x. 1 root root 312 1月   9 00:15 kefu.sh
-rwxr-xr-x  1 root root 225 1月  10 15:19 scyh.sh
-rwxr-xr-x  1 root root 356 1月  10 16:55 yhtj2.sh
-rwxr-xr-x  1 root root 537 1月  10 16:54 yhtj.sh

[root@king shell]# find .  -name "*.sh"  | ll |  awk '{sum+=$5} END {print "total = ", sum}'
total =  2554

12.针对这个文件大少统计,截取端日志信息,好好玩玩(这个是来自网上的)

[root@king shell]# cat 2.txt
file_numb: 3168; total_size: 105.801 GB;avg_size: 34.1983 MB
file_numb: 3154; total_size: 87.16 GB;avg_size: 28.298 MB
file_numb: 348; total_size: 140.047 GB;avg_size: 412.091 MB
file_numb: 306; total_size: 184.478 GB;avg_size: 617.339 MB
file_numb: 3168; total_size: 371.135 GB;avg_size: 119.963 MB
file_numb: 2427; total_size: 0.00790029 GB;avg_size: 0.00333329 MB
file_numb: 3146; total_size: 138.763 GB;avg_size: 45.1663 MB

  • 这里一共介绍了三个语法格式:
[root@king shell]# awk -F ";" '{ gsub("total_size:","", $2); gsub("GB", "", $2); total+=$2} END { print total}' 2.txt 
1027.39
[root@king shell]# awk '{sum+=$4} END {print "total = ", sum}' 2.txt
total =  1027.39
[root@king shell]# awk 'BEGIN{total=0} { total+=$4} END {print total}' 2.txt
1027.39

这个是参考博文:
https://www.v2ex.com/t/233306

13. 使用find命令把过完日志进行打包和删除

find /opt/aspire/product/mopush/*tomcat*/bin/servers/moxpps/log/bossquery/*.bak   -mtime +1| xargs gzip
find /opt/aspire/product/mopush/*tomcat*/bin/servers/moxpps/log/bossquery/*   -mtime +20|xargs rm -f

find /opt/aspire/product/mopush/apache/ReportLogs/ -mtime +1|grep -v gz |xargs gzip 

find /opt/aspire/product/mopush/*tomcat*/bin/servers/moxpps/log/appupgrade/ -type f        -mtime +3 |xargs rm -f

find /opt/aspire/product/mopush/*tomcat*/bin/servers/moxpps/log/error/*            -name "*.bak"| gzip -f

  • 大概就是这个类型的:
    从而内推吧!!!

14.实际生产环境下的find 使用

想把 16年和17你的日志信息 ,移动到其他的,目录 ,从而使数据过滤更加方便

  • 当前目录并创建一个备份目录
/opt/aspire/product/clientinf/apache-tomcat-6.0.35/bin/moClientUpload/log/flowsearch
创建一个备份目录
/opt/aspire/product/clientinf/apache-tomcat-6.0.35/bin/moClientUpload/log/flowsearch.bak

  • 查看一下 ,有多少日志信息
ls | wc -l
25104
  • 把日志信息移动到新的目录里,并查看一下,还有多少日志信息
find . -name "flowsearch.log.2018*"  -exec mv {} /opt/aspire/product/clientinf/apache-tomcat-6.0.35/bin/moClientUpload/log/flowsearch.bak \;
[clientinf@gd-mmdls05 flowsearch]$ ls | wc -l
1837

15.指定搜索深度。打印当前目录的文件

  • (深度为1)

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

15 find 安装文件的大少进行查找

  • find 安装文件的大少进行查看也是一个非常重要的查找手段
[root@king ~]# ls
anaconda-ks.cfg             houraccess_Guangdong_2019-03-04.txt  mmwiard.log.2019030519.bak  shell       公共  视频  文档  音乐    桌面
elasticsearch-6.3.2.tar.gz  jdk-8u162-linux-x64.tar.gz           python                      测试文件夹  模板  图片  下载  造文件
[root@king ~]# find . -type f -size +2M
./jdk-8u162-linux-x64.tar.gz
./mmwiard.log.2019030519.bak
./elasticsearch-6.3.2.tar.gz

16、使用find命令查找文件,并删除文件

./award.log.2018072006.bak
./award.log.2018081219.bak
./award.log.2018031412.bak
./award.log.2018071717.bak
./award.log.20180324.999999
./award.log.2018061900.bak
./award.log.2018072410.bak
./award.log.2018031218.bak
mssp3@nj-hyjfxt03:/opt/aspire/product/mssp3/tomcat/bin/mssp3/log/award > find . -name "*.2018*"  -exec rm -rf {} \;
mssp3@nj-hyjfxt03:/opt/aspire/product/mssp3/tomcat/bin/mssp3/log/award > ls
award.log

find找出所有文件的,权限并进行修改为755

find ./ -name *.sh  | xargs  chmod 755 {} \;`
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

运维螺丝钉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值