在 Linux 上使用 tar 命令壓縮和解壓縮檔案的最佳方法


您是否經常在 Linux/Ubuntu 上壓縮和解壓縮檔案?您聽說過.tar副檔名嗎?那麼這篇文章適合您學習使用 tar 命令壓縮和解壓縮檔案以及示例。

什麼是 .tar?

在計算中,tar 是一種應用程式實用程式,用於將許多記錄收集到一個歸檔檔案中,最常稱為 tarball,用於分發或備份功能。Tar 最初是在 Unix 早期開發的,目的是將記錄備份到基於磁帶的儲存裝置。後來它被正式化為 POSIX 標準的一部分。

要獲取有關 tar 的更多資訊,請使用以下命令:

$ 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

.........................................................................................

建立 .tar 歸檔檔案

要建立 .tar 歸檔檔案,請使用以下命令:

$ tar cvf tutorialspoint.tar /home/linux/12dec

在上述命令中,它將位於/home/linux/12dec處的 12dec 目錄歸檔為 tutorialspoint.tar。要驗證上述命令,請使用以下命令:

$ ls

示例輸出應如下所示:

12dec        Documents   flaskr      Music       static     tutorialspoint.tar
crawling     Downloads   intern      Pictures   templates   Videos
Desktop      flask       mozilla.pdf Public     Templates

解壓縮 .tar 歸檔檔案

要解壓縮 .tar 歸檔檔案,請使用以下命令:

$ tar -xvf tutorialspoint.tar

示例輸出應如下所示:

home/linux/12dec/
home/linux/12dec/final_url_weight.py
home/linux/12dec/tp_Crawled_few.txt
home/linux/12dec/Final_Url_Weight.csv
home/linux/12dec/extracting_keywors.py
home/linux/12dec/FINAL_URL_WEIGHT.db
home/linux/12dec/site_health_depth5.txt
home/linux/12dec/check_ageof_site.py
home/linux/12dec/final_url_weight_sqlite.py

建立 .tar.gz 歸檔檔案

要建立 .tar.gz 歸檔檔案,請使用以下命令:

$ tar czvf tutorialspoint.tar.gz /home/linux/12dec

在上述命令中,它將位於/home/linux/12dec處的 12dec 目錄歸檔為 tutorialspoint.tar。要驗證上述命令,請使用以下命令:

$ ls

示例輸出應如下所示:

12dec        Documents         flaskr         Music      static     tutorialspoint.tar
crawling     Downloads         intern         Pictures   templates  tutorialspoint.tar.gz
Desktop      flask             mozilla.pdf    Public     Templates  Videos

解壓縮 .tar.gz 歸檔檔案

要解壓縮 .tar.gz 歸檔檔案,請使用以下命令:

$ tar -xzvf tutorialspoint.tar.gz

示例輸出應如下所示:

home/linux/12dec/
home/linux/12dec/final_url_weight.py
home/linux/12dec/tp_Crawled_few.txt
home/linux/12dec/Final_Url_Weight.csv
home/linux/12dec/extracting_keywors.py
home/linux/12dec/FINAL_URL_WEIGHT.db
home/linux/12dec/site_health_depth5.txt
home/linux/12dec/check_ageof_site.py
home/linux/12dec/final_url_weight_sqlite.py

建立 .tar.bz2 歸檔檔案

要建立 .tar.bz2 歸檔檔案,請使用以下命令:

$ tar cjvf tutorialspoint.tar.bz2 /home/linux/12dec

在上述命令中,它將位於/home/linux/12dec處的 12dec 目錄歸檔為 tutorialspoint.tar。要驗證上述命令,請使用以下命令:

$ ls

示例輸出應如下所示:

12dec               Downloads       mozilla.pdf   static     tutorialspoint.tar.bz2
crawling            flask           Music         templates  tutorialspoint.tar.gz
Desktop             flaskr          Pictures      Templates  Videos
Documents           intern          Public        tutorialspoint.tar

解壓縮 .tar.bz2 歸檔檔案

要解壓縮 .tar.gz 歸檔檔案,請使用以下命令:

$ tar -xjvf tutorialspoint.tar.bz2

示例輸出應如下所示:

home/linux/12dec/
home/linux/12dec/final_url_weight.py
home/linux/12dec/tp_Crawled_few.txt
home/linux/12dec/Final_Url_Weight.csv
home/linux/12dec/extracting_keywors.py
home/linux/12dec/FINAL_URL_WEIGHT.db
home/linux/12dec/site_health_depth5.txt
home/linux/12dec/check_ageof_site.py
home/linux/12dec/final_url_weight_sqlite.py

在不同位置提取 .tar 檔案

要將 .tar 檔案提取到不同位置,請使用以下命令:

$ tar -xvf tutorialspoint.tar -C /home/linux/abc

在上述命令中,tutorialspoint.tar 歸檔檔案提取到/home/linux/abc/位置。示例輸出應如下所示:

home/linux/12dec/
home/linux/12dec/final_url_weight.py
home/linux/12dec/tp_Crawled_few.txt
home/linux/12dec/Final_Url_Weight.csv
home/linux/12dec/extracting_keywors.py
home/linux/12dec/FINAL_URL_WEIGHT.db
home/linux/12dec/site_health_depth5.txt
home/linux/12dec/check_ageof_site.py
home/linux/12dec/final_url_weight_sqlite.py

要驗證上述命令,請使用以下命令:

/abc/home/linux/12dec$ ls

示例輸出應如下所示:

check_ageof_site.py         FINAL_URL_WEIGHT.db       site_health_depth5.txt
extracting_keywors.py       final_url_weight.py       tp_Crawled_few.txt
Final_Url_Weight.csv        final_url_weight_sqlite.py

透過本文,您將能夠了解如何在 Linux 上使用 tar 命令壓縮和解壓縮檔案。在我們的下一篇文章中,我們將提供更多基於 Linux 的技巧和提示。繼續關注!

更新於: 2019-10-22

525 次瀏覽

啟動您的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.