In this post we will see how to use the tar command to compress and uncompress your files and directories. Many softwares are distributed by compressing them using tar so the examples below will guide you how you can uncompress or open such files. This command is also very useful when you have to archive or share huge directories or files.
Create a tar
In the below example we have a directory called cloudvedas which has multiple files. Let's tar this directory. With -v option you can see what all files it is adding in the tar ball.
-v: Verbose output shows you all the files being archived.
-f: Allows you to specify the filename of the archive.
cvterm#tar -cvf cloudvedas.tar cloudvedas
cloudvedas/
cloudvedas/cv1.sh
cloudvedas/cv2.sh
cloudvedas/cv3
cloudvedas/cv4
cloudvedas/cv5.py
cvterm#ls -lrth
total 12K
drwxr-xr-x 1 sagu sagu 4.0K Jun 16 08:19 cloudvedas
-rw-r--r-- 1 sagu sagu 10K Jun 16 08:20 cloudvedas.tar
cvterm#
List or View contents of a tar
Now if you want to check all the files which are in the tar ball without doing untar/opening/uncompressing you can check it as below
cvterm#tar --list -f cloudvedas.tar
cloudvedas/
cloudvedas/cv1.sh
cloudvedas/cv2.sh
cloudvedas/cv3
cloudvedas/cv4
cloudvedas/cv5.py
cvterm#
Another way to see the contents of a tar ball without untar is using -tvf option like below. This will show the permissions also.
cvterm#tar -tvf cloudvedas.tar
drwxr-xr-x sagu/sagu 0 2021-06-16 08:19 cloudvedas/
-rw-r--r-- sagu/sagu 0 2021-06-16 08:19 cloudvedas/cv1.sh
-rw-r--r-- sagu/sagu 0 2021-06-16 08:19 cloudvedas/cv2.sh
-rw-r--r-- sagu/sagu 0 2021-06-16 08:19 cloudvedas/cv3
-rw-r--r-- sagu/sagu 0 2021-06-16 08:19 cloudvedas/cv4
-rw-r--r-- sagu/sagu 0 2021-06-16 08:19 cloudvedas/cv5.py
cvterm#
Create tar.gz or .tgz which is tar and gzip in single command.
Now let's try to gzip the directory along with tar in a single command. We will do this with option -z .
-z: Compress the archive with gzip.
cvterm#tar -cvzf cloudvedas.tar.gz cloudvedas
We can see below a .tar.gz is created cloudvedas.tar.gz
cvterm#ls -lrth
total 8.0K
drwxr-xr-x 1 sagu sagu 4.0K Jun 17 19:34 cloudvedas
-rw-r--r-- 1 sagu sagu 5.5K Jun 17 19:34 cloudvedas.tar.gz
cvterm#
We can also create .tgz which is same as .tar.gz
cvterm#tar -cvzf cloudvedas.tgz cloudvedascvterm#ls -lrth
total 16K
drwxr-xr-x 1 sagu sagu 4.0K Jun 17 19:34 cloudvedas
-rw-r--r-- 1 sagu sagu 5.5K Jun 17 19:34 cloudvedas.tar.gz
-rw-r--r-- 1 sagu sagu 5.5K Jun 17 19:36 cloudvedas.tgz
cvterm#
Create a tar.bz2 or tbz or tb2
The tar.bz2 or tbz or tb2 creates a file which is more compressed as compared to gz but these 3 extensions takes more time to compress and uncompress. If you see the file size listed below you can see clear difference.
cvterm#tar -cvjf cloudvedas.tar.tb2 cloudvedas
or
cvterm#tar -cvjf cloudvedas.tar.tbz cloudvedas
or
cvterm#tar -cvjf cloudvedas.tar.bz2 cloudvedas
cvterm#ls -lrthtotal 5.1Mdrwxr-xr-x 1 sagu sagu 4.0K Jun 17 19:34 cloudvedas-rw-r--r-- 1 sagu sagu 5.5K Jun 17 19:34 cloudvedas.tar.gz-rw-r--r-- 1 sagu sagu 5.5K Jun 17 19:36 cloudvedas.tgz-rw-r--r-- 1 sagu sagu 332 Jun 17 19:37 cloudvedas.tar.tb2-rw-r--r-- 1 sagu sagu 332 Jun 17 19:38 cloudvedas.tar.tbz-rw-r--r-- 1 sagu sagu 332 Jun 17 19:38 cloudvedas.tar.bz2-rw-r--r-- 1 sagu sagu 5.1M Jun 17 19:44 cloudvedas.tarcvterm#
List / View the contents of .tar.gz .tgz .tar.bz2 .tar.tbz .tar.tb2
You can simply view the contents of all these file types by same option of -tvf
cvterm#tar -tvf cloudvedas.tar.gzorcvterm#tar -tvf cloudvedas.tgzorcvterm#tar -tvf cloudvedas.tar.tbzorcvterm#tar -tvf cloudvedas.tar.bz2orcvterm#tar -tvf cloudvedas.tar.tb2
Extract or Untar a file
To untar a file in the current directory do below
-f: It tells tar the name and path of the compressed file.
-x: Extract the files.
-v: Verbose output shows you all the files being extracted.
cvterm#tar -xvf cloudvedas.tar
To untar a file in a different directory use option -C
cvterm#tar -xvf cloudvedas.tar -C /var/tmp/newdir
cvterm#pwd
/var/tmp/newdir
cvterm#ls
cloudvedas
cvterm#
Now let's extract single file from tar
cvterm#tar -xvf cloudvedas.tar cloudvedas/file5.txt
Uncompress and untar .tar.gz or .tgz
cvterm#tar -xvf cloudvedas.tar.gz
or
cvterm#tar -xvf cloudvedas.tgz
Uncompress and untar single file .tar.gz or .tgz
cvterm#tar -zxvf cloudvedas.tar.gz cloudvedas/file4.txt
cloudvedas/file4.txt
cvterm#tar -zxvf cloudvedas.tgz cloudvedas/file5.txt
cloudvedas/file5.txt
Uncompress and untar .tar.gz or .tgz in a different directory
cvterm#tar -xvf cloudvedas.tar.gz -C /home/sagu/newdir/
or
cvterm#tar -xvf cloudvedas.tgz -C /home/sagu/newdir/
Uncompress and untar tar.bz2, tar.tbz, tar.tb2
cvterm#tar -xvf cloudvedas.tar.bz2
or
cvterm#tar -xvf cloudvedas.tar.tbz
or
cvterm#tar -xvf cloudvedas.tar.tb2
Extract multiple files from .tar
cvterm#tar -xvf cloudvedas.tar "cloudvedas/file.txt" "cloudvedas/file2.txt"
cloudvedas/file.txt
cloudvedas/file2.txt
Extract multiple files from tar.gz .tgz using -z option
-z: Decompress the archive using gzip
cvterm#tar -zxvf cloudvedas.tar.gz "cloudvedas/file3.txt" "cloudvedas/file4.txt"
or
cvterm#tar -zxvf cloudvedas.tgz "cloudvedas/file3.txt" "cloudvedas/file4.txt"
Extract multiple files from tar.bz2, tar.tbz, tar.tb2 using -j option
cvterm#tar -jxvf cloudvedas.tar.bz2 "cloudvedas/file5.txt" "cloudvedas/cv1.sh"
or
cvterm#tar -jxvf cloudvedas.tbz "cloudvedas/file5.txt" "cloudvedas/cv1.sh"
or
cvterm#tar -jxvf cloudvedas.tar.tb2 "cloudvedas/file5.txt" "cloudvedas/cv1.sh"
Extract group of files using --wildcard option
.tar
cvterm#tar -xvf cloudvedas.tar --wildcards '*.sh'
.tar.gz .tgz using -z option
cvterm#tar -zxvf cloudvedas.tar.gz --wildcards '*.sh'
cvterm#tar -zxvf cloudvedas.tgz --wildcards '*.sh'
tar.bz2, tar.tbz, tar.tb2 using -j option
cvterm#tar -jxvf cloudvedas.tar.bz2 --wildcards '*.txt'
cvterm#tar -jxvf cloudvedas.tar.tbz --wildcards '*.txt'
cvterm#tar -jxvf cloudvedas.tar.tb2 --wildcards '*.txt'
Add a new file or directory in .tar archive using -r option
cvterm#tar -rvf cloudvedas.tar cvnew1.sh ##Adding a file
cvterm#tar -rvf cloudvedas.tar newdir1 ## Adding dir
Check below that now we have new file and dir added in our .tar
cvterm#tar -tvf cloudvedas.tar
drwxr-xr-x sagu/sagu 0 2021-06-17 19:34 cloudvedas/
-rw-r--r-- sagu/sagu 0 2021-06-16 08:19 cloudvedas/cv1.sh
-rw-r--r-- sagu/sagu 0 2021-06-16 08:19 cloudvedas/cv2.sh
-rw-r--r-- sagu/sagu 0 2021-06-16 08:19 cloudvedas/cv3
-rw-r--r-- sagu/sagu 0 2021-06-16 08:19 cloudvedas/cv4
-rw-r--r-- sagu/sagu 0 2021-06-16 08:19 cloudvedas/cv5.py
drwxr-xr-x sagu/sagu 0 2021-06-17 19:17 cloudvedas/cvdir1/
drwxr-xr-x sagu/sagu 0 2021-06-17 19:17 cloudvedas/cvdir2/
-rw-r--r-- sagu/sagu 1048576 2021-06-17 19:33 cloudvedas/file.txt
-rw-r--r-- sagu/sagu 1048576 2021-06-17 19:34 cloudvedas/file2.txt
-rw-r--r-- sagu/sagu 1048576 2021-06-17 19:34 cloudvedas/file3.txt
-rw-r--r-- sagu/sagu 1048576 2021-06-17 19:34 cloudvedas/file4.txt
-rw-r--r-- sagu/sagu 1048576 2021-06-17 19:34 cloudvedas/file5.txt
-rw-r--r-- sagu/sagu 0 2021-06-18 19:27 cvnew1.sh
drwxr-xr-x sagu/sagu 0 2021-06-18 19:26 newdir1/
cvterm#
Note: You cannot add files or dir to tar.gz, .tgz, tar.bz2, tar.tbz, tar.tb2 archives
Exclude specific file or directories while creating a tar
cvterm#tar -cf cloudvedasexclude.tar --exclude='cloudvedas/cvdir1' --exclude='cloudvedas/file2.txt' cloudvedas
As we can see below the file and directory we excluded are not in the tar.
cvterm#tar -tvf cloudvedasexclude.tar
drwxr-xr-x sagu/sagu 0 2021-06-18 19:25 cloudvedas/
-rw-r--r-- sagu/sagu 0 2021-06-16 08:19 cloudvedas/cv1.sh
-rw-r--r-- sagu/sagu 0 2021-06-16 08:19 cloudvedas/cv2.sh
drwxr-xr-x sagu/sagu 0 2021-06-17 19:17 cloudvedas/cvdir2/
-rw-r--r-- sagu/sagu 1048576 2021-06-17 19:33 cloudvedas/file.txt
-rw-r--r-- sagu/sagu 1048576 2021-06-17 19:34 cloudvedas/file3.txt
-rw-r--r-- sagu/sagu 1048576 2021-06-17 19:34 cloudvedas/file4.txt
-rw-r--r-- sagu/sagu 1048576 2021-06-17 19:34 cloudvedas/file5.txt
cvterm#
Above we have seen some examples of frequently used tar command options. You can also use tar command to convert your vmware ova files to ovf . Check this post on how to convert ova to ovf using tar.
There are lot more options in tar that you can see using tar --help in your terminal. Do let us know in the comment section if you want us to add any other examples.