Linux学习——文件查找、打包压缩及解压

Linux学习——文件查找、打包压缩及解压

8.1 文件查找

echo $PATH ”——查看变量PATH的值,其被冒号分隔成7个字段,每个字段都代表一个目录

[root@localhost ~]# echo $PATH
/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/root/bin

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

[root@localhost ~]# which useradd
/usr/sbin/useradd
[root@localhost ~]# none
bash: none: 未找到命令...

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

[root@localhost ~]# PATH = /
[root@localhost ~]# ls
bash: ls: 未找到命令...

locate ”——让用户快速查找所需要的文件或目录,它不搜索全部数据信息,而是搜索数据库/var/lib/mlocate/mlocate. db。该数据库包含本地系统内所有文件名称及路径。系统会自动创建这个数据库,并且每天更新一次。
  在使用locate命令查找文件时,有时可以找到已经被删除的文件,但新创建的文件却无法查找到,原因是数据库文件没有被系统更新。为了避免上述情况,在使用locate 命令之前可以先使用updatedb命令手动更新数据库。

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

find ”——搜索速度较慢,它并不会索引目录,而是对整个目录进行遍历,这样会占用很多系统资源,所以一般利用find命令在指定目录下搜索,以缩小查找范围

[root@localhost ~]# find / -name "dir01"
find: ‘/proc/26513’: 没有那个文件或目录
find: ‘/run/user/1000/gvfs’: 权限不够
/root/test/dir01
/usr/dir01
/home/none/dir01
/home/dir01
/date01/dir01

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

[root@localhost ~]# find / -iname "dir01"
find: ‘/proc/61578’: 没有那个文件或目录
find: ‘/run/user/1000/gvfs’: 权限不够
/root/test/dir01
/usr/dir01
/home/none/dir01
/home/dir01
/date01/dir01

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

[root@localhost ~]# find / -iname "dir*"
/proc/sys/fs/dir-notify-enable
/proc/sys/vm/dirty_background_bytes
/proc/sys/vm/dirty_background_ratio
/proc/sys/vm/dirty_bytes
/proc/sys/vm/dirty_expire_centisecs
/proc/sys/vm/dirty_ratio
/proc/sys/vm/dirty_writeback_centisecs
find: ‘/proc/116236’: 没有那个文件或目录
find: ‘/run/user/1000/gvfs’: 权限不够
/sys/fs/selinux/class/dir
/sys/devices/pci0000:00/0000:00:11.0/0000:02:00.0/usb2/2-1/ep_00/direction
/sys/devices/pci0000:00/0000:00:11.0/0000:02:00.0/usb2/2-1/2-1:1.0/ep_81/direction
  • 如果已知文件的大概位置
[root@localhost ~]# find /root /usr /date01 -name "dir*"
/root/test/dir01
/root/test/dir02
/root/test/dir001
/root/test/dir002
/usr/bin/dir
/usr/bin/dircolors
/usr/bin/dirname
  • 根据文件的大小查找
[root@localhost ~]# find /etc/ -size +5M
[root@localhost ~]# find /etc/ -size 5M
[root@localhost ~]# find /etc/ -size -5M
  • 添加“ ls ”可以查看文件的详细信息
[root@localhost ~]# find /etc/ -size +5M -ls
51944562 8804 -r--r--r--   1 root     root      9011349 11月  1 13:45 /etc/udev/hwdb.bin
  • 在不指定目录的层级时,会逐层地对文件系统进行搜索,查找效率低下。通过“ -maxdepth ”可以指定查找的目录深度
[root@localhost ~]# find / -maxdepth 3 -a -name "dir01"
[root@localhost ~]# find / -maxdepth 4 -a -name "dir*"
  • 根据时间(atime、mtime、ctime)。
[root@localhost ~]# find /etc -mtime +5
[root@localhost ~]# find /etc -mtime 5
[root@localhost ~]# find /etc -mtime -5
  • 根据文件的属主、属组
[root@localhost ~]# find /home -user siso
[root@localhost ~]# find /home -group hhh
[root@localhost ~]# find /home -user siso -group hhh
[root@localhost ~]# find /home -user siso -a -group hhh
[root@localhost ~]# find /home -user siso -o -group hhh
[root@localhost ~]# find /home -nouser
[root@localhost ~]# find /home -nogroup
[root@localhost ~]# find /home -nouser -o -nogroup
  • 根据文件类型查找
