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
-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
-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:
--gzip : Used for Archival operation for .gzip type
--bzip2: Used for archival operation for .bzip2 type
--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
[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
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
How do I open a tar file in Linux?
- Let’s consider that we want to extract and open a doc file, and then we can use the below command to unzip the
- file on Linux:
- tar –xvzf doc.tar.gz. Remember that the tar. …
- tar –cvzf docs.tar.gz ~/Documents. …
- tar -cvf documents.tar ~/Documents. …
- tar –xvf docs.tar. …
- gzip xyz.txt. …
- gunzip test.txt. …
- gzip *.txt.
How do I view the contents of a tar file in Unix?
tar File Content. Use -t switch with tar command to list content of a archive. tar file without actually extracting.
How do I view a tar file?
How to open TAR files
- Download and save the TAR file to your computer. …
- Launch WinZip and open the compressed file by clicking File > Open. …
- Select all of the files in the compressed folder or select only the files you want to extract by holding the CTRL key and left-clicking on them.
Where can I find tar files in Linux?
Combining find and tar commands so that we can find and tar files into a tarball
- -name “*. doc” : Find file as per given pattern/criteria. In this case find all *. doc files in $HOME.
- -exec tar … : Execute tar command on all files found by the find command.
How do I extract a tar file?
To extract (unzip) a tar. gz file simply right-click on the file you want to extract and select “Extract”. Windows users will need a tool named 7zip to extract tar.
How do I unzip a tar file in Terminal?
The most common uses of the tar command are to create and extract a tar archive. To extract an archive, use the tar -xf command followed by the archive name, and to create a new one use tar -czf followed by the archive name and the files and directories you want to add to the archive.
How do I extract a tar file from a directory in Linux?
Syntax For Tar Command To Extract Tar Files To a Different Directory
- x : Extract files.
- f : Tar archive name.
- –directory : Set directory name to extract files.
- -C : Set dir name to extract files.
- -z : Work on . tar. …
- -j : Work on . tar. …
- -J (capital J ) : Work on . tar. …
- -v : Verbose output i.e. show progress on screen.
Can 7 Zip open tar files?
7-Zip can also be used to unpack many other formats and to create tar files (amongst others). Download and install 7-Zip from 7-zip.org. … Move the tar file to the directory you wish to unpack into (usually the tar file will put everything into a directory inside this directory).
What is tar file in Linux?
The Linux ‘tar’ stands for tape archive, is used to create Archive and extract the Archive files. tar command in Linux is one of the important command which provides archiving functionality in Linux. We can use Linux tar command to create compressed or uncompressed Archive files and also maintain and modify them.