当前位置: 首页 > news >正文

文件查找和打包压缩【1.7】

文件查找和打包压缩【1.7】

  • 八、文件查找和打包压缩
    • 8.1 文件查找
      • 8.1.1 locate
      • 8.1.2 find
        • find
        • 8.1.2.1 指定搜索目录层级
        • 8.1.2.2 先处理文件再处理目录
        • 8.1.2.3 根据文件名和inode查找
        • 8.1.2.4 根据属主属组查找
        • 8.1.2.5 根据文件类型查找
        • 8.1.2.6 空文件或目录
        • 8.1.2.7 组合条件
        • 8.1.2.8 排除目录
        • 8.1.2.9 根据文件大小来查找
        • 8.1.2.10 根据时间戳
        • 8.1.2.11 根据权限查找
        • 8.1.2.12 正则表达式
        • 8.1.2.13 处理动作
      • 8.1.3 参数替换 xargs
      • xargs 动态生成参数
    • 8.2 压缩和解压缩
      • 8.2.1 compress 和 uncompress
      • 8.2.2 gzip和gunzip
      • 8.2.3 bzip2和bunzip2
      • 8.2.4 xz 和 unxz
      • 8.2.5 zip 和 unzip
      • 8.2.6 zcat
      • 8.2.7 压缩率比较
    • 8.3 打包和解包tar
      • 8.3.1 tar
      • 8.3.2 split
  • endl

八、文件查找和打包压缩

  • 内容概述
    • locate
    • find
    • xargs
    • compress和uncompress
    • gzip和gunzip
    • bzip2和bunzip2
    • xz和unxz
    • zip和unzip
    • tar

8.1 文件查找

  • 在文件系统上查找符合条件的文件
  • 文件查找:
    • 非实时查找(数据库查找):locate
    • 实时查找:find

8.1.1 locate

  • locate 查询系统上预建的文件索引数据库 /var/lib/mlocate/mlocate.db

  • 索引的构建是在系统较为空闲时自动进行(周期任务),执行updatedb可以更新数据库

  • 索引构建过程需要遍历整个根文件系统,很消耗资源

  • locate和update命令来自于mlocate包

  • 工作特点:

    • 查找速度快
    • 模糊查找
    • 非实时查找
    • 搜索的是文件全路径,不仅仅是文件名
    • 可能只搜索用户具备读取和执行权限的目录
  • 如果没有程序,可先安装

#rehl系列
yum install -y mlocate
#ubuntu
apt install -y Plocate
  • 格式
locate [OPTION]... [PATTERN]...
常用选项含义
-A|–all输出所有能匹配到的文件名,不管文件是否存在
-b|–basename仅匹配文件名部份,而不匹配路径中的内容
-c|–count只输出找到的数量
-d|–database DBPATH指定数据库
-e|–existing仅打印当前现有文件的条目
-L|–follow遇到软链接时则跟随软链接去其对应的目标文件中查找 (默认)
-i|–ignore-case忽略大小写
-l|–limit|-n N只显示前N条匹配数据
-P|–nofollow, -H不跟随软链
-r|–regexp REGEXP使用基本正则表达式
–regex使用扩展正则表达式
-s|–stdio忽略向后兼容
-w|–wholename全路径匹配,就是只要在路径里面出现关键字(默认)
  • 范例:locatedb创建数据库