[root@localhost ~]# find /dev -type f  //f普通文件
[root@localhost ~]# find /dev -type d  //d目录文件
[root@localhost ~]# find /dev -type l  //l链接文件
[root@localhost ~]# find /dev -type b  //b块设备文件
[root@localhost ~]# find /dev -type c  //c字符设备文件
[root@localhost ~]# find /dev -type s  //s套接字文件
[root@localhost ~]# find /dev -type p  //p管道文件
  • 根据文件权限查找,在权限前加“ - ”表示包含
[root@localhost ~]# find . -perm 000 -ls
36669105    0 ----------   1 root     root            0 11月 25 15:00 ./test/dir02/file01.txt
36660724    0 d---------   2 root     root            6 11月 25 20:18 ./test/dir002
980828    0 ----------   1 root     root            0 11月 25 20:18 ./test/file002
[root@localhost ~]# find . -perm -000 -ls
33581121    4 dr-xr-x---  17 root     root         4096 11月 26 22:17 .
35521617    4 -rw-r--r--   1 root     root           18 12月 29  2013 ./.bash_logout
35521618    4 -rw-r--r--   1 root     root          176 12月 29  2013 ./.bash_profile
35521619    4 -rw-r--r--   1 root     root          176 12月 29  2013 ./.bashrc
35521620    4 -rw-r--r--   1 root     root          100 12月 29  2013 ./.cshrc
35521621    4 -rw-r--r--   1 root     root          129 12月 29  2013 ./.tcshrc
  • 查找SUID权限的文件
[root@localhost ~]# find /usr/bin /usr/bin -perm -4000 -ls
50415142   32 -rwsr-xr-x   1 root     root        32096 10月 31  2018 /usr/bin/fusermount
50776863   56 -rwsr-xr-x   1 root     root        54080 8月 20  2019 /usr/bin/cat
50965121   60 -rwsr-xr-x   1 root     root        61320 12月  1  2022 /usr/bin/ksu
51095642   24 -rws--x--x   1 root     root        23968 10月  1  2020 /usr/bin/c
  • 查找SGID权限的文件
[root@localhost ~]# find /usr/bin /usr/bin -perm -2000 -ls
50416235   16 -r-xr-sr-x   1 root     tty         15344 6月 10  2014 /usr/bin/wall
51095735   20 -rwxr-sr-x   1 root     tty         19544 10月  1  2020 /usr/bin/write
51209079  376 ---x--s--x   1 root     nobody     382216 8月  9  2019 /usr/bin/ss
选项 说明
-mount, -xdev 只检查和指定目录在同一个文件系统下的文件,避免列出其它文件系统中的文件
-amin 在过去 n 分钟内被读取过
-anewer file 比文件 file 更晚被读取过的文件
-atime n 在过去 n 天内被读取过的文件
-cmin n 在过去 n 分钟内被修改过
-cnewer file 比文件 file 更新的文件
-ctime n 在过去 n 天内创建的文件
-mtime n 在过去 n 天内修改过的文件
-empty 空的文件
-gid n or -group name : gid 是 n 或是 group 名称是 name
-ipath p, -path p 路径名称符合 p 的文件,ipath 会忽略大小写
-name name, -iname name 文件名称符合 name 的文件。iname 会忽略大小写
-size n 文件大小 是 n 单位
-type c 文件类型是 c 的文件
  • 设置处理动作,默认动作为“ -print ”
[root@localhost ~]# touch qfedu.txt
[root@localhost ~]# find . -name "qfedu.txt" -exec rm -rvf {} \;
已删除"./qfedu.txt"

8.2 文件压缩打包

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

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

[root@localhost ~]# mkdir dir0001
[root@localhost ~]# mkdir dir0001/file{1..20}
[root@localhost ~]# ls dir0001/
file1   file11  file13  file15  file17  file19  file20  file4  file6  file8
file10  file12  file14  file16  file18  file2   file3   file5  file7  file9
[root@localhost ~]# gzip dir0001/*
[root@localhost ~]# ls dir0001/
file1.gz   file11.gz  file13.gz  file15.gz  file17.gz  file19.gz  file20.gz  file4.gz  file6.gz  file8.gz
file10.gz  file12.gz  file14.gz  file16.gz  file18.gz  file2.gz   file3.gz   file5.gz  file7.gz  file9.gz

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

