The best way to compress and extract files using the tar command on linux

Information TechnologyLinux

Do you frequently compress and extract files on Linux/Ubuntu? Have you heard about.tar extension? Then this article is for you to learn about compress and extract files using the tar command with examples.

What is .tar?

In computing, tar is an application utility for collecting many records into one archive file, most likely known as a tarball, for distribution or backup functions .Tar was at the beginning developed in the early days of Unix for the intent of backing up records to tape-based storage. It used to be later formalized as a part of the POSIX standard.

To get more information about tar, use the following command –

$ tar --help

The sample output should be like this –

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

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

Creating a .tar Archive File

To create a .tar archive file, use the following command –

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

In the above command, it archives 12dec directory which is placed at /home/linux/12dec as tutorialspoint.tar. To verify the above command, use the following command –

$ ls

The sample output should be like this –

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

Uncompressing .tar Archive File

To uncompressing .tar archive file, use the following command –

$ tar -xvf tutorialspoint.tar

The sample output should be like this –

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

Creating a .tar.gz Archive File

To create a .tar.gz archive file, use the following command –

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

In the above command, it archives 12dec directory, which is placed at /home/linux/12dec as tutorialspoint.tar. To verify the above command, use the following command –

$ ls

The sample output should be like this –

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

Uncompressing .tar.gz Archive File

To uncompress .tar.gz archive file, use the following command –

$ tar -xzvf tutorialspoint.tar.gz

The sample output should be like this –

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

Creating a .tar.bz2 Archive File

To create a .tar.bz2 archive file, use the following command –

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

In the above command, it archives 12dec directory, which is placed at /home/linux/12dec as tutorialspoint.tar. To verify the above command, use the following command –

$ ls

The sample output should be like this –

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

Uncompressing .tar.bz2 Archive File

To uncompress .tar.gz archive file, use the following command –

$ tar -xjvf tutorialspoint.tar.bz2

The sample output should be like this –

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

Extracting a .tar file in different Location

To extract a .tar file in different location, use the following command –

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

In the above command tutorialspoint.tar archive file extracted at /home/linux/abc/ location. the sample output should be like this –

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

To verify the above command, use the following command –

/abc/home/linux/12dec$ ls

The sample output should be like this –

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

After this article, you will be able to understand on How to Compress and Extract Files Using the tar Command on Linux. In our next articles, we will come up with more Linux based tricks and tips. Keep reading!

raja
Published on 22-Oct-2019 16:25:41
Advertisements