檔案的打包

  1. tar 命令
    [root@kvm8 ~]# 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
          --delete               delete from the archive (not on mag tapes!)
      -d, --diff, --compare      find differences between archive and file system
      -r, --append               append files to the end of an archive
          --test-label           test the archive volume label and exit
      -t, --list                 list the contents of an archive
      -u, --update               only append files newer than copy in archive
      -x, --extract, --get       extract files from an archive
    ......以下省略........................
    
    參數:
    -c  :建立一個打包檔案(create);
    -x  :解開一個打包檔案; 
    -t  :查看 tarfile 內的檔案;(c/x/t 不可同時存在!)
    -z  :同時用 gzip 壓縮;
    -j  :同時用 bzip2 壓縮;
    -J  :同時用 xz 壓縮;
    -v  :壓縮的過程中顯示檔案;
    -f  :使用檔名,在 f 之後要立即接檔名!
    -p  :保留檔案的原來屬性(屬性不會依據使用者而變) 
    -P  :保留絕對路徑;
    -N  :比後面接的日期(yyyy/mm/dd)還要新的才會被打包; 
    --exclude FILE:排除 FILE 。
    
  2. 將檔案 test.txt 打包為 test.tar
    [root@kvm8 ~]# ll test*
    -rw-r--r--. 1 root root 5 Oct 30 15:59 test.txt
    [root@kvm8 ~]# tar cvf test.tar test.txt
    test.txt
    [root@kvm8 ~]# ll test*
    -rw-r--r--. 1 root root 10240 Oct 30 17:07 test.tar
    -rw-r--r--. 1 root root     5 Oct 30 15:59 test.txt
    
  3. 將/usr/local/bin 整個目錄打包為 testlocal.tar
    [root@kvm8 ~]# tar cvf testbin.tar /usr/local/bin
    tar: Removing leading `/' from member names
    /usr/local/bin/
    /usr/local/bin/ex200
    /usr/local/bin/garmin
    [root@kvm8 ~]# ll test*
    -rw-r--r--. 1 root root 20480 Oct 30 17:07 testbin.tar
    -rw-r--r--. 1 root root 10240 Oct 30 17:07 test.tar
    -rw-r--r--. 1 root root     5 Oct 30 15:59 test.txt