24.第七章 Linux打包压缩(二)

2.压缩和解压缩

2.1 compress和uncompress

此工具来自于ncompress包

格式

compress Options [file ...]
uncompress file.Z #解压缩

常用选项

-d 解压缩,相当于uncompress
-c 结果输出至标准输出,不删除原文件
-v 显示详情

zcat file.Z 不显式解压缩的前提下查看文本文件内容

范例:

zcat file.Z >file

范例:

[root@rocky8 ~]# dnf -y install ncompress
[root@rocky8 ~]# cp /var/log/messages .
[root@rocky8 ~]# cp messages m.txt
[root@rocky8 ~]# compress m.txt
[root@rocky8 ~]# ll
total 740
-rw-------. 1 root root   1318 Oct  6 19:20 anaconda-ks.cfg
drwxr-x---. 2 root root     87 Oct  6 19:20 anaconda-screenshots
-rw-------  1 root root 607137 Oct 11 15:32 messages
-rw-------  1 root root 140581 Oct 11 15:32 m.txt.Z
[root@rocky8 ~]# uncompress m.txt.Z
[root@rocky8 ~]# ll
total 1196
-rw-------. 1 root root   1318 Oct  6 19:20 anaconda-ks.cfg
drwxr-x---. 2 root root     87 Oct  6 19:20 anaconda-screenshots
-rw-------  1 root root 607137 Oct 11 15:32 messages
-rw-------  1 root root 607137 Oct 11 15:32 m.txt
[root@rocky8 ~]# compress m.txt
[root@rocky8 ~]# cp messages m.txt
[root@rocky8 ~]# ll
total 1336
-rw-------. 1 root root   1318 Oct  6 19:20 anaconda-ks.cfg
drwxr-x---. 2 root root     87 Oct  6 19:20 anaconda-screenshots
-rw-------  1 root root 607137 Oct 11 15:32 messages
-rw-------  1 root root 607137 Oct 11 15:34 m.txt
-rw-------  1 root root 140581 Oct 11 15:32 m.txt.Z

2.2 gzip和gunzip

格式:

gzip [OPTION]... FILE ...

常用选项:

-k keep, 保留原文件,CentOS 8 新特性
-d 解压缩,相当于gunzip
-c 结果输出至标准输出,保留原文件不改变
-# 指定压缩比,#取值为1-9,值越大压缩比越大

范例:

[root@rocky8 ~]# gzip --help
Usage: gzip [OPTION]... [FILE]...
Compress or uncompress FILEs (by default, compress FILES in-place).

