Linux操作系统的打包/归档工具介绍

               Linux操作系统的打包/归档工具介绍

                                   作者:尹正杰

版权声明:原创作品,谢绝转载!否则将追究法律责任。

一.tar( Tape ARchive)工具

1>.tar命令常用选项介绍

[root@node101.yinzhengjie.org.cn ~]# tar --help
Usage: tar [OPTION...] [FILE]...
GNU `tar' saves many files together into a single tape or disk archive, and can
restore individual files from the archive.

Examples:
  tar -cf archive.tar foo bar  # Create archive.tar from files foo and bar.
  tar -tvf archive.tar         # List all files in archive.tar verbosely.
  tar -xf archive.tar          # Extract all files from archive.tar.

 Main operation mode:

  -A, --catenate, --concatenate   append tar files to an archive
  -c, --create               create a new archive
  -d, --diff, --compare      find differences between archive and file system
      --delete               delete from the archive (not on mag tapes!)
  -r, --append               append files to the end of an archive
  -t, --list                 list the contents of an archive
      --test-label           test the archive volume label and exit
  -u, --update               only append files newer than copy in archive
  -x, --extract, --get       extract files from an archive

 Operation modifiers:

      --check-device         check device numbers when creating incremental
                             archives (default)
  -g, --listed-incremental=FILE   handle new GNU-format incremental backup
  -G, --incremental          handle old GNU-format incremental backup
      --ignore-failed-read   do not exit with nonzero on unreadable files
      --level=NUMBER         dump level for created listed-incremental archive
  -n, --seek                 archive is seekable
      --no-check-device      do not check device numbers when creating
                             incremental archives
      --no-seek              archive is not seekable
      --occurrence[=NUMBER]  process only the NUMBERth occurrence of each file
                             in the archive; this option is valid only in
                             conjunction with one of the subcommands --delete,
                             --diff, --extract or --list and when a list of
                             files is given either on the command line or via
                             the -T option; NUMBER defaults to 1
      --sparse-version=MAJOR[.MINOR]
                             set version of the sparse format to use (implies
                             --sparse)
  -S, --sparse               handle sparse files efficiently

 Overwrite control:

  -k, --keep-old-files       don't replace existing files when extracting,
                             treat them as errors
      --keep-directory-symlink   preserve existing symlinks to directories when
                             extracting
      --keep-newer-files     don't replace existing files that are newer than
                             their archive copies
      --no-overwrite-dir     preserve metadata of existing directories
      --overwrite            overwrite existing files when extracting
      --overwrite-dir        overwrite metadata of existing directories when
                             extracting (default)
      --recursive-unlink     empty hierarchies prior to extracting directory
      --remove-files         remove files after adding them to the archive
      --skip-old-files       don't replace existing files when extracting,
                             silently skip over them
  -U, --unlink-first         remove each file prior to extracting over it
  -W, --verify               attempt to verify the archive after writing it

 Select output stream:

      --ignore-command-error ignore exit codes of children
      --no-ignore-command-error   treat non-zero exit codes of children as
                             error
  -O, --to-stdout            extract files to standard output
      --to-command=COMMAND   pipe extracted files to another program

 Handling of file attributes:

      --atime-preserve[=METHOD]   preserve access times on dumped files, either
                             by restoring the times after reading
                             (METHOD='replace'; default) or by not setting the
                             times in the first place (METHOD='system')
      --delay-directory-restore   delay setting modification times and
                             permissions of extracted directories until the end
                             of extraction
      --group=NAME           force NAME as group for added files
      --mode=CHANGES         force (symbolic) mode CHANGES for added files
      --mtime=DATE-OR-FILE   set mtime for added files from DATE-OR-FILE
  -m, --touch                don't extract file modified time
      --no-delay-directory-restore
                             cancel the effect of --delay-directory-restore
                             option
      --no-same-owner        extract files as yourself (default for ordinary
                             users)
      --no-same-permissions  apply the user's umask when extracting permissions
                             from the archive (default for ordinary users)
      --numeric-owner        always use numbers for user/group names
      --owner=NAME           force NAME as owner for added files
  -p, --preserve-permissions, --same-permissions
                             extract information about file permissions
                             (default for superuser)
      --preserve             same as both -p and -s
      --same-owner           try extracting files with the same ownership as
                             exists in the archive (default for superuser)
  -s, --preserve-order, --same-order
                             member arguments are listed in the same order as
                             the files in the archive

 Handling of extended file attributes:

      --acls                 Enable the POSIX ACLs support
      --no-acls              Disable the POSIX ACLs support
      --no-selinux           Disable the SELinux context support
      --no-xattrs            Disable extended attributes support
      --selinux              Enable the SELinux context support
      --xattrs               Enable extended attributes support
      --xattrs-exclude=MASK  specify the exclude pattern for xattr keys
      --xattrs-include=MASK  specify the include pattern for xattr keys

 Device selection and switching:

  -f, --file=ARCHIVE         use archive file or device ARCHIVE
      --force-local          archive file is local even if it has a colon
  -F, --info-script=NAME, --new-volume-script=NAME
                             run script at end of each tape (implies -M)
  -L, --tape-length=NUMBER   change tape after writing NUMBER x 1024 bytes
  -M, --multi-volume         create/list/extract multi-volume archive
      --rmt-command=COMMAND  use given rmt COMMAND instead of rmt
      --rsh-command=COMMAND  use remote COMMAND instead of rsh
      --volno-file=FILE      use/update the volume number in FILE

 Device blocking:

  -b, --blocking-factor=BLOCKS   BLOCKS x 512 bytes per record
  -B, --read-full-records    reblock as we read (for 4.2BSD pipes)
  -i, --ignore-zeros         ignore zeroed blocks in archive (means EOF)
      --record-size=NUMBER   NUMBER of bytes per record, multiple of 512

 Archive format selection:

  -H, --format=FORMAT        create archive of the given format

 FORMAT is one of the following:

    gnu                      GNU tar 1.13.x format
    oldgnu                   GNU format as per tar <= 1.12
    pax                      POSIX 1003.1-2001 (pax) format
    posix                    same as pax
    ustar                    POSIX 1003.1-1988 (ustar) format
    v7                       old V7 tar format

      --old-archive, --portability
                             same as --format=v7
      --pax-option=keyword[[:]=value][,keyword[[:]=value]]...
                             control pax keywords
      --posix                same as --format=posix
  -V, --label=TEXT           create archive with volume name TEXT; at
                             list/extract time, use TEXT as a globbing pattern
                             for volume name

 Compression options:

  -a, --auto-compress        use archive suffix to determine the compression
                             program
  -I, --use-compress-program=PROG
                             filter through PROG (must accept -d)
  -j, --bzip2                filter the archive through bzip2
  -J, --xz                   filter the archive through xz
      --lzip                 filter the archive through lzip
      --lzma                 filter the archive through lzma
      --lzop
      --no-auto-compress     do not use archive suffix to determine the
                             compression program
  -z, --gzip, --gunzip, --ungzip   filter the archive through gzip
  -Z, --compress, --uncompress   filter the archive through compress

 Local file selection:

      --add-file=FILE        add given FILE to the archive (useful if its name
                             starts with a dash)
      --backup[=CONTROL]     backup before removal, choose version CONTROL
  -C, --directory=DIR        change to directory DIR
      --exclude=PATTERN      exclude files, given as a PATTERN
      --exclude-backups      exclude backup and lock files
      --exclude-caches       exclude contents of directories containing
                             CACHEDIR.TAG, except for the tag file itself
      --exclude-caches-all   exclude directories containing CACHEDIR.TAG
      --exclude-caches-under exclude everything under directories containing
                             CACHEDIR.TAG
      --exclude-tag=FILE     exclude contents of directories containing FILE,
                             except for FILE itself
      --exclude-tag-all=FILE exclude directories containing FILE
      --exclude-tag-under=FILE   exclude everything under directories
                             containing FILE
      --exclude-vcs          exclude version control system directories
  -h, --dereference          follow symlinks; archive and dump the files they
                             point to
      --hard-dereference     follow hard links; archive and dump the files they
                             refer to
  -K, --starting-file=MEMBER-NAME
                             begin at member MEMBER-NAME when reading the
                             archive
      --newer-mtime=DATE     compare date and time when data changed only
      --no-null              disable the effect of the previous --null option
      --no-recursion         avoid descending automatically in directories
      --no-unquote           do not unquote filenames read with -T
      --null                 -T reads null-terminated names, disable -C
  -N, --newer=DATE-OR-FILE, --after-date=DATE-OR-FILE
                             only store files newer than DATE-OR-FILE
      --one-file-system      stay in local file system when creating archive
  -P, --absolute-names       don't strip leading `/'s from file names
      --recursion            recurse into directories (default)
      --suffix=STRING        backup before removal, override usual suffix ('~'
                             unless overridden by environment variable
                             SIMPLE_BACKUP_SUFFIX)
  -T, --files-from=FILE      get names to extract or create from FILE
      --unquote              unquote filenames read with -T (default)
  -X, --exclude-from=FILE    exclude patterns listed in FILE

 File name transformations:

      --strip-components=NUMBER   strip NUMBER leading components from file
                             names on extraction
      --transform=EXPRESSION, --xform=EXPRESSION
                             use sed replace EXPRESSION to transform file
                             names

 File name matching options (affect both exclude and include patterns):

      --anchored             patterns match file name start
      --ignore-case          ignore case
      --no-anchored          patterns match after any `/' (default for
                             exclusion)
      --no-ignore-case       case sensitive matching (default)
      --no-wildcards         verbatim string matching
      --no-wildcards-match-slash   wildcards do not match `/'
      --wildcards            use wildcards (default)
      --wildcards-match-slash   wildcards match `/' (default for exclusion)

 Informative output:

      --checkpoint[=NUMBER]  display progress messages every NUMBERth record
                             (default 10)
      --checkpoint-action=ACTION   execute ACTION on each checkpoint
      --full-time            print file time to its full resolution
      --index-file=FILE      send verbose output to FILE
  -l, --check-links          print a message if not all links are dumped
      --no-quote-chars=STRING   disable quoting for characters from STRING
      --quote-chars=STRING   additionally quote characters from STRING
      --quoting-style=STYLE  set name quoting style; see below for valid STYLE
                             values
  -R, --block-number         show block number within archive with each message
                            
      --show-defaults        show tar defaults
      --show-omitted-dirs    when listing or extracting, list each directory
                             that does not match search criteria
      --show-transformed-names, --show-stored-names
                             show file or archive names after transformation
      --totals[=SIGNAL]      print total bytes after processing the archive;
                             with an argument - print total bytes when this
                             SIGNAL is delivered; Allowed signals are: SIGHUP,
                             SIGQUIT, SIGINT, SIGUSR1 and SIGUSR2; the names
                             without SIG prefix are also accepted
      --utc                  print file modification times in UTC
  -v, --verbose              verbosely list files processed
      --warning=KEYWORD      warning control
  -w, --interactive, --confirmation
                             ask for confirmation for every action

 Compatibility options:

  -o                         when creating, same as --old-archive; when
                             extracting, same as --no-same-owner

 Other options:

  -?, --help                 give this help list
      --restrict             disable use of some potentially harmful options
      --usage                give a short usage message
      --version              print program version

Mandatory or optional arguments to long options are also mandatory or optional
for any corresponding short options.

The backup suffix is `~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.
The version control may be set with --backup or VERSION_CONTROL, values are:

  none, off       never make backups
  t, numbered     make numbered backups
  nil, existing   numbered if numbered backups exist, simple otherwise
  never, simple   always make simple backups