[root@ubuntu2204 ~]# apt install mlocate -y[root@ubuntu2204 ~]# locate conf
/var/lib/plocate/plocate.db: No such file or directory[root@ubuntu2204 ~]# updatedb[root@ubuntu2204 ~]# ll /var/lib/plocate/plocate.db
-rw-r----- 1 root plocate 3142215 May 10 12:17 /var/lib/plocate/plocate.db[root@ubuntu2204 ~]# locate -n 3 conf
/boot/config-5.15.0-25-generic
/boot/config-5.15.0-71-generic
/boot/grub/i386-pc/configfile.mod[root@rocky8 ~]# dnf -y install mlocate
[root@rocky86 ~]# locate conf
locate: can not stat () `/var/lib/mlocate/mlocate.db': No such file or directory[root@rocky8 ~]# updatedb
[root@rocky8 ~]# ll /var/lib/mlocate/mlocate.db
-rw-r----- 1 root slocate 3143608 Jun 18 08:58 /var/lib/mlocate/mlocate.db[root@rocky8 ~]# locate -n 3 conf
/boot/config-4.18.0-348.el8.0.2.x86_64
/boot/grub2/i386-pc/configfile.mod
/boot/loader/entries/c0298860b41f4cd296da0d2853451604-0-rescue.conf
  • 范例:文件新创建和删除,无法马上更新locate数据库
[root@ubuntu2204 ~]# touch test.log
[root@ubuntu2204 ~]# locate test.log#更新数据库之后再查找
[root@ubuntu2204 ~]# updatedb
[root@ubuntu2204 ~]# locate test.log
/root/test.log#文件被删除,还能查到
[root@ubuntu2204 ~]# rm -f test.log
[root@ubuntu2204 ~]# locate test.log
/root/test.log#但实际上文件己经不存在了
[root@ubuntu2204 ~]# stat /root/test.log
stat: cannot statx '/root/test.log': No such file or directory#再次更新数据库
[root@ubuntu2204 ~]# updatedb
[root@ubuntu2204 ~]# locate test.log
  • 范例:
#搜索名称或路径中包含“conf”的文件
[root@ubuntu2204 ~]# locate conf#搜索ect目录中以a开头的文件或目录,路径包含写法
[root@ubuntu2204 ~]# locate /etc/a#仅搜索文件名中包含share 的内容
[root@ubuntu2204 ~]# locate -b share#显示数量
[root@ubuntu2204 ~]# locate -c conf#显示前10[root@ubuntu2204 ~]# locate -n 10 conf#使用基本正则表达式
[root@ubuntu2204 ~]# locate -r '\.conf$'#指定数据库
[root@ubuntu2204 ~]# locate -d /tmp/nofile conf
locate: can not stat () `/tmp/nofile': No such file or directory#安静模式,不输出错误信息,新版本中没有 -q 选项
[root@ubuntu2204 ~]# locate -qd /tmp/nofile conf

8.1.2 find

  • find 是实时查找工具,通过遍历指定路径完成文件查找;
  • 工作特点:
    • 查找速度略慢
    • 精确查找
    • 实时查找
    • 查找条件丰富
    • 可能只搜索用户具备读取和执行权限的目录
  • 格式:
#find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec]
[path...] [expression]find [OPTION]... [查找路径] [查找条件] [处理动作]
  • 查找路径:指定具体目标路径;默认为当前目录

  • 查找条件:指定的查找标准,可以文件名、大小、类型、权限等标准进行;默认为找出指定路径下的所有文件

  • 处理动作:对符合条件的文件做操作,默认输出至屏幕

  • 范例:

  • 默认列出当前目录下的所有文件

find
  • 查找大于100M的文件
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
  • 查找大于120M,小于130M的文件
    在这里插入图片描述
[root@ubuntu2204 ~]# find
.
./dir1
./dir1/dir2
./dir1/dir2/dir3
./dir1/dir2/dir3/fx
./dir1/dir2/dir3/fy
./dir1/dir2/fa
./dir1/dir2/fb
./dir1/f1
./dir1/f2
./fstab
./.issue
8.1.2.1 指定搜索目录层级
-maxdepth N 	#最大搜索目录深度,指定目录下的文件为第1-mindepth N 	#最小搜索目录深度
  • 范例:
#最大搜索深度
[root@ubuntu2204 ~]# find /etc/ -maxdepth 2#最小搜索深度
[root@ubuntu2204 ~]# find /etc/ -mindepth 2#仅搜索第二层目录
[root@ubuntu2204 ~]# find /etc/ -maxdepth 2 -mindepth 2
8.1.2.2 先处理文件再处理目录
-depth #先处理文件
  • 范例:
[root@ubuntu2204 ~]# tree -a
.
├── dir1
│ ├── dir2
│ │ ├── dir3
│ │ │ ├── fx
│ │ │ └── fy
│ │ ├── fa
│ │ └── fb
│ ├── f1
│ └── f2
├── fstab
└── .issue
3 directories, 8 files#默认先显示目录
[root@ubuntu2204 ~]# find
.
./dir1
./dir1/dir2
./dir1/dir2/dir3
./dir1/dir2/dir3/fx
./dir1/dir2/dir3/fy
./dir1/dir2/fa
./dir1/dir2/fb
./dir1/f1
./dir1/f2
./fstab
./.issue#先显示文件
[root@ubuntu2204 ~]# find -depth
./dir1/dir2/dir3/fx
./dir1/dir2/dir3/fy
./dir1/dir2/dir3
./dir1/dir2/fa
./dir1/dir2/fb
./dir1/dir2
./dir1/f1
./dir1/f2
./dir1
./fstab
./.issue
.
8.1.2.3 根据文件名和inode查找
-name name 			#支持使用glob,如:*, ?, [], [^],通配符要加双引号引起来
-iname name 		#不区分字母大小写
-inum number 		#按inode号查找
-samefile name 		#相同inode号的文件
-links n 			#链接数为n的文件
-regex "PATTERN" 	#以PATTERN匹配整个文件路径,而非文件名称
  • 范例:
