The Linux tar command abbreviation is “tar archive” released under POSIX standards initially and It now follows the GNU standards
Often when you download the files from the internet they are in the .tar, tar.gz. Or tar.bz2 compressed format using either bz2 or gz compression algorithm.
If you are familiar with the Opensource then there’s a good chance that you would have come across the extensions like Package-name.tar, tar.gz, tar.bz, tar.xz which are standard.
Well most open source software use tarballs to distribute programs/source codes in this format as it offers efficient compression and better organized way of grouping files.
It supports a vast range of compression programs such as gzip, bzip2, xz, lzip, lzma, lzop.
In the following tutorial we will show how to Compress/Extract the files into tar.gz or tgz.
How to specify the format POSIX Or GNU while using tar Command?
[vamshi@linuxcent ]$ tar --format=posix -cf posix.tar * [vamshi@linuxcent ]$ tar --format=gnu -cf gnu.tar * [vamshi@linuxcent ]$ file posix.tar gnu.tar posix.tar: POSIX tar archive gnu.tar: POSIX tar archive (GNU)
GNU is based on an older POSIX format, So that’s why it says POSIX for both.
To print the contents information of an archived directory, use the directory name as a file name argument in conjunction with --list (-t)
. To find out file attributes, include the --verbose (-v)
option.
Firstly start off by long listing ls
in the present directory we have here:
vamshi@LinuxCent:~/Linux> ls Debian-Distro/ OpenSuse-Distro/ README Redhat-Distro/
We will now compress the present working directory using Linux GNU’s tar command line utility.. Creating a tar of the current directory mentioned by Asterisk *
. The Options -c
creates, -v
: Verbose mode, -z
: Uses GZIP algorithm
Demonstration shown below:
Either one of the following commands can be used
vamshi@LinuxCent:~/Linux> tar --gzip -cvf Linux-flavours.tar.gz * vamshi@LinuxCent:~/Linux> tar -cvzf Linux-flavours.tar.gz * Debian-Distro/ Debian-Distro/ubuntu.txt Debian-Distro/debian.txt Debian-Distro/README-Debian-Distro Linux-flavours.tar.gz OpenSuse-Distro/ OpenSuse-Distro/README-Opensuse-Distro OpenSuse-Distro/opensuse.txt README Redhat-Distro/ Redhat-Distro/Fedora/ Redhat-Distro/Fedora/fedora.txt Redhat-Distro/Centos/ Redhat-Distro/Centos/centos.txt Redhat-Distro/Centos/CentOS-versions/ Redhat-Distro/Centos/CentOS-versions/centos7.txt Redhat-Distro/Centos/CentOS-versions/centos5.5.txt Redhat-Distro/Centos/CentOS-versions/centos6.9.txt Redhat-Distro/Centos/CentOS-versions/centos5.8.txt Redhat-Distro/Centos/CentOS-versions/centos6.1.txt Redhat-Distro/Centos/README-CentOS Redhat-Distro/README-Redhat-Distro Redhat-Distro/RHEL-Versions/ Redhat-Distro/RHEL-Versions/redhat5.txt Redhat-Distro/RHEL-Versions/redhat7.txt Redhat-Distro/RHEL-Versions/redhat6.txt Redhat-Distro/RHEL-Versions/redhat8.txt Redhat-Distro/redhat.txt
How to List/view Archives
The option-t
does a Dry-run of extract operation but only to print the contents of the mentioned compression.
vamshi@LinuxCent:~/Linux> tar -tvf Linux-flavours.tar.gz Debian-Distro/ Debian-Distro/ubuntu.txt Debian-Distro/debian.txt Debian-Distro/README-Debian-Distro …
How to Extracting tar.gz File?
Extracting the tar file using the options -x
works out just fine as -x option chooses the tye of decompression based on the compression file type, and the content will be extracted to current working directory.
Here are various extraction options:
$ tar -xvf Linux-flavours.tar.gz $ tar -zxvf Linux-flavours.tar.gz $ tar --gzip -xvf Linux-flavours.tar.gz
The Filter / Options for compression types:
-z
or --gzip
: Used for Archival operation for .gzip type
-j
-r --bzip2
: Used for archival operation for .bzip2 type
[/code]-J[/code] or --xz
: User for Archival for .xz type
How to Extract Only Specific Files from a compressed tar archive (tar) File
This doesn’t require any special option but you have to name the exact file and directory that you want to extract
[vamshi@linuxcent ~]$ tar -zxvf redmine-4.0.6.tar.gz redmine-4.0.6/bin/about redmine-4.0.6/bin/about
Successful in extracting only the redmine-4.0.6/bin/about
file from the redmine-4.0.6.tar.gz
archive.
[vamshi@linuxcent ~]$ ls -l redmine-4.0.6/bin/about -rwxrwxr-x. 1 vamshi vamshi 167 Dec 20 11:46 redmine-4.0.6/bin/about
How to Extract specific files or Directories from the archive
vamshi@LinuxCent:/tmp/linux-test> tar -zxvf Linux-flavours.tar.gz Redhat-Distro/Centos/ Redhat-Distro/Centos/ Redhat-Distro/Centos/centos.txt Redhat-Distro/Centos/CentOS-versions/ Redhat-Distro/Centos/CentOS-versions/centos7.txt Redhat-Distro/Centos/CentOS-versions/centos5.5.txt Redhat-Distro/Centos/CentOS-versions/centos6.9.txt Redhat-Distro/Centos/CentOS-versions/centos5.8.txt Redhat-Distro/Centos/CentOS-versions/centos6.1.txt Redhat-Distro/Centos/README-CentOS
This way we are able to extract only the specific directories pertaining to Centos from Redhat-Distro.
Now we would do the extraction of the contents on Redhat-Distro and its sub directories.
Extracting Sub-directories inside the compressed tar.gz
How to Extract TAR file Contents to a New directory?
The tar program by default, extracts the archive contents to the present working directory.
By specifying the option --directory (-C)
You can extract archive files in a Target directory.
vamshi.santhapuri@LinuxCent:/tmp/linux-test1> tar -xzvf ~/Linux/jdk-8u101-linux-x64.tar.gz -C /usr/local/
The above operation extracts the java bundle to /usr/local/ directory.
How to extract only the specific directory from the Compression to a Target directory?
vamshi.santhapuri@LinuxCent:/tmp/linux-test1> tar -xzvf ~/Linux/Linux-flavours.tar.gz Redhat-Distro/RHEL-Versions/ -C /tmp/linux-test Redhat-Distro/RHEL-Versions/ Redhat-Distro/RHEL-Versions/redhat5.txt Redhat-Distro/RHEL-Versions/redhat7.txt Redhat-Distro/RHEL-Versions/redhat6.txt Redhat-Distro/RHEL-Versions/redhat8.txt
How to tar compress the specific Files and Directory and Sub-directories / Multiple Directories using tar command?
Below is the Demonstration of compression program using gz compression.
vamshi@LinuxCent:~/Linux> tar -cvzf Redhat-Distro/ Linux-flavours.tar.gz centos.txt fedora.txt opensuse.txt redhat.txt ubuntu.txt centos.txt fedora.txt opensuse.txt redhat.txt Redhat-Distro/centos.txt Redhat-Distro/fedora.txt Redhat-Distro/redhat.txt Redhat-Distro/Redhat-Versions/ Redhat-Distro/Redhat-Versions/redhat5.txt Redhat-Distro/Redhat-Versions/redhat7.txt Redhat-Distro/Redhat-Versions/redhat6.txt Redhat-Distro/Redhat-Versions/redhat8.txt
You can also compress multiple directories and files into a single tar.gz file as demonstrated below
How to exclude particular directories and file from the compression using tar command?
We can make use of the --exclude="DontIncludethisPath"
in Linux tar command, where in the base directory of mentioned pattern are excluded.. Lets run the tar command and see the results
vamshi@linuxCent:~/Linux/OSes> tar -czvf exclude-flavours.tar.gz --exclude="Redhat-Distro" . ./ ./OpenSuse-Distro/ ./OpenSuse-Distro/README-Opensuse-Distro ./OpenSuse-Distro/opensuse.txt ./Debian-Distro/ ./Debian-Distro/ubuntu.txt ./Debian-Distro/debian.txt ./Debian-Distro/README-Debian-Distro tar: .: file changed as we read it
The compression successfully completed and now lets list out the files
vamshi@linuxCent:~/Linux/OSes> ls -lthr total 16K drwxr-xr-x 2 vamshi users 4.0K Apr 8 14:09 OpenSuse-Distro drwxr-xr-x 2 vamshi users 4.0K Apr 8 14:09 Debian-Distro drwxr-xr-x 5 vamshi users 4.0K Apr 8 14:09 Redhat-Distro -rw-r--r-- 1 vamshi users 718 Apr 8 19:18 exclude-flavours.tar.gz
From the dry-run extract output of the compression does not contain the Directory Redhat-Distro
vamshi@linuxCent:~/Linux/OSes> tar -tvf exclude-flavours.tar.gz drwxr-xr-x vamshi/users 0 2020-04-08 19:18 ./ drwxr-xr-x vamshi/users 0 2020-04-08 14:09 ./OpenSuse-Distro/ -rw-r--r-- vamshi/users 0 2020-04-08 14:09 ./OpenSuse-Distro/README-Opensuse-Distro -rw-r--r-- vamshi/users 9 2020-04-08 13:22 ./OpenSuse-Distro/opensuse.txt drwxr-xr-x vamshi/users 0 2020-04-08 14:09 ./Debian-Distro/ -rw-r--r-- vamshi/users 7 2020-04-08 13:22 ./Debian-Distro/ubuntu.txt -rw-r--r-- vamshi/users 7 2020-04-08 13:50 ./Debian-Distro/debian.txt -rw-r--r-- vamshi/users 0 2020-04-08 14:09 ./Debian-Distro/README-Debian-Distro