find
根据文件的名称或者属性查找文件
语法格式:
find [查找范围] [参数]
参数:
-name : 安装文件的名字查找文件
[root@localhost ~]# find /etc/ -name 'hosts'
/etc/hosts
* : 通配符
案例2:查询/etc目录下名称中包含hosts文件
[root@localhost ~]# find /etc/ -name '*hosts*'
-iname : 按照inde node号码查询文件
-size : 按照文件的大小查询文件
[root@localhost ~]# find ./ -size -5k
+ : 大于
- : 小于
没有符号 : 等于
# 创建一个100M的文件/在根目录可查询到此文件
[root@localhost dev]# dd if=/dev/zero of=100.txt bs=10M count=10
"""
dd : 生成文件
if : 从什么地方读
of : 写入到什么文件
bs : 每次写入多少内容
count : 写入多少次
"""
-mtime : 按照修改时间查询
[root@localhost ~]# find ./ -mtime 2
+(可以省略) : n天之前
[root@localhost ~]# find ./ -mtime -1
- :n天以内
-atime :
-ctime :
-user : 按照用户的属主查询
[root@localhost ~]# find /home -user 1000
-group : 按照用户的属组查询
[root@localhost ~]# find /home -group 1000
-type : 按照文件的类型查询
[root@localhost ~]# find -type f
d : 文件夹
l : 链接文件
s : 套接字文件
p : 管道文件
c : 字符文件
b :磁盘文件
f : 普通文件
-perm : 安装文件的权限查询
[root@localhost ~]# find -perm 644
-inum : 根据index node 号码查询
ll -i 查询node
33575015 -rw-r--r-- 1 root root 4430 12月 20 21:20 100.txt
33574998 -rw-r--r-- 1 root root 100 12月 17 22:15 11.txt
33575005 -rwxr-xr-x 1 root root 32 12月 17 21:58 1.sh
33574991 -rw-r--r-- 1 root root 170 12月 20 21:38 2.txt
34365760 -rw-r--r-- 1 root root 203 12月 20 17:36 a
[root@localhost ~]# find ./ -inum 33575015
./100.txt
[root@localhost ~]#
-a : 并且 (可以省略,默认为并且)
[root@localhost dbus-1]# find /etc/ -maxdepth 4 -name '*conf'
-o : 或者
# 查询目录深度为4的文件 或者 文件conf的文件(若两者条件均满足,均打印,满足单个也打印)
[root@localhost dbus-1]# find /etc/ -maxdepth 4 -o -name '*conf'
-maxdepth: 查询目录的深度(必须放置与第一个参数为)
案例1:查询/etc目录下hosts文件
find列举
# 寻找/etc/ 目录下 hosts的文件
[root@localhost ~]# find /etc/ -name 'hosts'
/etc/hosts
# 这是一个文件,可以vim
[root@localhost ~]# vim /etc/hosts
# 搭配 * 通配符使用
[root@localhost ~]# find /etc/ -name 'hosts*'
/etc/hosts
/etc/hosts.allow
/etc/hosts.deny
[root@localhost ~]# find /etc/ -name '*hosts*'
/etc/selinux/targeted/active/modules/100/denyhosts
/etc/hosts
/etc/hosts.allow
/etc/hosts.deny
[root@localhost ~]# find /etc/ -name '*hosts'
/etc/selinux/targeted/active/modules/100/denyhosts
/etc/hosts
案例3:要求把/etc目录下,所有的普通文件打包压缩到/tmp目录
[root@localhost /tmp]# tar -czPf /tmp/etcv2.tar.gz `find /etc/ -type f | xargs`
知识储备:
| : 前面一个命令的结果交给后面一个命令处理
xargs : 把处理的文本变成以空格来分割的一行
'' : 优先执行 将结果交给其他命令处理