Linux随笔08

第九章 文件查找、打包压缩及解压

9.1 文件查找

   echo命令可以查看变量PATH的值

   其值被冒号分隔成7个字段,每个字段都代表一个目录

 

  使用which命令在环境变量PATH设置的目录中查找符合条件的命令文件,可查找其是否存在以及执行的位置

  

  把PATH变量重新定义为/,此时输入任何命令都是从/这一级查找,查找ls命令显示不存在

 

  locate命令可以让用户快速查找到所需要的文件或目录。它不搜索全部数据信息,而是搜索数据库/var/lib/mlocate/mlocate.db。该数据库包含本地系统内所有文件名称及路径。系统会自动创建这个数据库,并且每天更新一次。

  在使用locate命令查找文件时,有时可以找到已经被删除的文件,但新创建的文件却无法查找到,原因是数据库文件没有被系统更新。

  为了避免上述情况,在使用locate命令之前可以先使用updatedb命令手动跟新数据库

[root@localhost suli]# touch suli.txt
[root@localhost suli]# locate suli.txt
[root@localhost suli]# updatedb
[root@localhost suli]# locate suli.txt
/root/suli.txt

 

  与locate命令相比,find命令搜索速度较慢,它并不会索引目录,而是对整个目录进行便利,这会占用很多系统资源。为了提供效率,建议在使用find命令时尽量在指定目录下进行搜索,以缩小查找范围。

  find命令可以根据文件名查找

  eg.在/目录下查找ifcfg-ens33网卡文件

[root@localhost suli]# find / -name "ifcfg-ens33"

 

  添加“i”参数忽略大小写

[root@localhost suli]# find / -iname "ifcfg-ens33"
/etc/sysconfig/network-scripts/ifcfg-ens33

 

   "*"符号为通配符,在不确定文件名称时可以使用

[root@localhost suli]# find / -iname "ifcfg-*"

 

  如果已知文件的大概位置,建议提供可能存在的路径,用关键词逐渐缩小范围,从而提高查找效率。

 

  find命令还可以根据文件大小查找

  eg.在/etc目录下分别查找大于、等于、小于5MiB的文件

[root@localhost suli]# find /etc/ -size +5M
[root@localhost suli]# find /etc/ -size 5M
[root@localhost suli]# find /etc/ -size -5M

  添加“-ls”可以查找文件的详细信息

[root@localhost suli]# find /etc/ -size +5M -ls

 

  find命令在不指定目录的层级时,会逐层地对文件系统进行搜索,查找效率低下。

  通过“-maxdepth”可以指定查找的目录深度

[root@localhost suli]# find / -maxdepth 3 -a -name "ifcfg-en*'
[root@localhost suli]# find / -maxdepth 4 -a -name "ifcfg-en*'
/rtc/sysconfig/network-scripts/ifcfg-ens33

 

  find命令还可以根据时间查找(atime、mtime、ctime)

  eg.查找修改时间距当前时间大于、等于、小于5天的文件,此处的5天是从修改的那一刻计算,共5×24小时,并非日期

[root@localhost suli]# find /etc -mtime +5
[root@localhost suli]# find /etc -mtime 5
[root@localhost suli]# find /etc -mtime -5

 

  find根据文件属主、属组查找

[root@localhost suli]# find /home -user jack  //属主是jack的文件
[root@localhost suli]# find /home -group hr  //属组是hr组的文件
[root@localhost suli]# find /home -user jack -group hr 
[root@localhost suli]# find /home -user jack -a -group hr 
[root@localhost suli]# find /home -user jack -o -group hr 
[root@localhost suli]# find /home -nouser
[root@localhost suli]# find /home -nogroup
[root@localhost suli]# find /home -nouser -o -nogroup

 

  find命令根据文件类型查找

[root@localhost suli]# find /dev -type f     //f普通文件
[root@localhost suli]# find /dev -type d     //d目录文件
[root@localhost suli]# find /dev -type l     //l链接文件
[root@localhost suli]# find /dev -type b     //b块设备文件
[root@localhost suli]# find /dev -type c     //c字符设备文件
[root@localhost suli]# find /dev -type s     //s套接字文件
[root@localhost suli]# find /dev -type p     //p管道文件
  

 

   find命令根据文件权限查找

[root@localhost suli]# find . -perm 644 -ls  //查找权限为644的文件
[root@localhost suli]# find . -perm -644 -ls  //查找权限包含644的文件

 

  find查找包含SUID权限的文件

[root@localhost suli]# find /usr/bin /usr/sbin -perm -4000 -ls

 

  find查找包含SGID权限的文件

[root@localhost suli]# find /usr/bin /usr/sbin -perm -2000 -ls

 

  find命令还可以设置处理动作,默认动作为“-print”

  eg. 查找当前目录下的qf.txt文件并删除

 

 

9.2 文件打包压缩

  文件过大会占用很多硬盘存储空间,在网络传输的过程中也会消耗很长时间。对文件进行打包压缩后,相同容量能够存储更多数据,在网络传输时因数据量的降低而速度更快。

  Linux系统支持的打包压缩命令有很多种,不同的命令所用的压缩技术不同,彼此无法相互解压缩。压缩文件的名称会添加后缀,如.gz、.bz2、.tar.xz、.tar.gz

  eg. 创建目录dir01,在目录下创建20个文件file1~file20

 

  使用gzip命令对dir01目录进行压缩。完成后每个文件都增加了.gz后缀

[root@localhost suli]# gzip dir01/*
[root@localhost suli]# ls dir01/

  gzip命令只对单一的文件进行压缩,对目录压缩也只是分别对文件进行压缩,并不能将多个文件打包为一个大文件

 

  tar命令可以将多个目录与文件打包在一起,同时还可以使用gzip命令对文件进行压缩

  “-c”参数表示创建一个打包文件,“-z”参数表示通过调用gzip对文件进行压缩,“-j”参数表示通过调用bzip2对文件进行压缩,“-J”参数表示通过调用xz对文件进行压缩,"-f"参数表示后面为被处理的文件名称。在Linux系统中并不存在文件扩展名,但为了用户识别分别,创建文件名称应添加后缀。

 

 

9.3 文件解压

   为解压某个压缩文件时,首先需要知道该文件是有何种压缩方式创建出来的,然后用相应的解压方式解压文件。

  当用户不清楚使用何种压缩工具压缩时,可以通过file命令查看文件的压缩信息

 

  在不解压的情况下,使用tar命令也可以查看文件的压缩信息,添加“-t”参数可查看打包文件的文件名

   使用tar命令解压文件,添加“-x”参数表示解打包或者解压缩,“-C”参数表示解压至指定目录。解压比较大的文件需要消耗较长的时间,添加“-v”参数可以显示解压过程。

  eg. 将etcl-gzip.tar.gz解压至/var/tmp目录下

[root@localhost suli]# tar -xvf etc1-gzip.tar.gz -C /var/tmp
[root@localhost suli]# ls /var/tmp
abrt
etc
sssd_is_running

 

  后缀为.zip的压缩文件,直接使用unzip命令解压即可

[root@localhost suli]# unzip xxx.zip

 

posted @   苏梨  阅读(7)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示