Valid arguments for the --quoting-style option are:

  literal
  shell
  shell-always
  c
  c-maybe
  escape
  locale
  clocale

*This* tar defaults to:
--format=gnu -f- -b20 --quoting-style=escape --rmt-command=/etc/rmt
--rsh-command=/usr/bin/ssh

Report bugs to <bug-tar@gnu.org>.
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# tar --help
-c:
  表示创建一个文件,一般是用在打包时的参数。  
-p:   保留文件权限属性。
-v:
  查看操作的详细过程。
-f:
  需要指定一个具体的文件名次。
-r:
  将新的文件或目录追加到已归档文件中。
-t:
  预览文件内容,并不解包。
-x:
  解包操作。

-z:
  使用gzip方式压缩文件  

--exclude:
  排除指定的文件。

-T:
  指定输入文件列表,即需要归档的文件或目录。

-X:
  指定包含要排除的文件列表。  

2>.创建"/etc/"的归档文件且保留权限

[root@node101.yinzhengjie.org.cn ~]# du -sh /etc/
37M     /etc/
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ll
total 0
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# tar -cpf etc.tar /etc/
tar: Removing leading `/' from member names
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ll -h                   #虽然看起来目录是小了点,但咱们在归档时并没有添加压缩参数哟~ 
total 32M
-rw-r--r--. 1 root root 32M Dec 31 05:00 etc.tar
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# tar -cpf etc.tar /etc/

3>.预览文件内容

[root@node101.yinzhengjie.org.cn ~]# ll -h
total 32M
-rw-r--r--. 1 root root 32M Dec 31 05:00 etc.tar
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# tar -tf etc.tar             #预览文件内容
......
etc/NetworkManager/dispatcher.d/
etc/NetworkManager/dispatcher.d/11-dhclient
etc/NetworkManager/dispatcher.d/20-chrony
etc/NetworkManager/dispatcher.d/pre-down.d/
etc/NetworkManager/dispatcher.d/00-netreport
etc/NetworkManager/dispatcher.d/no-wait.d/
etc/NetworkManager/dispatcher.d/pre-up.d/
etc/NetworkManager/system-connections/
etc/NetworkManager/NetworkManager.conf
etc/bashrc
etc/ssl/
etc/ssl/certs
etc/dhcp/
etc/dhcp/dhclient-exit-hooks.d/
etc/dhcp/dhclient-exit-hooks.d/azure-cloud.sh
etc/dhcp/dhclient.d/
etc/dhcp/dhclient.d/chrony.sh
etc/opt/
etc/shadow-
etc/kernel/
etc/kernel/postinst.d/
etc/kernel/postinst.d/51-dracut-rescue-postinst.sh
etc/statetab
etc/python/
etc/python/cert-verification.cfg
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ll -h                        #预览文件并没有进行解压操作
total 32M
-rw-r--r--. 1 root root 32M Dec 31 05:00 etc.tar
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# tar -tf etc.tar             #预览文件内容

4>.追加文件至归档(注意,不支持对压缩文件追加)

[root@node101.yinzhengjie.org.cn ~]# ll -h
total 32M
-rw-r--r--. 1 root root 32M Dec 31 05:09 etc.tar
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# tar -rf etc.tar /etc/        #将另外一个目录追加到一个已经归档到文件中
tar: Removing leading `/' from member names
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ll -h
total 63M
-rw-r--r--. 1 root root 63M Dec 31 05:09 etc.tar
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# tar -rf etc.tar /etc/        #将另外一个目录追加到一个已经归档到文件中

