Linux系统配置及服务管理第08章文件查找

一.文件查找

简介:which :命令查找

           find: 文件查找,针对文件名

            locate:文件查找,依赖数据库

1、命令文件查找

查找lvim命令的位置:[[email protected] ~]# which ivim  

Linux系统配置及服务管理第08章文件查找

2、任意文件

语法:find [path...] [options]  [expression] [action]
       命令 路径       选项             表达式        动作

1.按文件名:

[[email protected] ~]# find /etc/ -name "hosts"
Linux系统配置及服务管理第08章文件查找

[[email protected] ~]# find /etc/ -iname "hosts"
Linux系统配置及服务管理第08章文件查找

[[email protected] ~]# find /etc/ -iname "hos*"

Linux系统配置及服务管理第08章文件查找

-i忽略大小写

2.按文件大小

[[email protected] ~]# find /etc/ -size +5M    //文件>5M

[[email protected] ~]# find /etc/ -size 5M   //文件=5M

[[email protected] ~]# find /etc/ -size -5M    // 文件<5M  

3.指定查找的目录深度

可查找范围:[[email protected] ~]# find / -maxdepth 4 -a -name "ifcfg-en*"
Linux系统配置及服务管理第08章文件查找

不可查找范围: [[email protected] ~]# find / -maxdepth 3 -a -name "ifcfg-en*"

Linux系统配置及服务管理第08章文件查找

4.按文件属主、属组找:

请注意,查找的用户和组要提前创建好。

[[email protected] ~]# find /home -user jack   //属主
Linux系统配置及服务管理第08章文件查找

[[email protected] ~]# find /home -group hr   //属组是hr组的文件

5.按文件类型:

[[email protected] ~]# find /tmp -type //f普通文件

[[email protected] ~]# find /tmp -type b  //b块设备文件
[[email protected] ~]# find /tmp -type p   //p管道
[[email protected] ~]# find /tmp -type l   //ll连接文件
[[email protected] ~]# find /tmp -type d   //d目录

6.按文件权限:

[[email protected] ~]# find . -perm 644 -ls
Linux系统配置及服务管理第08章文件查找

  7.找到后处理的动作 ACTIONS:

找到后默认是显示文件

[[email protected] ~]# find . -perm 644 -print     //显示文件名
Linux系统配置及服务管理第08章文件查找

找到后删除

[[email protected] ~]# find /etc -name "775" -delete
找到后复制
[[email protected] ~]# find /etc -name "ifcfg*" -ok cp -rvf {} /tmp \;
 

二.文件打包及压缩

简介:tar命令是Unix/Linux系统中备份文件的可靠方法,
几乎可以工作于任何环境中,它的使用权限是所有用户。
建议针对目录

1.打包,压缩

语法:tar  选项  压缩包名称  源文件

打包,压缩

[[email protected] ~]# tar -cf etc.tar /etc   //压缩 /etc
Linux系统配置及服务管理第08章文件查找

解压  :[[email protected] ~]# tar -xf etc.tar
Linux系统配置及服务管理第08章文件查找

[[email protected] ~]# tar -czvf etc-gzip.tar.gz  /etc/       //z是gzip

[[email protected] ~]# tar -cjf etc-bzip.tar.bz /etc/        //j是bzip
[[email protected] ~]# tar -cjf etc-bzip.tar.xz /etc/      //J是xzip

观察三个包的体积

[[email protected] ~]# ll -h etc*
Linux系统配置及服务管理第08章文件查找

压缩速度和压缩体积成反比。

2.解压,解包

查看,并没有解压

# tar -tf       etc.tar      //t查看f文件名

解压缩

[[email protected] ~]# tar xf etc-bzip.tar.xz   //简单粗暴

tar -xvf etc-bzip.tar.bz -C /tmp      //-C重定向到//tmp目录