Mandatory arguments to long options are mandatory for short options too.

  -c, --stdout      write on standard output, keep original files unchanged
  -d, --decompress  decompress
  -f, --force       force overwrite of output file and compress links
  -h, --help        give this help
  -k, --keep        keep (don't delete) input files #压缩时,保留原文件,CentOS 8的新特性。
  -l, --list        list compressed file contents
  -L, --license     display software license
  -n, --no-name     do not save or restore the original name and timestamp
  -N, --name        save or restore the original name and timestamp
  -q, --quiet       suppress all warnings
  -r, --recursive   operate recursively on directories
      --rsyncable   make rsync-friendly archive
  -S, --suffix=SUF  use suffix SUF on compressed files
      --synchronous synchronous output (safer if system crashes, but slower)
  -t, --test        test compressed file integrity
  -v, --verbose     verbose mode
  -V, --version     display version number
  -1, --fast        compress faster
  -9, --best        compress better

With no FILE, or when FILE is -, read standard input.

Report bugs to <bug-gzip@gnu.org>.

[root@centos7 ~]# gzip --help
Usage: gzip [OPTION]... [FILE]...
Compress or uncompress FILEs (by default, compress FILES in-place).

Mandatory arguments to long options are mandatory for short options too.

  -c, --stdout      write on standard output, keep original files unchanged
  -d, --decompress  decompress
  -f, --force       force overwrite of output file and compress links
  -h, --help        give this help
  -l, --list        list compressed file contents
  -L, --license     display software license
  -n, --no-name     do not save or restore the original name and time stamp
  -N, --name        save or restore the original name and time stamp
  -q, --quiet       suppress all warnings
  -r, --recursive   operate recursively on directories
  -S, --suffix=SUF  use suffix SUF on compressed files
  -t, --test        test compressed file integrity
  -v, --verbose     verbose mode
  -V, --version     display version number
  -1, --fast        compress faster
  -9, --best        compress better
    --rsyncable   Make rsync-friendly archive

With no FILE, or when FILE is -, read standard input.

Report bugs to <bug-gzip@gnu.org>.
# CentOS 7上没有-k 选项

[root@rocky8 ~]# gzip m.txt
[root@rocky8 ~]# ll
total 824
-rw-------. 1 root root   1318 Oct  6 19:20 anaconda-ks.cfg
drwxr-x---. 2 root root     87 Oct  6 19:20 anaconda-screenshots
-rw-------  1 root root 607137 Oct 11 15:32 messages
-rw-------  1 root root  85040 Oct 11 15:34 m.txt.gz
-rw-------  1 root root 140581 Oct 11 15:32 m.txt.Z

[root@rocky8 ~]# gunzip m.txt.gz #解压缩
[root@rocky8 ~]# ll
total 1336
-rw-------. 1 root root   1318 Oct  6 19:20 anaconda-ks.cfg
drwxr-x---. 2 root root     87 Oct  6 19:20 anaconda-screenshots
-rw-------  1 root root 607137 Oct 11 15:32 messages
-rw-------  1 root root 607137 Oct 11 15:34 m.txt
-rw-------  1 root root 140581 Oct 11 15:32 m.txt.Z
[root@rocky8 ~]# gzip -k m.txt #-k保留原文件
[root@rocky8 ~]# ll
total 1420
-rw-------. 1 root root   1318 Oct  6 19:20 anaconda-ks.cfg
drwxr-x---. 2 root root     87 Oct  6 19:20 anaconda-screenshots
-rw-------  1 root root 607137 Oct 11 15:32 messages
-rw-------  1 root root 607137 Oct 11 15:34 m.txt
-rw-------  1 root root  85040 Oct 11 15:34 m.txt.gz
-rw-------  1 root root 140581 Oct 11 15:32 m.txt.Z

[root@rocky8 ~]# zcat m.txt.gz #zcat 不解压缩文件,直接查看文本文件内容

范例:

[root@rocky8 ~]# gzip -c messages >messages.gz
[root@rocky8 ~]# gzip -c -d messages.gz > messages
[root@rocky8 ~]# zcat messages.gz > messages
[root@rocky8 ~]# cat messages | gzip > m.gz

2.3 bzip2和bunzip2

来自于 bzip2 包

格式:

bzip2 [OPTION]... FILE ...

常用选项

-k keep, 保留原文件
-d 解压缩
-c 结果输出至标准输出,保留原文件不改变
-# 1-9,压缩比,默认为9

范例:

[root@rocky8 ~]# bzip2 -k m.txt
-bash: bzip2: command not found
[root@rocky8 ~]# dnf provides bzip2
Last metadata expiration check: 1:34:14 ago on Mon 11 Oct 2021 02:10:23 PM CST.
bzip2-1.0.6-26.el8.x86_64 : A file compression utility
Repo        : BaseOS
Matched from:
Provide    : bzip2 = 1.0.6-26.el8

[root@rocky8 ~]# dnf -y install bzip2

[root@rocky8 ~]# bzip2 -k m.txt
[root@rocky8 ~]# ll
total 1628
-rw-------. 1 root root   1318 Oct  6 19:20 anaconda-ks.cfg
drwxr-x---. 2 root root     87 Oct  6 19:20 anaconda-screenshots
-rw-------  1 root root 607137 Oct 11 15:42 messages
-rw-r--r--  1 root root  85043 Oct 11 15:42 messages.gz
-rw-r--r--  1 root root  85034 Oct 11 15:42 m.gz
-rw-------  1 root root 607137 Oct 11 15:34 m.txt
-rw-------  1 root root  39140 Oct 11 15:34 m.txt.bz2
-rw-------  1 root root  85040 Oct 11 15:34 m.txt.gz
-rw-------  1 root root 140581 Oct 11 15:32 m.txt.Z
[root@rocky8 ~]# rm -f m.txt
[root@rocky8 ~]# bunzip2 -d m.txt.bz2 #bzip2 -d 和bunzip2 一样,都是解压
[root@rocky8 ~]# ll
total 1588
-rw-------. 1 root root   1318 Oct  6 19:20 anaconda-ks.cfg
drwxr-x---. 2 root root     87 Oct  6 19:20 anaconda-screenshots
-rw-------  1 root root 607137 Oct 11 15:42 messages
-rw-r--r--  1 root root  85043 Oct 11 15:42 messages.gz
-rw-r--r--  1 root root  85034 Oct 11 15:42 m.gz
-rw-------  1 root root 607137 Oct 11 15:34 m.txt
-rw-------  1 root root  85040 Oct 11 15:34 m.txt.gz
-rw-------  1 root root 140581 Oct 11 15:32 m.txt.Z

[root@rocky8 ~]# bzip2 -k m.txt
[root@rocky8 ~]# ll
total 1628
-rw-------. 1 root root   1318 Oct  6 19:20 anaconda-ks.cfg
drwxr-x---. 2 root root     87 Oct  6 19:20 anaconda-screenshots
-rw-------  1 root root 607137 Oct 11 15:42 messages
-rw-r--r--  1 root root  85043 Oct 11 15:42 messages.gz
-rw-r--r--  1 root root  85034 Oct 11 15:42 m.gz
-rw-------  1 root root 607137 Oct 11 15:34 m.txt
-rw-------  1 root root  39140 Oct 11 15:34 m.txt.bz2
-rw-------  1 root root  85040 Oct 11 15:34 m.txt.gz
-rw-------  1 root root 140581 Oct 11 15:32 m.txt.Z

[root@rocky8 ~]# bzcat m.txt.bz2 不显式解压缩的前提下查看文本文件内容

2.4 xz和unxz

来自于 xz 包

格式

xz [OPTION]... FILE ...

常用选项

-k keep, 保留原文件
-d 解压缩,相当于unxz
-c 结果输出至标准输出,保留原文件不改变
-# 压缩比,取值1-9,默认为6

范例:

[root@rocky8 ~]# xz -k m.txt
[root@rocky8 ~]# ll
total 1664
-rw-------. 1 root root   1318 Oct  6 19:20 anaconda-ks.cfg
drwxr-x---. 2 root root     87 Oct  6 19:20 anaconda-screenshots
-rw-------  1 root root 607137 Oct 11 15:42 messages
-rw-r--r--  1 root root  85043 Oct 11 15:42 messages.gz
-rw-r--r--  1 root root  85034 Oct 11 15:42 m.gz
-rw-------  1 root root 607137 Oct 11 15:34 m.txt
-rw-------  1 root root  39140 Oct 11 15:34 m.txt.bz2
-rw-------  1 root root  85040 Oct 11 15:34 m.txt.gz
-rw-------  1 root root  33300 Oct 11 15:34 m.txt.xz
-rw-------  1 root root 140581 Oct 11 15:32 m.txt.Z

[root@rocky8 ~]# unxz m.txt.xz #解压缩

[root@rocky8 ~]# xzcat m.txt.xz #不显式解压缩的前提下查看文本文件内容

2.5 zip和unzip

zip 可以实现打包目录和多个文件成一个文件并压缩,但可能会丢失文件属性信息,如:所有者和组信息,一般建议使用 tar 代替
来自于zip 和 unzip 包

范例:

[root@rocky8 ~]# zip -r etc.zip /etc
-bash: zip: command not found
[root@rocky8 ~]# dnf provides zip
Last metadata expiration check: 1:40:39 ago on Mon 11 Oct 2021 02:10:23 PM CST.
zip-3.0-23.el8.x86_64 : A file compression and packaging utility compatible with PKZIP
Repo        : BaseOS
Matched from:
Provide    : zip = 3.0-23.el8

[root@rocky8 ~]# dnf -y install zip

[root@rocky8 ~]# zip -r etc.zip /etc
[root@rocky8 ~]# ll
total 8500
-rw-------. 1 root root    1318 Oct  6 19:20 anaconda-ks.cfg
drwxr-x---. 2 root root      87 Oct  6 19:20 anaconda-screenshots
-rw-r--r--  1 root root 6997370 Oct 11 15:51 etc.zip
-rw-------  1 root root  607137 Oct 11 15:42 messages
-rw-r--r--  1 root root   85043 Oct 11 15:42 messages.gz
-rw-r--r--  1 root root   85034 Oct 11 15:42 m.gz
-rw-------  1 root root  607137 Oct 11 15:34 m.txt
-rw-------  1 root root   39140 Oct 11 15:34 m.txt.bz2
-rw-------  1 root root   85040 Oct 11 15:34 m.txt.gz
-rw-------  1 root root   33300 Oct 11 15:34 m.txt.xz
-rw-------  1 root root  140581 Oct 11 15:32 m.txt.Z
[root@rocky8 ~]# du -sh /etc
22M	/etc
[root@rocky8 ~]# ll -h etc.zip
-rw-r--r-- 1 root root 6.7M Oct 11 15:51 etc.zip

范例:

#打包并压缩
[root@rocky8 ~]# zip -r /data/sysconfig.zip /etc/sysconfig/
  adding: etc/sysconfig/ (stored 0%)
  adding: etc/sysconfig/run-parts (stored 0%)
  adding: etc/sysconfig/crond (deflated 15%)
  adding: etc/sysconfig/network-scripts/ (stored 0%)
  adding: etc/sysconfig/network-scripts/ifcfg-eth0 (deflated 16%)
  adding: etc/sysconfig/ip6tables-config (deflated 61%)
  adding: etc/sysconfig/iptables-config (deflated 60%)
  adding: etc/sysconfig/ebtables-config (deflated 44%)
  adding: etc/sysconfig/selinux (deflated 51%)
  adding: etc/sysconfig/console/ (stored 0%)
  adding: etc/sysconfig/modules/ (stored 0%)
  adding: etc/sysconfig/grub (deflated 24%)
  adding: etc/sysconfig/cpupower (deflated 25%)
  adding: etc/sysconfig/nftables.conf (deflated 40%)
  adding: etc/sysconfig/firewalld (stored 0%)
  adding: etc/sysconfig/kdump (deflated 53%)
  adding: etc/sysconfig/rsyslog (deflated 23%)
  adding: etc/sysconfig/sshd (deflated 41%)
  adding: etc/sysconfig/irqbalance (deflated 45%)
  adding: etc/sysconfig/man-db (deflated 35%)
  adding: etc/sysconfig/kernel (deflated 34%)
  adding: etc/sysconfig/anaconda (deflated 9%)
  adding: etc/sysconfig/sshd-permitrootlogin (deflated 17%)
  adding: etc/sysconfig/network (stored 0%)
  adding: etc/sysconfig/atd (deflated 31%)

#不包括目录本身,只打包目录内的文件和子目录
[root@rocky8 ~]# cd /etc/sysconfig; zip -r /root/sysconfig.zip *
  adding: anaconda (deflated 9%)
  adding: atd (deflated 31%)
  adding: console/ (stored 0%)
  adding: cpupower (deflated 25%)
  adding: crond (deflated 15%)
  adding: ebtables-config (deflated 44%)
  adding: firewalld (stored 0%)
  adding: grub (deflated 24%)
  adding: ip6tables-config (deflated 61%)
  adding: iptables-config (deflated 60%)
  adding: irqbalance (deflated 45%)
  adding: kdump (deflated 53%)
  adding: kernel (deflated 34%)
  adding: man-db (deflated 35%)
  adding: modules/ (stored 0%)
  adding: network (stored 0%)
  adding: network-scripts/ (stored 0%)
  adding: network-scripts/ifcfg-eth0 (deflated 16%)
  adding: nftables.conf (deflated 40%)
  adding: rsyslog (deflated 23%)
  adding: run-parts (stored 0%)
  adding: selinux (deflated 51%)
  adding: sshd (deflated 41%)
  adding: sshd-permitrootlogin (deflated 17%)

#默认解压缩至当前目录
[root@rocky8 sysconfig]# unzip /data/sysconfig.zip
Archive:  /data/sysconfig.zip
   creating: etc/sysconfig/
 extracting: etc/sysconfig/run-parts  
  inflating: etc/sysconfig/crond     
   creating: etc/sysconfig/network-scripts/
  inflating: etc/sysconfig/network-scripts/ifcfg-eth0  
  inflating: etc/sysconfig/ip6tables-config  
  inflating: etc/sysconfig/iptables-config  
  inflating: etc/sysconfig/ebtables-config  
  inflating: etc/sysconfig/selinux   
   creating: etc/sysconfig/console/
   creating: etc/sysconfig/modules/
  inflating: etc/sysconfig/grub      
  inflating: etc/sysconfig/cpupower  
  inflating: etc/sysconfig/nftables.conf  
 extracting: etc/sysconfig/firewalld  
  inflating: etc/sysconfig/kdump     
  inflating: etc/sysconfig/rsyslog   
  inflating: etc/sysconfig/sshd      
  inflating: etc/sysconfig/irqbalance  
  inflating: etc/sysconfig/man-db    
  inflating: etc/sysconfig/kernel    
  inflating: etc/sysconfig/anaconda  
  inflating: etc/sysconfig/sshd-permitrootlogin  
 extracting: etc/sysconfig/network   
  inflating: etc/sysconfig/atd 

#解压缩至指定目录,如果指定目录不存在,会在其父目录(必须事先存在)下自动生成
[root@rocky8 sysconfig]# unzip /data/sysconfig.zip -d /tmp/config
Archive:  /data/sysconfig.zip
   creating: /tmp/config/etc/sysconfig/
 extracting: /tmp/config/etc/sysconfig/run-parts  
  inflating: /tmp/config/etc/sysconfig/crond  
   creating: /tmp/config/etc/sysconfig/network-scripts/
  inflating: /tmp/config/etc/sysconfig/network-scripts/ifcfg-eth0  
  inflating: /tmp/config/etc/sysconfig/ip6tables-config  
  inflating: /tmp/config/etc/sysconfig/iptables-config  
  inflating: /tmp/config/etc/sysconfig/ebtables-config  
  inflating: /tmp/config/etc/sysconfig/selinux  
   creating: /tmp/config/etc/sysconfig/console/
   creating: /tmp/config/etc/sysconfig/modules/
  inflating: /tmp/config/etc/sysconfig/grub  
  inflating: /tmp/config/etc/sysconfig/cpupower  
  inflating: /tmp/config/etc/sysconfig/nftables.conf  
 extracting: /tmp/config/etc/sysconfig/firewalld  
  inflating: /tmp/config/etc/sysconfig/kdump  
  inflating: /tmp/config/etc/sysconfig/rsyslog  
  inflating: /tmp/config/etc/sysconfig/sshd  
  inflating: /tmp/config/etc/sysconfig/irqbalance  
  inflating: /tmp/config/etc/sysconfig/man-db  
  inflating: /tmp/config/etc/sysconfig/kernel  
  inflating: /tmp/config/etc/sysconfig/anaconda  
  inflating: /tmp/config/etc/sysconfig/sshd-permitrootlogin  
 extracting: /tmp/config/etc/sysconfig/network  
  inflating: /tmp/config/etc/sysconfig/atd 

[root@rocky8 sysconfig]# cat /var/log/messages | zip messages -
  adding: - (deflated 86%)
  
#-p 表示管道
[root@rocky8 sysconfig]# unzip -p message.zip > message

范例: 非交互式加密和解密

[root@rocky8 ~]# zip -P 123456 ~/linux.zip *
  adding: anaconda-ks.cfg (deflated 47%)
  adding: anaconda-screenshots/ (stored 0%)
  adding: etc.zip (stored 0%)
  adding: messages (deflated 86%)
  adding: messages.gz (deflated 4%)
  adding: m.gz (deflated 4%)
  adding: m.txt (deflated 86%)
  adding: m.txt.bz2 (deflated 8%)
  adding: m.txt.gz (deflated 4%)
  adding: m.txt.xz (deflated 0%)
  adding: m.txt.Z (stored 0%)
  adding: sysconfig.zip (stored 0%)

[root@rocky8 ~]# mv linux.zip /opt
[root@rocky8 ~]# cd /opt
[root@rocky8 opt]# unzip -P 123456 linux.zip 
Archive:  linux.zip
  inflating: anaconda-ks.cfg         
   creating: anaconda-screenshots/
 extracting: etc.zip                 
  inflating: messages                
  inflating: messages.gz             
  inflating: m.gz                    
  inflating: m.txt                   
  inflating: m.txt.bz2               
  inflating: m.txt.gz                
  inflating: m.txt.xz                
 extracting: m.txt.Z                 
 extracting: sysconfig.zip

3.打包和解包tar

tar 即 Tape ARchive 磁带归档,可以对目录和多个文件打包一个文件,并且可以压缩,保留文件属性不丢失,常用于备份功能,推荐使用

格式

tar [OPTION]...

(1) 创建归档,保留权限

tar -cpvf /PATH/FILE.tar FILE...

(2) 追加文件至归档: 注:不支持对压缩文件追加

tar -rf /PATH/FILE.tar FILE...

(3) 查看归档文件中的文件列表

tar -t -f /PATH/FILE.tar

(4) 展开归档

tar xf /PATH/FILE.tar
tar xf /PATH/FILE.tar -C /PATH/

(5) 结合压缩工具实现:归档并压缩

-z 相当于gzip压缩工具
-j 相当于bzip2压缩工具
-J 相当于xz压缩工具

范例:

[root@rocky8 ~]# tar cf sysconfig.tar /etc/sysconfig/
tar: Removing leading `/' from member names
[root@rocky8 ~]# ll -h sysconfig.tar
-rw-r--r-- 1 root root 150K Oct 11 16:06 sysconfig.tar
[root@rocky8 ~]# du -sh /etc/sysconfig/
244K	/etc/sysconfig/

[root@rocky8 ~]# tar cf etc.tar /etc
tar: Removing leading `/' from member names
[root@rocky8 ~]# ll -h etc.tar
-rw-r--r-- 1 root root 21M Oct 11 16:07 etc.tar
[root@rocky8 ~]# du -sh /etc
22M	/etc

[root@rocky8 ~]# tar zcf etc.tar.gz /etc
tar: Removing leading `/' from member names
[root@rocky8 ~]# ll -h etc.tar.gz
-rw-r--r-- 1 root root 5.1M Oct 11 16:08 etc.tar.gz

[root@rocky8 ~]# tar jcf etc.tar.bz2 /etc
tar: Removing leading `/' from member names
[root@rocky8 ~]#  tar Jcf etc.tar.xz /etc
tar: Removing leading `/' from member names
[root@rocky8 ~]#  ll -h etc.*
-rw-r--r-- 1 root root  21M Oct 11 16:07 etc.tar
-rw-r--r-- 1 root root 3.6M Oct 11 16:08 etc.tar.bz2
-rw-r--r-- 1 root root 5.1M Oct 11 16:08 etc.tar.gz
-rw-r--r-- 1 root root 3.1M Oct 11 16:09 etc.tar.xz
-rw-r--r-- 1 root root 6.7M Oct 11 15:51 etc.zip

[root@rocky8 ~]# tar tf etc.tar.gz
[root@rocky8 ~]# tar tf etc.tar.bz2
[root@rocky8 ~]# tar tf etc.tar.xz

[root@rocky8 ~]# rm -rf /opt/*
[root@rocky8 ~]# ls /opt
[root@rocky8 ~]# tar xf etc.tar.xz -C /opt
[root@rocky8 ~]# ls /opt
etc

[root@rocky8 ~]# cp -r . /data
cp: overwrite '/data/./sysconfig.zip'? y
[root@rocky8 ~]# ls /data
anaconda-ks.cfg       etc.tar      etc.tar.gz  etc.zip   messages.gz  m.txt      m.txt.gz  m.txt.Z        sysconfig.zip
anaconda-screenshots  etc.tar.bz2  etc.tar.xz  messages  m.gz         m.txt.bz2  m.txt.xz  sysconfig.tar

[root@rocky8 ~]# tar cf data.tar /data/*
tar: Removing leading `/' from member names
tar: Removing leading `/' from hard link targets
[root@rocky8 ~]#  ls /data
anaconda-ks.cfg       etc.tar      etc.tar.gz  etc.zip   messages.gz  m.txt      m.txt.gz  m.txt.Z        sysconfig.zip
anaconda-screenshots  etc.tar.bz2  etc.tar.xz  messages  m.gz         m.txt.bz2  m.txt.xz  sysconfig.tar
[root@rocky8 ~]# tar -tf data.tar
data/anaconda-ks.cfg
data/anaconda-screenshots/
data/anaconda-screenshots/screenshot-0000.png
data/anaconda-screenshots/screenshot-0001.png
data/anaconda-screenshots/screenshot-0002.png
data/etc.tar
data/etc.tar.bz2
data/etc.tar.gz
data/etc.tar.xz
data/etc.zip
data/messages
data/messages.gz
data/m.gz
data/m.txt
data/m.txt.bz2
data/m.txt.gz
data/m.txt.xz
data/m.txt.Z
data/sysconfig.tar

[root@rocky8 ~]# rm -rf /opt/*
[root@rocky8 ~]# tar xf data.tar -C /opt
[root@rocky8 ~]# ls /opt
data
# 打包的文件夹 里边都带目录

[root@rocky8 ~]# cd /data
[root@rocky8 data]# tar zcf /root/data.tar.gz *
[root@rocky8 data]# tar -tf /root/data.tar.gz
anaconda-ks.cfg
anaconda-screenshots/
anaconda-screenshots/screenshot-0000.png
anaconda-screenshots/screenshot-0001.png
anaconda-screenshots/screenshot-0002.png
etc.tar
etc.tar.bz2
etc.tar.gz
etc.tar.xz
etc.zip
messages
messages.gz
m.gz
m.txt
m.txt.bz2
m.txt.gz
m.txt.xz
m.txt.Z
sysconfig.tar
sysconfig.zip
#打包不带目录,进入到目录打包,就没有目录,只有文件

#tar 指定目录打包,不包含目录本身
[root@rocky8 ~]# tar -C /boot -cvf /root/boot3.tar .
[root@rocky8 ~]# tar tvf boot3.tar |less
dr-xr-xr-x root/root         0 2021-10-06 19:21 ./
drwxr-xr-x root/root         0 2021-10-06 19:16 ./efi/
drwxr-xr-x root/root         0 2021-10-06 19:16 ./efi/EFI/
drwx------ root/root         0 2021-05-28 01:21 ./efi/EFI/rocky/
drwx------ root/root         0 2021-10-06 19:24 ./grub2/
-rw-r--r-- root/root        64 2021-10-06 19:19 ./grub2/device.map
drwxr-xr-x root/root         0 2021-10-06 19:19 ./grub2/i386-pc/
-rw-r--r-- root/root      2760 2021-10-06 19:19 ./grub2/i386-pc/gcry_dsa.mod
-rw-r--r-- root/root     13488 2021-10-06 19:19 ./grub2/i386-pc/acpi.mod
-rw-r--r-- root/root      4672 2021-10-06 19:19 ./grub2/i386-pc/gcry_idea.mod
-rw-r--r-- root/root      1648 2021-10-06 19:19 ./grub2/i386-pc/adler32.mod
-rw-r--r-- root/root      4336 2021-10-06 19:19 ./grub2/i386-pc/gcry_md4.mod
-rw-r--r-- root/root      7340 2021-10-06 19:19 ./grub2/i386-pc/affs.mod
-rw-r--r-- root/root      5368 2021-10-06 19:19 ./grub2/i386-pc/gcry_md5.mod
-rw-r--r-- root/root      9232 2021-10-06 19:19 ./grub2/i386-pc/afs.mod
-rw-r--r-- root/root      3920 2021-10-06 19:19 ./grub2/i386-pc/gcry_rfc2268.mod
-rw-r--r-- root/root     21752 2021-10-06 19:19 ./grub2/i386-pc/ahci.mod
-rw-r--r-- root/root     15756 2021-10-06 19:19 ./grub2/i386-pc/diskfilter.mod
-rw-r--r-- root/root       580 2021-10-06 19:19 ./grub2/i386-pc/all_video.mod
-rw-r--r-- root/root     22084 2021-10-06 19:19 ./grub2/i386-pc/gcry_rijndael.mod
-rw-r--r-- root/root      1248 2021-10-06 19:19 ./grub2/i386-pc/aout.mod
-rw-r--r-- root/root     13616 2021-10-06 19:19 ./grub2/i386-pc/appended_signature_test.mod
-rw-r--r-- root/root      1292 2021-10-06 19:19 ./grub2/i386-pc/div.mod
-rw-r--r-- root/root     44176 2021-10-06 19:19 ./grub2/i386-pc/appendedsig.mod
-rw-r--r-- root/root     13792 2021-10-06 19:19 ./grub2/i386-pc/gcry_rmd160.mod
-rw-r--r-- root/root      3852 2021-10-06 19:19 ./grub2/i386-pc/archelp.mod

#利用 tar 进行文件复制
[root@rocky8 ~]# tar c /data/ | tar x -C /tmp
tar: Removing leading `/' from member names
[root@rocky8 ~]# ll /tmp
total 12
drwxr-xr-x  3 root root   17 Oct 11 15:55 config
drwxr-xr-x  4 root root 4096 Oct 11 16:11 data
-rwx------. 1 root root  291 Oct  6 19:20 ks-script-hu2_65lt
-rwx------. 1 root root  701 Oct  6 19:20 ks-script-ltsb0ybt
drwx------  2 root root    6 Oct 11 13:47 vmware-root_726-2957583432
drwx------. 2 root root    6 Oct  6 19:21 vmware-root_727-4290690966
drwx------  2 root root    6 Oct  6 19:25 vmware-root_733-4248680474
drwx------. 2 root root    6 Oct  6 19:21 vmware-root_773-4256676260

–exclude 排除文件

范例:

tar zcvf /root/a.tgz --exclude=/app/host1 --exclude=/app/host2 /app

-T 选项指定输入文件
-X 选项指定包含要排除的文件列表

范例:

tar zcvf mybackup.tgz -T /root/includefilelist -X /root/excludefilelist

范例:

[root@rocky8 ~]# wget https://mirrors.bfsu.edu.cn/apache//httpd/httpd-2.4.46.tar.bz2
-bash: wget: command not found
[root@rocky8 ~]# dnf -y install wget

[root@rocky8 ~]# wget https://mirrors.bfsu.edu.cn/apache//httpd/httpd-2.4.46.tar.bz2
--2021-10-11 16:20:30--  https://mirrors.bfsu.edu.cn/apache//httpd/httpd-2.4.46.tar.bz2
Resolving mirrors.bfsu.edu.cn (mirrors.bfsu.edu.cn)... 39.155.141.16, 2001:da8:20f:4435:4adf:37ff:fe55:2840
Connecting to mirrors.bfsu.edu.cn (mirrors.bfsu.edu.cn)|39.155.141.16|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 7187805 (6.9M) [application/octet-stream]
Saving to: ‘httpd-2.4.46.tar.bz2’

httpd-2.4.46.tar.bz2                100%[==================================================================>]   6.85M  3.97MB/s    in 1.7s    

2021-10-11 16:20:32 (3.97 MB/s) - ‘httpd-2.4.46.tar.bz2’ saved [7187805/7187805]

[root@rocky8 ~]# wget https://mirrors.bfsu.edu.cn/apache//httpd/httpd-2.4.46.tar.gz
--2021-10-11 16:20:54--  https://mirrors.bfsu.edu.cn/apache//httpd/httpd-2.4.46.tar.gz
Resolving mirrors.bfsu.edu.cn (mirrors.bfsu.edu.cn)... 39.155.141.16, 2001:da8:20f:4435:4adf:37ff:fe55:2840
Connecting to mirrors.bfsu.edu.cn (mirrors.bfsu.edu.cn)|39.155.141.16|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 9363314 (8.9M) [application/octet-stream]
Saving to: ‘httpd-2.4.46.tar.gz’

httpd-2.4.46.tar.gz                 100%[==================================================================>]   8.93M  4.17MB/s    in 2.1s    

2021-10-11 16:20:57 (4.17 MB/s) - ‘httpd-2.4.46.tar.gz’ saved [9363314/9363314]

[root@rocky8 ~]# ll -h httpd-2.4.46.tar*
-rw-r--r-- 1 root root 6.9M Aug  5  2020 httpd-2.4.46.tar.bz2
-rw-r--r-- 1 root root 9.0M Aug  5  2020 httpd-2.4.46.tar.gz
[root@rocky8 ~]# tar xf httpd-2.4.46.tar.gz -C /usr/local/src/
[root@rocky8 ~]# ls /usr/local/src/
httpd-2.4.46

[root@rocky8 ~]# wget https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.9.11.tar.xz
[root@rocky8 ~]# tar xf linux-5.9.11.tar.xz 
[root@rocky8 ~]# ls linux-5.9.11/
arch   certs    CREDITS  Documentation  fs       init  Kbuild   kernel  LICENSES     Makefile  net     samples  security  tools  virt
block  COPYING  crypto   drivers        include  ipc   Kconfig  lib     MAINTAINERS  mm        README  scripts  sound     usr
[root@rocky8 ~]# find -name "*.c" |wc -l
29214
上一篇:系统目录结构介绍


下一篇:Spring Cloud各组件调优参数