5>.展开归档(即解包) 

[root@node101.yinzhengjie.org.cn ~]# ll -h
total 32M
-rw-r--r--. 1 root root 32M Dec 31 05:16 etc.tar
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# tar -xf etc.tar             #解压文件到当前目录
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ll -h
total 32M
drwxr-xr-x. 74 root root 4.0K Dec 31 03:25 etc
-rw-r--r--.  1 root root  32M Dec 31 05:16 etc.tar
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# du -sh etc
37M     etc
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# du -sh /etc/              
37M     /etc/
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# mkdir /tmp/test
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ll /tmp/test/
total 0
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# tar -xf etc.tar -C /tmp/test/        #使用"-C"参数可以解压文件到指定到目录("/tmp/test")
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ll /tmp/test/                
total 4
drwxr-xr-x. 74 root root 4096 Dec 31 03:25 etc
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# du -sh /tmp/test/
37M     /tmp/test/
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# tar -xf etc.tar -C /tmp/test/     #使用"-C"参数可以解压文件到指定到目录("/tmp/test")

6>.打包并压缩文件

[root@node101.yinzhengjie.org.cn ~]# yum -y install zip unzip
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.huaweicloud.com
 * extras: mirrors.huaweicloud.com
 * updates: mirrors.tuna.tsinghua.edu.cn