[root@ubuntu2204 ~]# ls
dir1 fstab test-a.log test-A.log test-a.txt test-A.txt test-b.log test-
B.log test-b.txt test-B.txt#指定文件名查找
[root@ubuntu2204 ~]# find -name test-a.log
./test-a.log#指定文件名,忽略大小写
[root@ubuntu2204 ~]# find -iname test-a.log
./test-a.log
./test-A.log#通配符
[root@ubuntu2204 ~]# find -name "*txt"
./test-a.txt
./test-b.txt
./test-A.txt
./test-B.txt#通配符
[root@ubuntu2204 ~]# find -name "test-a*"
./test-a.log
./test-a.txt#正则表达式
[root@ubuntu2204 ~]# find -regex ".*\.log$"
./test-a.log
./test-b.log
./test-A.log
./test-B.log#正则表达式
[root@ubuntu2204 ~]# find -regex ".*test-[a-z].*"
./test-a.log
./test-a.txt
./test-b.log
./test-b.txt#正则表达式,路径匹配
[root@ubuntu2204 ~]# find -regex ".*dir3.*"
./dir1/dir2/dir3
./dir1/dir2/dir3/fx
./dir1/dir2/dir3/fy#正则表达式,路径匹配
[root@ubuntu2204 ~]# find -regex ".*dir3$"
./dir1/dir2/dir3
8.1.2.4 根据属主属组查找
-user USERNAME 		#查找属主为指定用户(UID)的文件
-group GRPNAME 		#查找属组为指定组(GID)的文件
-uid UserID 		#查找属主为指定的UID号的文件
-gid GroupID 		#查找属组为指定的GID号的文件
-nouser 			#查找没有属主的文件
-nogroup 			#查找没有属组的文件
  • 范例:
[root@ubuntu2204 ~]# ll dir1/
total 0
drwxr-xr-x 3 root root 38 Jul 23 10:21 dir2
-rw-r--r-- 1 123 456 0 Jul 23 10:14 f1
-rw-r--r-- 1 789 root 0 Jul 23 10:14 f2
-rw-r--r-- 1 jose root 0 Jul 23 10:48 fa.txt
-rw-r--r-- 1 jose root 0 Jul 23 10:48 fb.txt
-rw-r--r-- 1 root mage 0 Jul 23 10:48 fc.txt
-rw-r--r-- 1 root mage 0 Jul 23 10:48 fd.txt#指定属主
[root@ubuntu2204 ~]# find -user jose
./dir1/fa.txt
./dir1/fb.txt#指定属主可以用 UID
[root@ubuntu2204 ~]# find -user 1010
./fa.txt
./fb.txt#指定属组
[root@ubuntu2204 ~]# find -group mage
./dir1/fc.txt
./dir1/fd.txt#指定属组可以用GID
[root@ubuntu2204 ~]# find -group 1000
./fc.txt
./fd.txt#指定属主属组
[root@ubuntu2204 ~]# find -user jose -group root
./dir1/fa.txt
./dir1/fb.txt#指定属主ID
[root@ubuntu2204 ~]# find -uid 1010
./dir1/fa.txt
./dir1/fb.txt#指定属组ID
[root@ubuntu2204 ~]# find -gid 456
./dir1/f1#指定属主属组ID
[root@ubuntu2204 ~]# find -uid 1010 -gid 0
./dir1/fa.txt
./dir1/fb.txt#属主用户不存在
[root@ubuntu2204 ~]# find -nouser
./dir1/f1
./dir1/f2#属组不在在
[root@ubuntu2204 ~]# find -nogroup
./dir1/f1#属主属组不存在
[root@ubuntu2204 ~]# find -nouser -nogroup
./dir1/f1
8.1.2.5 根据文件类型查找
-type TYPE 	#指定文件类型#type 值
f 	#普通文件
d 	#目录文件
l 	#符号链接文件
s 	#套接字文件
b 	#块设备文件
c 	#字符设备文件
p 	#管道文件
  • 范例:
#查看当前目录下的所有目录文件
[root@ubuntu2204 ~]# find -type d
.
./dir1
./dir1/dir2
./dir1/dir2/dir3#查找run 目录下所有管道文件
[root@ubuntu2204 ~]# find /run/ -type p
8.1.2.6 空文件或目录
-empty #空文件或空目录
  • 范例:
