本文开始学习Linux文件搜索命令。
Linux上搜索命令有很多,本文主要记录几个常用的命令。
{collapse-item label="find
文件搜索"}
- 执行权限:所有用户
- 语法:
find [搜索范围] [匹配条件]
按文件名搜索 -name
-iname
-iname
不区分大小写
搜索名为init.d的文件
root@DESKTOP:~# find /etc -name init.d
/etc/init.d
{/tabs-pane}
{tabs-pane label="模糊搜索"}
搜索文件名以init四个字符开头的文件
root@DESKTOP:~# find /etc -name init*
/etc/init.d
搜索文件名含有init四个连续字符的文件
root@DESKTOP:~# find /etc -name *init*
/etc/systemd/system/sysinit.target.wants
/etc/init.d
/etc/security/namespace.init
搜索文件名为init+任意2个字符的文件
root@DESKTOP:~# find /etc -name init??
/etc/init.d
{/tabs-pane}
按文件大小搜索 -size
- 计量单位是数据块,一数据块=512字节,即0.5Kb
- +n 大于,-n小于,n等于
- 在/bin搜索大于1MB的文件 =1024KB=2048数据块
root@DESKTOP:~# find /bin -size +2048
/bin/bash
/bin/systemctl
/bin/udevadm
{/tabs-pane}
{tabs-pane label="组合条件"}
- 在/bin搜索大于500KB且小于1MB的文件
root@DESKTOP:~# find /bin -size +1024 -a -size -2048
/bin/tar
/bin/ip
-a
条件同时满足-o
满足其一即可
{/tabs-pane}
按所有者、所属组搜索 -user
-group
- 在home下搜索所有者为susu的文件
root@DESKTOP:~# find /home -user susu
/home/www
按时间搜索
- 15分钟内访问过(access)的文件
root@DESKTOP:~# find /tmp -amin -15
/tmp/susu.txt
{/tabs-pane}
{tabs-pane label="按文件属性修改"}
- 60分钟内修改过(change)属性的文件
root@DESKTOP:~# find /var/log -cmin -60
/var/log/faillog
/var/log/lastlog
{/tabs-pane}
{tabs-pane label="按文件内容修改"}
- 内容被修改(modify)时间超过10分钟的文件
root@DESKTOP:~# find /var/log -mmin +10
/var/log
/var/log/faillog
/var/log/syslog
/var/log/daemon.log
/var/log/apt
/var/log/apt/term.log
/var/log/apt/history.log
{/tabs-pane}
其它参数
-type
按文件类型查找,可选参数d→目录、f→文件、l→软链接-inum
按i节点查找
比如用来删除一些奇奇怪怪名字的文件
root@DESKTOP:~# find /tmp -inum 368 -ok rm {} \;
< rm ... /tmp/susu.txt > ?
综合运用
搜索1小时内修改过的时间,并列出详细信息
root@DESKTOP:~# find /var/log -mmin -60 -exec ls -l {} \;
-rw-r--r-- 1 root root 32064 May 29 18:44 /var/log/faillog
-rw-rw-r-- 1 root utmp 292584 May 29 18:44 /var/log/lastlog
搜索1小时内修改过的时间,并列出详细信息,每查询一个需要确认一次
root@DESKTOP:~# find /var/log -mmin -60 -ok ls -l {} \;
< ls ... /var/log/faillog > ? y
-rw-r--r-- 1 root root 32064 May 29 18:44 /var/log/faillog
< ls ... /var/log/lastlog > ? n
列出某个文件及其所有硬链接
root@DESKTOP:~# find /tmp -inum 368 -exec ls -li {} \;
368 -rwxrwxrwx 5 root susu 3 May 29 19:19 /tmp/susu.txt.3.bak
368 -rwxrwxrwx 5 root susu 3 May 29 19:19 /tmp/susu.txt
368 -rwxrwxrwx 5 root susu 3 May 29 19:19 /tmp/susu.txt.4.bak
368 -rwxrwxrwx 5 root susu 3 May 29 19:19 /tmp/susu.txt.bak
368 -rwxrwxrwx 5 root susu 3 May 29 19:19 /tmp/susu.txt.2.bak
{/collapse-item}
{collapse-item label="locate
本地索引库内搜索"}
- 执行权限:所有用户
- 语法:
locate 文件名
- 如果提示该命令不存在,需要安装mlocate
- 更新索引库:
updatedb
- 不包括/tmp下的文件
-i
不区分大小写
{/collapse-item}
{collapse-item label="which
搜索命令所在位置及别名"}
- 执行权限:所有用户
- 语法:
which 命令
[root@DESKTOP:~]# which locate
/bin/locate
[root@DESKTOP:~]# which rm
alias rm='rm -i'
/bin/rm
{/collapse-item}
{collapse-item label="whereis
搜索命令及帮助文件所在位置"}
- 执行权限:所有用户
- 语法:
whereis 命令
[root@DESKTOP:~]# whereis locate
locate: /usr/bin/locate /usr/share/man/man1/locate.1.gz
{/collapse-item}
{collapse-item label="grep
在文件中搜索字符串匹配的行并输出"}
- 执行权限:所有用户
- 语法:
grep -iv 指定字符串 文件
-i
不区分大小写-v
排除指定字符串(比如用于查看配置文件已开启的选项)
[root@DESKTOP:~]# grep python /var/log/dnf.log
2022-04-19T19:57:53+0800 DEBUG ---> Package python3-dnf-plugins-core.noarch 4.0.21-3.el8 will be upgraded
2022-04-19T19:57:53+0800 DEBUG ---> Package python3-dnf-plugins-core.noarch 4.0.21-4.el8_5 will be an upgrade
2022-04-19T19:57:53+0800 DEBUG ---> Package python3-rpm.x86_64 4.14.3-19.el8 will be upgraded
2022-04-19T19:57:53+0800 DEBUG ---> Package python3-rpm.x86_64 4.14.3-19.el8_5.2 will be an upgrade
python3-dnf-plugins-core noarch 4.0.21-4.el8_5 baseos 233 k
python3-rpm x86_64 4.14.3-19.el8_5.2 baseos 153 k
2022-04-19T19:58:07+0800 DEBUG Upgraded: python3-dnf-plugins-core-4.0.21-4.el8_5.noarch
[root@DESKTOP:~]# grep -v ^# /etc/yum.conf
[main]
gpgcheck=1
installonly_limit=3
clean_requirements_on_remove=True
best=True
skip_if_unavailable=False
exclude=httpd nginx php mysql mairadb python-psutil python2-psutil
{/collapse-item}
评论 (0)