Resolving Dependencies
--> Running transaction check
---> Package unzip.x86_64 0:6.0-20.el7 will be installed
---> Package zip.x86_64 0:3.0-11.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=================================================================================================================================
 Package                      Arch                          Version                            Repository                   Size
=================================================================================================================================
Installing:
 unzip                        x86_64                        6.0-20.el7                         base                        170 k
 zip                          x86_64                        3.0-11.el7                         base                        260 k

Transaction Summary
=================================================================================================================================
Install  2 Packages

Total download size: 430 k
Installed size: 1.1 M
Downloading packages:
(1/2): unzip-6.0-20.el7.x86_64.rpm                                                                        | 170 kB  00:00:00     
(2/2): zip-3.0-11.el7.x86_64.rpm                                                                          | 260 kB  00:00:00     
---------------------------------------------------------------------------------------------------------------------------------
Total                                                                                            2.2 MB/s | 430 kB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : zip-3.0-11.el7.x86_64                                                                                         1/2 
  Installing : unzip-6.0-20.el7.x86_64                                                                                       2/2 
  Verifying  : unzip-6.0-20.el7.x86_64                                                                                       1/2 
  Verifying  : zip-3.0-11.el7.x86_64                                                                                         2/2 

Installed:
  unzip.x86_64 0:6.0-20.el7                                        zip.x86_64 0:3.0-11.el7                                       