[root@localhost ~]# tar -czf tecl-gzip.tar.gz /etc/
tar: 从成员名中删除开头的“/”
[root@localhost ~]# 
[root@localhost ~]# tar -cjf tecl-gzip2.tar.gz /etc/
tar: 从成员名中删除开头的“/”
[root@localhost ~]# tar -cJf tecl-xz.tar.gz /etc/
tar: 从成员名中删除开头的“/”
[root@localhost ~]# ll -h etcl*

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

8.3 文件解压

​       当解压某个压缩文件时,首先需要知道该文件是由何种压缩方式创建出来的,然后用相应的解压方式解压文件。当用户不清楚文件使用何种压缩工具压缩时,可以通过file 命令查看文件的压缩信息。

[root@localhost ~]# file etcl-gzip.tar.gz
  • 在不解压的情况下,使用tar命令可以查看文件信息,添加 “ -t ”参数可查看打包文件的文件名
[root@localhost ~]# tar -tf etcl-gzip.tar.gz
  • 使用tar命令解压文件时,添加“ -x ”参数表示解打包或者解压缩,“ -C “参数表示解压到指定文件夹,添加” -v “参数可以显示解压过程。
[root@localhost ~]# tar -xvf etcl-gzip.tar.gz -C /tar/temp
[root@localhost ~]# ls /tar/temp
  • 使用wget命令从Nginx官方网站下载软件包
[root@localhost ~]# wget http://nginx.org/download/nginx-1.14.0.tar.gz
  • 使用tar命令进行解压
[root@localhost ~]# tar xf nginx-1.14.0.tar.gz
[root@localhost ~]# ls
  • 解压后查看软件包内的文件
[root@localhost ~]# cd nginx-1.14.0/
  • 还有一种后缀为.zip的文件,直接使用unzip命令解压
[root@localhost ~]# unzip xxx.zip

8.4 tar命令实例

  • MySQL 物理备份及恢复。

       首先安装 mariadb-server,然后启动该服务,创建备份目录, 具体如下所示。

[root@qfedu ~]# yum -y install mariadb-server

[root@qfedu ~]# systemctl start mariadb

[root@qfedu ~]# mkdir /backup

       接着将/var/lib/mysql中的文件打包压缩至/backup 目录下,删除/var/lib/mysql/目录下文件,具体如下所示。

[root@qfedu ~]# tar -cJf /backup/mysql.tar.xz/var/lib/mysql
[root@qfedu ~]# rm -rf/var/lib/mysql/*

       最后, 将 mysql. tar. xz解压至根目录下,具体如下所示。

[root@qfedu ~]# tar -xf /backup/mysql.tar.xz-C /

       此外,通过以下方式也可以进行恢复,具体如下所示。

[root@qfedu ~]# cd /var/lib/mysql
[root@qfedu mysql]# tar -cJf /backup/mysql.tar.xz*
[root@qfedu mysql]# tar -xf /backup/mysql.tar.xz-C /var/lib/mysql
  • 主机A 把海量的几KB的小文件(这里以/etc目录为例)复制到主机A的/tmp 目录下。

       用打包压缩的方式是可以的,但会消耗较多时间和系统资源,引起I/O操作。存储是速度最慢的一个环节,可以让打包和压缩过程只发生在内存中。

       使用tar命令打包压缩并在文件名前添加“-”符号,中间以管道符连接,再次用tar命令解包解压,具体如下所示。

[root@qfedu ~]# tar -czf - /etc Itar-xzf - -C/tmp
tar: Removing leading '/' from member names
  • 主机A 把海量的几KB的小文件(这里以/etc目录为例)复制到主机B的/tmp 目录下。常规的方法是使用scp命令复制,效率较低,具体如下所示。
[root@qfedu ~]# scp-r /etc 10.18.45.50:/tmp

       建议前面的思维方法,效率较高,具体如下所示。

[root@qfedu ~]# firewall-cmd --permanent --add-port=8888/tcp
[root@qfedu ~]# firewall-cm d -reload
[root@linux~]# nc -1 8888 Itar -xzf - -C /tmp
[root@qfedu ~]# tar -czf - /etc| nc 10.18.45.50 8888

8.5 本章小结

       本章重点讲解了文件查找、打包压缩及解压,这三种文件操作在实际环境中经常使用,自己在后面的日子也时不时练习这些命令,另外,本章最后讲解tar命令实战案例,我仔细体会到了tar命令的巧妙使用。

8.6 参考资料

Linux命令之查找文件find

Linux环境下,文件的压缩/解压

posted @   林小满吼吼吼  阅读(68)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
点击右上角即可分享
微信分享提示