【Linux】find文件查找

1.常用特殊符号的使用

Linux系统下特殊符号起到了很大的作用,特殊符号可以完成一些特殊的功能。

  • * 匹配任意字符

  • { } 匹配多组不同字符

特殊符号应用案例:

#查找以tab结尾的文件
[root@localhost ~]# ls /etc/*tab
[root@localhost ~]# ls /etc/*wd
[root@localhost ~]# ls /etc/*.conf
[root@localhost ~]# ls /etc/redhat*
[root@localhost ~]# ls /etc/*ss*
​
​#查找tty开头结尾为不连续字符结尾
[root@localhost ~]# ls /dev/tty{1,3,5,7,9,15,20,30}
[root@localhost ~]# ls /dev/tty{1..9}
[root@localhost ~]# ls /dev/tty{1..10}
[root@localhost ~]# ls /dev/tty[1-10]

2. du统计文件/目录大小

  • du命令用于统计磁盘下目录或文件的大小

  • 命令格式:du 选项...

  • 常用选项:

  • -h #以人类易读方式(Kb,MB,GB)显示文件大小

  • -s #只统计每个参数的总数


3. find文件/目录查找命令

find 命令根据预设的条件递归查找文件或目录所在位置

  • 命令格式:find 查找路径 查找条件1 查找条件2 .. [-exec 处理命令 {} \; ]

  • –exec 可接额外的命令来处理查找到的结果 → 理解为之前学习过的管道符|

  • { } 代表find查找到的内容被放置在{}中

  • \; 代表额外处理命令的结束

  • 常用查找条件

-type 类型(f文件 d目录 l链接文件)
-name “文件名”
-iname 按文件名查找忽略大小写
-size 文件大小(k、M、G + 大于 - 小于)
-a (and并且)两个条件同时满足
-o (or或者)两个条件满足任意一个即可
-user 用户名
-mtime 按日期查找(+ 代表多少天之前 - 代表多少天之内,0代表24小时之内)

find应用案例:

[root@localhost ~]# ls /var/log
​
#按照类型查找,类型为文件
[root@localhost ~]# find /var/log -type f
[root@localhost ~]# ll boot.log-20210417
[root@localhost ~]# ll /var/log/boot.log-20210417
[root@localhost ~]# ll /var/log/vmware-network.2.log
​
#按照类型查找,类型为目录
[root@localhost ~]# find /var/log -type d
[root@localhost ~]# ll -d /var/log/tuned
[root@localhost ~]# ll -d /var/log/qemu-ga
​
#按照类型查找,类型为链接文件
[root@localhost ~]# find /var/log -type l
[root@localhost ~]# fin /etc/ -type l
[root@localhost ~]# find /etc/ -type l
[root@localhost ~]# ll /etc/scl/conf
​
#按照名字查找
[root@localhost ~]# find /etc/ -name passwd
/etc/passwd
/etc/pam.d/passwd
​
#按照名字查找,类型为文件
[root@localhost ~]# find /etc/ -name passwd -type f
​
#按照名字查找,以tab结尾,类型为文件
[root@localhost ~]# find /etc/ -name '*tab' -type f
​
#按照名字查找,以pass开头,类型为文件
[root@localhost ~]# find /etc/ -name 'pass*' -type f
[root@localhost etc]# find . -name '*.conf' -type f
​
[root@localhost ~]# find /etc/ -name '*tab*' -type f
​
#按照名字忽略大小写查找,类型为文件
[root@localhost ~]# find /etc/ -iname FSTAB -type f
/etc/fstab
[root@localhost ~]# find /etc/ -name FSTAB -type f
​
#查找大于10k的文件
[root@localhost ~]# find /var/log -size +10k -type f
[root@localhost ~]# du -h /var/log/boot.log-20210417
16K /var/log/boot.log-20210417
​
#查找大于1M的文件
[root@localhost ~]# find /var/log -size +1M -type f
[root@localhost ~]# du -h /var/log/audit/audit.log
2.4M    /var/log/audit/audit.log
​
[root@localhost ~]# find /home -size +1M -type f
​
#查找小于1M的文件
[root@localhost ~]# find /var/log -size -1M -type f
[root@localhost ~]# du -h /var/log/spooler
0   /var/log/spooler
​
#查找大于10k并且小于20k,类型为文件
[root@localhost ~]# find /var/log -size +10k -a -size -20k -type f
​
#-o或者,当有多个条件时,满足任意其中一个即可
[root@thinkmo ~]# find /var/log -name "*.log" -o -size -10k -type f
​
#查找属于lisi用户的文件/目录
[root@localhost ~]# find /home -user lisi
​
#查找30天之前被修改过,类型为文件
[root@localhost ~]# find /var/log -mtime +30 -type f
[root@localhost ~]# find /var/log -mtime +10 -type f
​
#查找10天之内被修改过,类型为文件
[root@localhost ~]# find /var/log -mtime -10 -type f
root@localhost ~]# find /var/log -mtime -30 -type f
​
#查找30之前被修改过,类型为文件,拷贝到/opt目录下
[root@localhost ~]# find /var/log -mtime -30 -type f -exec cp {} /opt \;

题型:

  • 查找/etc/目录下以.conf结尾的文件(只能在/etc这一层目录去查找)

ls /etc/*.conf
  • 查找/etc/目录下以.conf结尾的文件(包含所有的子目录)

find /etc/ -name "*.conf"

百度:多查--多查--多查

查找/var/log/messages 文件,清空文件内容,使用find实现

find /var/log/ -name messages -type f -exec cp /dev/null {} \;

查找/var/log以.log结尾的文件,清空文件内容,使用find实现

find /var/log -name *.log -type f -a -mtime +10 -exec cp /dev/null {} \;
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值