#空文件或空目录
[root@ubuntu2204 ~]# find dir1/dir2/dir3/ -empty
dir1/dir2/dir3/fx
dir1/dir2/dir3/fy
dir1/dir2/dir3/dir4#查找空目录
[root@ubuntu2204 ~]# find dir1/dir2/dir3/ -empty -type d
dir1/dir2/dir3/dir4#查找空文件
[root@ubuntu2204 ~]# find dir1/dir2/dir3/ -empty -type f
dir1/dir2/dir3/fx
dir1/dir2/dir3/fy
8.1.2.7 组合条件
-a 			#与,多条件默认就是与关系,可省略
-o 			#或
-not|! 	#非
  • 范例:
#默认 -a, 可省略
[root@ubuntu2204 ~]# find -name "*log" -a -type f
./test-a.log
./test-b.log
./test-A.log
./test-B.log#或
[root@ubuntu2204 ~]# find -name "test*log" -o -name "test*txt"
./test-a.log
./test-a.txt
./test-b.log
./test-b.txt#非
[root@ubuntu2204 ~]# find dir1/dir2/dir3/ -empty -not -type d
dir1/dir2/dir3/fx
dir1/dir2/dir3/fy#非
[root@ubuntu2204 ~]# find dir1/dir2/dir3/ -empty ! -type d
dir1/dir2/dir3/fx
dir1/dir2/dir3/fy
  • 范例:配合处理动作
[root@ubuntu2204 ~]# find -user jose -o -name "*log"
./dir1/fa.txt
./dir1/fb.txt
./test-a.log
./test-b.log
./test-A.log
./test-B.log#此处 ls 只列出了后一个条件的匹配
[root@ubuntu2204 ~]# find -user jose -o -name "*log" -ls
138733453 0 -rw-r--r-- 1 root root 0 Jul 23 10:30 ./testa.log
138733455 0 -rw-r--r-- 1 root root 0 Jul 23 10:30 ./testb.log
138733459 0 -rw-r--r-- 1 root root 0 Jul 23 10:30 ./test-A.log
138733461 0 -rw-r--r-- 1 root root 0 Jul 23 10:30 ./test-B.log#把条件括起来才表示全部
[root@ubuntu2204 ~]# find \( -user jose -o -name "*log" \) -ls
202397817 0 -rw-r--r-- 1 jose root 0 Jul 23 10:48 ./dir1/fa.txt
202397821 0 -rw-r--r-- 1 jose root 0 Jul 23 10:48 ./dir1/fb.txt
138733453 0 -rw-r--r-- 1 root root 0 Jul 23 10:30 ./testa.log
138733455 0 -rw-r--r-- 1 root root 0 Jul 23 10:30 ./testb.log
138733459 0 -rw-r--r-- 1 root root 0 Jul 23 10:30 ./test-A.log
138733461 0 -rw-
http://www.lryc.cn/news/444092.html

相关文章:

  • 速盾:cdn一般多长时间清理下缓存?
  • react hooks--useRef
  • GPT对话知识库——将寄存器中的一位数据读到变量中需要什么步骤?C语言中掩码的作用。
  • 【计算机网络】运输层协议解析
  • Redis存储原理
  • PHP、Java等其他语言转Go时选择GoFly快速快速开发框架指南
  • 【MySQL】获取最近7天和最近14天的订单数量,使用MySQL详细写出,使用不同的方法
  • WebView2新增、修改、删除、禁用右键菜单相关操作。
  • 使用vue创建项目
  • Apache CVE-2021-41773 漏洞攻略
  • 【redis-02】深入理解redis中RBD和AOF的持久化
  • 亚马逊IP关联揭秘:发生ip关联如何处理
  • jQuery Mobile 弹窗
  • 【macOS】【zsh报错】zsh: command not found: python
  • NoSql数据库Redis知识点
  • Redis 使用指南
  • c++与cmake:完整的C++项目构建注意事项
  • Linux自主学习篇
  • MQ入门(4)
  • linux下共享内存的3种使用方式
  • 伊丽莎白·赫莉为杂志拍摄一组素颜写真,庆祝自己荣膺全球最性感女人第一名
  • Qt快捷键说明与用法
  • 技术周刊 | TS 5.6、Chrome DevTools 性能面板上新、Vite 6 Beta、Fastify v5、HTTP 新方法 Query
  • 使用Mockito进行单元测试
  • CSS 布局三大样式简单学习
  • 集成运放UA741的原理与应用的探索
  • LeetCode337. 打家劫舍III
  • python基础(二) 包和import
  • 选址模型 | 基于混沌模拟退火粒子群优化算法的电动汽车充电站选址与定容(Matlab)
  • WPF入门教学十 资源与字典