加入收藏 | 设为首页 | 会员中心 | 我要投稿 云计算网_梅州站长网 (https://www.0753zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 服务器 > 搭建环境 > Linux > 正文

详解 Linux文件查找、打包压缩

发布时间:2022-10-13 12:51:05 所属栏目:Linux 来源:
导读:  Linux文件查找、打包压缩详解文件查找1,简介

  grep:文件内容过滤

  find:文件查找,针对文件名;locate:文件查询,依赖数据库

  which:命令查询

  2,命令文件查找

  查找

  Linux文件查找、打包压缩详解文件查找1,简介
 
  grep:文件内容过滤
 
  find:文件查找,针对文件名;locate:文件查询,依赖数据库
 
  which:命令查询
 
  2,命令文件查找
 
  查找ls 命令的位置 # which ls //从PATH环境变量 或者 # whereis vim
 
  一.文件查找工具locate和find
 
  1.locate 数据库:/var/lib/mlocate/mlocate.db
 
  locate [OPTION]... PATTERN...
      -i 不区分大小写的搜索
      -n  N 只列举前N个匹配项目
      -r  使用基本正则表达式
  示例
      搜索名称或路径中带有“conf”的文件
          locate  conf
      使用Regex来搜索以“.conf”结尾的文件
          locate  -r  ‘\.conf$’
  注意:
 
  在mnt目录中的文件无法用locate搜索。 因为配置文件中有排除查找路径。 /etc/updatedb.conf 第一行PRUNE_BIND_MOUNTS="yes"的意思是:是否进行限制搜索。 第二行是排除检索的文件系统类型,即列出的文件系统类型不进行检索。 第二行表示对哪些后缀的文件排除检索linux 压缩,也就是列在这里面的后缀的文件跳过不进行检索。不同后缀之间用空格隔开。 第四行是排除检索的路径,即列出的路径下的文件和子文件夹均跳过不进行检索。updatedb之后使用locate仍然找不到想要文件
 
  2.find
 
  工作特点:
      ? 查找速度略慢
      ? 精确查找
      ? 实时查找
      ? 可能只搜索用户具备读取和执行权限的目录
  find [OPTION]... [查找路径] [查找条件] [处理动作]
  2.1查找路径:指定具体目标路径;默认为当前目录
 
  指定的查找标准,可以文件名、大小、类型、权限等标准进行;默认为找出指定路径下的所有文件
 
  按文件名
 
  [root@tianyun ~]# find /etc -name "ifcfg-ens32"
 
  [root@tianyun ~]# find /etc -iname "ifcfg-ens32" //-i忽略大小写
 
  [root@tianyun ~]# find /etc -iname "ifcfg-ens*"
 
  按文件大小
 
  [root@tianyun ~]# find /etc -size +5M //大于5M
 
  [root@tianyun ~]# find /etc -size 5M //等于5M
 
  [root@tianyun ~]# find /etc -size -5M //小于5M
 
  [root@tianyun ~]# find /etc -size +5M -ls //-ls找到的处理动作
 
  指定查找的目录深度
 
  -maxdepth levels
 
  -mindepth levels
 
  [root@tianyun ~]# find / -maxdepth 3 -a -name "ifcfg-en"
 
  [root@tianyun ~]# find / -maxdepth 4 -a -name "ifcfg-en"
 
  按时间找(atime,mtime,ctime)
 
  时间的概念
 
  atime:(access time)显示的是文件中的数据最后被访问的时间,比如系统的进程直接使用或通过一些命令和脚本间接使用。(执行一些可执行文件或脚本)
 
  mtime: (modify time)显示的是文件内容被修改的最后时间,比如用vi编辑时就会被改变。(也就是Block的内容)
 
  ctime: (change time)显示的是文件的权限、拥有者、所属的组、链接数发生改变时的时间。当然当内容改变时也会随之改变(即inode内容发生改变和Block内容发生改变时)* *
 
  *案例*
 
  按时间找(atime,mtime,ctime):
 
  [root@tianyun ~]# find /etc -mtime +5 //修改时间超过5天
 
  [root@tianyun ~]# find /etc -mtime 5 //修改时间等于5天
 
  [root@tianyun ~]# find /etc -mtime -5 //修改时间5天以内
 
  按文件属性、属组找
 
  [root@tianyun ~]# find /home -user jack //属主是jack的文件
 
  [root@tianyun ~]# find /home -group hr //属组是hr组的文件
 
  [root@tianyun ~]# find /home -user jack -group hr //和
 
  [root@tianyun ~]# find /home -user jack -a -group hr //-a和
 
  [root@tianyun ~]# find /home -user jack -o -group hr //-o或
 
  [root@tianyun ~]# find /home -nouser
 
  [root@tianyun ~]# find /home -nogroup
 
  [root@tianyun ~]# find /home -nouser -o -nogroup
 
  按文件类型(了解)
 
  [root@tianyun ~]# find /dev -type f //f普通
 
  [root@tianyun ~]# find /dev -type d //d目录
 
  [root@tianyun ~]# find /dev -type l //l链接
 
  [root@tianyun ~]# find /dev -type b //b块设备
 
  [root@tianyun ~]# find /dev -type c //c字符设备
 
  [root@tianyun ~]# find /dev -type s //s套接字
 
  [root@tianyun ~]# find /dev -type p //p管道文件
 
  按文件权限
 
  普通权限
 
  [root@tianyun ~]# find . -perm 644 -ls //精确权限
 
  [root@tianyun ~]# find . -perm -644 -ls //包含权限即可***
 
  *特殊权限*
 
  [root@tianyun ~]# find /usr/bin /usr/sbin -perm -4000 -ls //包含set uid
 
  [root@tianyun ~]# find /usr/bin /usr/sbin -perm -2000 -ls //包含set gid
 
  [root@tianyun ~]# find /usr/bin /usr/sbin -perm -1000 -ls //包含sticky
 
  按正则表达式(了解)
 
  -regex pattern
 
  [root@tianyun ~]# find /etc/ -regex '.*ifcfg-ens3[0-9]' [0-9] 任意一个数字
 
  找到后处理的动作ACTIONS
 
  类型
 
  -print 打印,
 
  默认选项
 
  -ls
 
  -delete
 
  -exec 后面跟自定义的shell命令
 
  -ok 后面跟自定义的shell命令***
 
  *示例*
 
  # find /etc -name "ifcfg"
 
  # find /etc -name "ifcfg" -print
 
  # find /etc -name "ifcfg" -ls
 
  # find /etc -name "775" -delete /775.txt是自定义文件
 
  # find /etc -name "ifcfg" -exec cp -rvf {} /tmp \; //不提示
 
  # find /etc -name "ifcfg" -ok cp -rvf {} /tmp \; //提示
 
  # find /etc -name "775*" -exec rm -rf {} \;
 
  处理动作:对符合条件的文件做操作,默认输出至屏幕
 
  -print:默认的处理动作,显示至屏幕
  -ls:类似于对查找到的文件执行“ls -l”命令
  -delete:删除查找到的文件
  -fls file:查找到的所有文件的长格式信息保存至指定文件中
  -ok COMMAND {} \; 对查找到的每个文件执行由COMMAND指定的命令,对于每个文件执行命令之前,都会交互式要求用户确认
  -exec COMMAND {} \; 对查找到的每个文件执行由COMMAND指定的命令  
                {}: 用于引用查找到的文件名称自身
  find传递查找到的文件至后面指定的命令时,查找到所有符合条件的文件一次性传递给后面的命令
  2.4由于很多命令不支持管道|来传递参数,xargs用于产生某个命令的参数,xargs
 
  find和xargs的组合:find | xargs COMMAND
  示例:
      ls  | xargs   rm  
      删除当前目录下的大量文件
  [root@tianyun ~]#find /sbin/ -perm +700 | ls -l       这个命令是错误的
  [root@tianyun ~]# find /bin/ -perm /7000 | xargs ls -Sl  查找有特殊权限的文件,并排序
  [root@tianyun ~]# find /bin/ -perm -7000 | xargs ls -Sl  此命令和上面有何区别?  
  [root@tianyun ~]# find -type f -name “*.txt” -print0 | xargs -0 rm 以字符nul分隔
  [root@tianyun ~]# find . -name "yang*.txt" |xargs rm -rf
  [root@tianyun ~]# find /etc -name "ifcfg-eth0" |xargs -I {} cp -rf {} /var/tmp
  二,文件打包及压缩1,简介
 
  tar命令是Unix/Linux系统中备份文件的可靠方法,几乎可以工作于任何环境中,它的使用权限是所有用户。 建议针对目录
 
  2,打包,压缩
 
  语法:tar 选项 压缩包名称 源文件
 
  ===打包,压缩===
 
  # tar -czf etc-gzip.tar.gz /etc///z是gzip
 
  # tar -cjf etc-bzip.tar.bz /etc///j是bzip
 
  # tar -cJf etc-xzip.tar.xz /etc///J是xzip 观察三个包的体积。
 
  # ll -h etc*
 
  -rw-r--r--. 1 root root 11M 10月 14 10:07 etc-gzip.tar.gz
 
  -rw-r--r--. 1 root root 8.9M 10月 14 10:08 etc-bzip.tar.bz
 
  -rw-r--r--. 1 root root 7.6M 10月 14 10:08 etc-xzip.tar.xz
 
  压缩速度和压缩体积成反比。 file etc.tar.gz
 
  3,压缩,解包
 
  ===解压,解包===
 
  # tar -tf sys.tar.xz//t查看f文件名
 
  # tar -xzvf etc1.tar.gz //x解压z调gzip,还可以自动判断
 
  # tar -xvf etc1.tar.gz //无需指定解压工具,tar会自动判断
 
  # tar -xvf etc2.tar.bz2 -C /tmp //-C重定向到//tmp目录 终极大法:
 
  # tar xf etc3.tar.xz //简单粗暴
 

(编辑:云计算网_梅州站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!