Linux系统文件查找
一、文件查找
1.简介
- which:命令查找
[root@localhost ~]# which ln
/usr/bin/ln
- find: 文件查找,针对文件名
[root@localhost ~]# find 1.txt
1.txt
- locate:文件查找,依赖数据库,开机之前的能找到。开机之后创建的文件,可以用updatedb刷新再查找。
[root@localhost ~]# locate 1.txt
/root/1.txt
2.命令文件查找
[root@localhost ~]# which ls
[root@localhost ~]# whereis ls
3.任意文件
3.1语法
find [path…] [options] [expression] [action]
命令 路径 选项 表达式 动作
3.2按文件名
[root@localhost ~]# find /etc -name "hosts"
/etc/hosts
/etc/avahi/hosts
[root@localhost ~]# find /etc -iname "hosts" #-i不区分发大小写
/etc/hosts
/etc/avahi/hosts
[root@localhost ~]# find /etc -iname "host*"
3.3按文件大小
[root@localhost ~]# find /etc -size +5M #文件>5M
/etc/udev/hwdb.bin
[root@localhost ~]# find /etc -size 5M #文件=5M
[root@localhost ~]# find /etc -size -5M #文件<5M
3.4指定查找的目录深度
- 可查找范围
[root@localhost ~]# find / -maxdepth 4 -a -name "ifcfg-en*" #-a为多加个选项
/etc/sysconfig/network-scripts/ifcfg-ens33
- 不可查找范围
[root@localhost ~]# find / -maxdepth 3 -a -name "ifcfg-en*"
#失败文件在4级目录下,只搜索3级查找不到
3.5按文件属主、属组找
#查找的用户和组要是提前创建好的
[root@localhost ~]# find /home -user u1 #属主是u1的文件
[root@localhost ~]# find /home -group hr #属组是hr的文件
3.6按文件类型
[root@localhost ~]# find /tmp -type f
[root@localhost ~]# find /tmp -type b
#
f普通文件
b块设备文件
d目录
p管道
l连接
3.7按文件权限
[root@localhost ~]# find . -perm 644 -ls
#
-ls 是find的动作之一,精确权限
3.8找到后处理的动作ACTIONS
- 找到后默认是显示文件
[root@localhost ~]# find . -perm 715 -print #短格式,文件名
[root@localhost ~]# find . -perm 715 -ls #长格式,文件属性
- 找到后删除
[root@localhost ~]# find /etc -name "775*" -delete
- 找到后复制
[root@localhost ~]# find /etc -name "ifcfg*" -ok cp -rvf {} /tmp \;
二、文件打包及压缩
1.简介
tar命令是Unix/Linux系统中备份文件的可靠方法,几乎可以工作于任何环境中,它的使用权限是所有用户。建议针对目录。
2.打包,压缩
- 语法
tar 选项 压缩包名称 源文件 - 打包,压缩
[root@localhost ~]# tar -cf etc.tar /etc #打包
tar: 从成员名中删除开头的“/”
[root@localhost ~]# tar -czf etc.tar.gz /etc #压缩,z是gzip
tar: 从成员名中删除开头的“/”
[root@localhost ~]# tar -cjf etc.tar.bz /etc #压缩,j是bzip
tar: 从成员名中删除开头的“/”
[root@localhost ~]# tar -cJf etc.tar.xz /etc #压缩,J是xzip
tar: 从成员名中删除开头的“/”
- 观察三个包的体积
[root@localhost ~]# ll -h etc*
-rw-r--r--. 1 root root 37M 8月 4 11:38 etc.tar
-rw-r--r--. 1 root root 11M 8月 4 11:40 etc.tar.bz
-rw-r--r--. 1 root root 12M 8月 4 11:39 etc.tar.gz
-rw-r--r--. 1 root root 8.2M 8月 4 11:40 etc.tar.xz
3.解压,解包
- 查看,但不解压
[root@localhost ~]# tar -tf etc.tar #t是查看,f是文件名
- 解压缩
[root@localhost ~]# tar -xf etc.tar.gz
[root@localhost ~]# tar -xvf etc.tar.gz -C /tmp #重定向到/tmp目录