Complete!
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# yum -y install zip unzip
[root@node101.yinzhengjie.org.cn ~]# yum -y install bzip2
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.huaweicloud.com
 * extras: mirrors.huaweicloud.com
 * updates: mirrors.tuna.tsinghua.edu.cn
Resolving Dependencies
--> Running transaction check
---> Package bzip2.x86_64 0:1.0.6-13.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=================================================================================================================================
 Package                     Arch                         Version                               Repository                  Size
=================================================================================================================================
Installing:
 bzip2                       x86_64                       1.0.6-13.el7                          base                        52 k

Transaction Summary
=================================================================================================================================
Install  1 Package

Total download size: 52 k
Installed size: 82 k
Downloading packages:
bzip2-1.0.6-13.el7.x86_64.rpm                                                                             |  52 kB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : bzip2-1.0.6-13.el7.x86_64                                                                                     1/1 
  Verifying  : bzip2-1.0.6-13.el7.x86_64                                                                                     1/1 

Installed:
  bzip2.x86_64 0:1.0.6-13.el7                                                                                                    

Complete!
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# yum -y install bzip2
[root@node101.yinzhengjie.org.cn ~]# du -sh /etc/
37M     /etc/
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ll -h
total 0
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# tar -zcf etc.tar.gz /etc/          #以gzip方式归档并压缩文件
tar: Removing leading `/' from member names
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ll -h
total 11M
-rw-r--r--. 1 root root 11M Dec 31 05:26 etc.tar.gz
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# tar -zcf etc.tar.gz /etc/          #以gzip方式归档并压缩文件
[root@node101.yinzhengjie.org.cn ~]# ll -h
total 11M
-rw-r--r--. 1 root root 11M Dec 31 05:26 etc.tar.gz
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# tar jcf etc.tar.bz2 /etc/          #以bzip2方式归档并压缩文件
tar: Removing leading `/' from member names
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ll -h
total 20M
-rw-r--r--. 1 root root 9.3M Dec 31 05:36 etc.tar.bz2
-rw-r--r--. 1 root root  11M Dec 31 05:26 etc.tar.gz
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# tar jcf etc.tar.bz2 /etc/          #以bzip2方式归档并压缩文件
[root@node101.yinzhengjie.org.cn ~]# ll -h
total 20M
-rw-r--r--. 1 root root 9.3M Dec 31 05:36 etc.tar.bz2
-rw-r--r--. 1 root root  11M Dec 31 05:26 etc.tar.gz
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# tar Jcf etc.tar.xz /etc/          #以xz方式归档并压缩文件
tar: Removing leading `/' from member names
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ll -h
total 28M
-rw-r--r--. 1 root root 9.3M Dec 31 05:36 etc.tar.bz2
-rw-r--r--. 1 root root  11M Dec 31 05:26 etc.tar.gz
-rw-r--r--. 1 root root 7.6M Dec 31 05:39 etc.tar.xz
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# tar Jcf etc.tar.xz /etc/          #以xz方式归档并压缩文件
[root@node101.yinzhengjie.org.cn ~]# ll
total 0
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# dd if=/dev/zero of=f1.txt bs=1M count=4096        #我们用数字"0"来创建一个4G的文件
4096+0 records in
4096+0 records out
4294967296 bytes (4.3 GB) copied, 7.6036 s, 565 MB/s
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# hexdump -C f1.txt 
00000000  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
100000000
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ll -h
total 4.1G
-rw-r--r--. 1 root root 4.0G Dec 31 05:47 f1.txt
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# xz f1.txt                            #注意,使用xz压缩一个4G全是"0"的文件,可能结果很很小,因此解压一个xz文件时我们得留意磁盘空间是否充足。
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ll -h     
total 612K
-rw-r--r--. 1 root root 611K Dec 31 05:47 f1.txt.xz
[root@node101.yinzhengjie.org.cn ~]# 
关于xz压缩文件的小案例

7>.排除"/etc"目录中的"/etc/hosts","/etc/hostname","/etc/fstab"这三个文件,并以gzip方式归档并压缩文件

[root@node101.yinzhengjie.org.cn ~]# ll -h
total 0
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# tar -zcf /root/etc.tgz --exclude=/etc/hosts --exclude=/etc/hostname --exclude=/etc/fstab /etc/ 
tar: Removing leading `/' from member names
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ll -h
total 11M
-rw-r--r--. 1 root root 11M Dec 31 06:06 etc.tgz
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# tar -zcf /root/etc.tgz --exclude=/etc/hosts --exclude=/etc/hostname --exclude=/etc/fstab /etc/
[root@node101.yinzhengjie.org.cn ~]# ll -h
total 8.0K
-rw-r--r--. 1 root root 36 Dec 31 06:34 exclude.txt
-rw-r--r--. 1 root root  6 Dec 31 06:37 include.txt
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat exclude.txt 
/etc/hosts
/etc/fstab
/etc/hostname
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat include.txt 
/etc/
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# tar zcf myetc.tgz -T include.txt -X exclude.txt 
tar: Removing leading `/' from member names
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ll -h
total 11M
-rw-r--r--. 1 root root  36 Dec 31 06:34 exclude.txt
-rw-r--r--. 1 root root   6 Dec 31 06:37 include.txt
-rw-r--r--. 1 root root 11M Dec 31 06:39 myetc.tgz
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# tar zcf myetc.tgz -T include.txt -X exclude.txt

8>.使用splist分割一个文件为多个文件

[root@node101.yinzhengjie.org.cn ~]# ll -h
total 11M
-rw-r--r--. 1 root root 11M Dec 31 06:06 etc.tgz
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# split -b 3M etc.tgz myEtc_                  #将"etc.tgz"文件按照3M的大小进行切割,指定前缀为"myEtc_"
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ll -h
total 22M
-rw-r--r--. 1 root root  11M Dec 31 06:06 etc.tgz
-rw-r--r--. 1 root root 3.0M Dec 31 06:11 myEtc_aa
-rw-r--r--. 1 root root 3.0M Dec 31 06:11 myEtc_ab
-rw-r--r--. 1 root root 3.0M Dec 31 06:11 myEtc_ac
-rw-r--r--. 1 root root 1.7M Dec 31 06:11 myEtc_ad
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# split -b 3M etc.tgz myEtc_           #将"etc.tgz"文件按照3M的大小进行切割,指定前缀为"myEtc_"
[root@node101.yinzhengjie.org.cn ~]# ll -h
total 22M
-rw-r--r--. 1 root root  11M Dec 31 06:06 etc.tgz
-rw-r--r--. 1 root root 3.0M Dec 31 06:11 myEtc_aa
-rw-r--r--. 1 root root 3.0M Dec 31 06:11 myEtc_ab
-rw-r--r--. 1 root root 3.0M Dec 31 06:11 myEtc_ac
-rw-r--r--. 1 root root 1.7M Dec 31 06:11 myEtc_ad
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# split -d -b 3M etc.tgz myEtc_                #将"etc.tgz"文件按照3M的大小进行切割,指定前缀为"myEtc_",每部分使用数字进行标识。
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ll -h
total 33M
-rw-r--r--. 1 root root  11M Dec 31 06:06 etc.tgz
-rw-r--r--. 1 root root 3.0M Dec 31 06:12 myEtc_00
-rw-r--r--. 1 root root 3.0M Dec 31 06:12 myEtc_01
-rw-r--r--. 1 root root 3.0M Dec 31 06:12 myEtc_02
-rw-r--r--. 1 root root 1.7M Dec 31 06:12 myEtc_03
-rw-r--r--. 1 root root 3.0M Dec 31 06:11 myEtc_aa
-rw-r--r--. 1 root root 3.0M Dec 31 06:11 myEtc_ab
-rw-r--r--. 1 root root 3.0M Dec 31 06:11 myEtc_ac
-rw-r--r--. 1 root root 1.7M Dec 31 06:11 myEtc_ad
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# split -d -b 3M etc.tgz myEtc_         #将"etc.tgz"文件按照3M的大小进行切割,指定前缀为"myEtc_",每部分使用数字进行标识。
[root@node101.yinzhengjie.org.cn ~]# ll
total 32772
-rw-r--r--. 1 root root 11185594 Dec 31 06:06 etc.tgz
-rw-r--r--. 1 root root  3145728 Dec 31 06:12 myEtc_00
-rw-r--r--. 1 root root  3145728 Dec 31 06:12 myEtc_01
-rw-r--r--. 1 root root  3145728 Dec 31 06:12 myEtc_02
-rw-r--r--. 1 root root  1748410 Dec 31 06:12 myEtc_03
-rw-r--r--. 1 root root  3145728 Dec 31 06:11 myEtc_aa
-rw-r--r--. 1 root root  3145728 Dec 31 06:11 myEtc_ab
-rw-r--r--. 1 root root  3145728 Dec 31 06:11 myEtc_ac
-rw-r--r--. 1 root root  1748410 Dec 31 06:11 myEtc_ad
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat myEtc_a* > myetc01                #将切分的多个文件合并成一个文件
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat myEtc_0* > myetc02 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ll
total 54620
-rw-r--r--. 1 root root 11185594 Dec 31 06:06 etc.tgz
-rw-r--r--. 1 root root  3145728 Dec 31 06:12 myEtc_00
-rw-r--r--. 1 root root 11185594 Dec 31 06:17 myetc01
-rw-r--r--. 1 root root  3145728 Dec 31 06:12 myEtc_01
-rw-r--r--. 1 root root 11185594 Dec 31 06:18 myetc02
-rw-r--r--. 1 root root  3145728 Dec 31 06:12 myEtc_02
-rw-r--r--. 1 root root  1748410 Dec 31 06:12 myEtc_03
-rw-r--r--. 1 root root  3145728 Dec 31 06:11 myEtc_aa
-rw-r--r--. 1 root root  3145728 Dec 31 06:11 myEtc_ab
-rw-r--r--. 1 root root  3145728 Dec 31 06:11 myEtc_ac
-rw-r--r--. 1 root root  1748410 Dec 31 06:11 myEtc_ad
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat myEtc_a* > myetc01             #将切分的多个文件合并成一个文件

 

二.cpio工具

1>.cpio常用选项介绍

  cpio命令是通过重定向的方式将文件进行打包备份,还原恢复的工具,它可以解压以".cpio"或者".tar"结尾的文件。

  常用选项如下:
    -o:
      将文件拷贝打包成文件或者将文件输出到设备上     
-O filename:
      输出到指定的归档文件名     
-A:
      向已存在的归档文件中追加文件     
-i:
      解包,将打包文件解压或将设备上的备份还原到系统     
-I filename:
      对指定的归档文件名解压     
-t:
      预览,查看文件内容或者输出到设备上的文件内容     
-F filename:
      使用指定的文件名替代标准输入或输出     
-d:
      解包生成目录,在cpio还原时,自动的建立目录     
-v:
      显示打包过程中的文件名称  

2>.解包cpio文件 

[root@node101.yinzhengjie.org.cn ~]# cp /boot/initramfs-3.10.0-957.el7.x86_64kdump.img ./
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ll -h
total 13M
-rw-------. 1 root root 13M Dec 31 06:53 initramfs-3.10.0-957.el7.x86_64kdump.img
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# file initramfs-3.10.0-957.el7.x86_64kdump.img 
initramfs-3.10.0-957.el7.x86_64kdump.img: ASCII cpio archive (SVR4 with no CRC)
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cpio -i < initramfs-3.10.0-957.el7.x86_64kdump.img 
38 blocks
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ll -h
total 13M
-rw-r--r--. 1 root root    2 Dec 31 06:54 early_cpio
-rw-------. 1 root root  13M Dec 31 06:53 initramfs-3.10.0-957.el7.x86_64kdump.img
drwxr-xr-x. 3 root root 4.0K Dec 31 06:54 kernel
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# tree 
.
├── early_cpio
├── initramfs-3.10.0-957.el7.x86_64kdump.img
└── kernel
    └── x86
        └── microcode
            └── GenuineIntel.bin

3 directories, 3 files
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cpio -i < initramfs-3.10.0-957.el7.x86_64kdump.img

3>.将"/etc"目录备份

[root@node101.yinzhengjie.org.cn ~]# ll -h
total 0
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# find /etc/ | cpio -o > myEtc.cpio
60977 blocks
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ll -h
total 30M
-rw-r--r--. 1 root root 30M Dec 31 07:03 myEtc.cpio
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# find /etc/ | cpio -o > myEtc.cpio

4>.将"/tmp"目录的内容追加到myEtc.cpio文件(该文件是上一步咱们的"/etc"归档文件)中

[root@node101.yinzhengjie.org.cn ~]# ll -h
total 30M
-rw-r--r--. 1 root root 30M Dec 31 07:07 myEtc.cpio
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# find /tmp/ | cpio -oAF myEtc.cpio       
61024 blocks
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ll -h
total 60M
-rw-r--r--. 1 root root 60M Dec 31 07:08 myEtc.cpio
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# find /tmp/ | cpio -oAF myEtc.cpio

5>.归档文件内容预览 

[root@node101.yinzhengjie.org.cn ~]# ll -h
total 60M
-rw-r--r--. 1 root root 60M Dec 31 07:08 myEtc.cpio
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cpio -tv < myEtc.cpio 
......
-rw-r--r--   1 root     root          381 Oct 30  2018 /tmp/test/etc/python/cert-verification.cfg
drwxrwxrwt   2 root     root            0 Dec 31 01:26 /tmp/.ICE-unix
drwxrwxrwt   2 root     root            0 Dec 31 01:26 /tmp/.font-unix
-rw-------   1 root     root            0 Dec 31 01:26 /tmp/yum.log
drwxrwxrwt   2 root     root            0 Dec 31 01:26 /tmp/.XIM-unix
drwxrwxrwt   2 root     root            0 Dec 31 01:26 /tmp/.X11-unix
drwxr-xr-x   2 root     root            0 Dec 31 01:31 /tmp/prl-tools-dir
-rwx------   1 root     root          751 Dec 31 01:31 /tmp/ks-script-KhhKXM
drwx------   3 root     root            0 Dec 31 03:25 /tmp/systemd-private-ca870d3e3b7c4bc8ab2e38ceee2fe2c1-chronyd.service-alyo6n
drwxrwxrwt   2 root     root            0 Dec 31 03:25 /tmp/systemd-private-ca870d3e3b7c4bc8ab2e38ceee2fe2c1-chronyd.service-alyo6n/tmp
-rwx------   1 root     root          836 Dec 31 01:34 /tmp/ks-script-fTLjOG
122000 blocks
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cpio -tv < myEtc.cpio
原文地址:https://www.cnblogs.com/yinzhengjie/p/12056858.html