Cyberithub

How to use tar command in Solaris 11

Advertisements

In this tutorial I will explain about how to use tar command in Solaris 11. Tar is a widely used tool for archiving and compressing the files in Unix environment. By default if you simply create .tar file then it will be just an archive of the files which is collected and grouped together in a single .tar file. But if you want to reduce or compress the size of the files, then you need to use .tar.gz compression or .tar.bz2 compression.

As we go through our article of how to use tar command in Solaris, you will understand the different ways through which files can be compressed and uncompressed.

How to Use Tar command in Solaris 11

Before explaining about how to use tar command in Solaris, it is important to make sure that you have a running Solaris 11 System along with tar command installed. Now let's move ahead and understand how to use tar command in Solaris 11.

How to use tar command in Solaris 11 1

Also Read: How to Install and Use netstat in Linux

1. To compress the files using tar command

If you want to compress the files in .tar format, you need to use below command:-

root@localhost:~# tar -cvf files.tar test.txt hello.txt
a test.txt 0K
a hello.txt 0K

-c: this switch will be used to compress the files in tar format

v: to view the compression

f: to specify the name of tar file

Check and verify if the files.tar is created or not.

root@localhost:~# ls -lrt
total 7
-rw-r--r-- 1 root root 0 Dec 23 08:15 test.txt
-rw-r--r-- 1 root root 0 Dec 23 08:15 hello.txt
-rw-r--r-- 1 root root 2048 Dec 23 08:22 files.tar

2. To check the Contents of tar file

If you want to check the contents of tar file, you need to use tar -tvf command.

root@localhost:~# tar -tvf files.tar
tar: blocksize = 4
-rw-r--r-- 0/0 0 Dec 23 08:15 2019 test.txt
-rw-r--r-- 0/0 0 Dec 23 08:15 2019 hello.txt

-t: this switch will be used to show the contents of the tar file

3. To extract files using tar command

If you need to uncompress the files from tar file, then you need to use tar xvf command.

root@localhost:~# tar xvf files.tar
tar: blocksize = 4
x test.txt, 0 bytes, 0 tape blocks
x hello.txt, 0 bytes, 0 tape blocks

x: this switch will be used to uncompress the tar file

Check and verify if it has extracted all the files in current folder or not

root@localhost:~# ls -lrt
total 7
-rw-r--r-- 1 root root 0 Dec 23 08:15 test.txt
-rw-r--r-- 1 root root 0 Dec 23 08:15 hello.txt
-rw-r--r-- 1 root root 2048 Dec 23 08:22 files.tar

4. To add an extra file in already existing tar file

If you want to add another file in already existing tar compression, you can use tar rvf command.

root@localhost:~# tar rvf files.tar hello.txt
a hello.txt 0K

r: this switch will be used to add an extra file in tar file

Check and verify if hello.txt file is added to files.tar or not

root@localhost:~# tar tvf files.tar
tar: blocksize = 4
-rw-r--r-- 0/0 0 Dec 23 08:15 2019 test.txt
-rw-r--r-- 0/0 0 Dec 23 08:15 2019 hello.txt

5. To extract a single file from tar file

If you want to extract only a single file from files.tar compression, then you need to use below command:-

root@localhost:~# tar xvf files.tar "test.txt"
tar: blocksize = 4
x test.txt, 0 bytes, 0 tape blocks

Check and verify if it has only extracted test.txt file from files.tar or not.

root@localhost:~# ls -lrt
total 7
-rw-r--r-- 1 root root 0 Dec 23 08:15 test.txt

6. To zip the file along with tar compression

If you want to compress the files in .tar.gz format, you need to use tar cvzf command.

root@localhost:~# tar cvzf files.tar.gz test.txt hello.txt
Compressing 'files.tar.gz' with '/usr/bin/gzip'...
a test.txt 0K
a hello.txt 0K

cz: this switch will be used to compress in .tar.gz format.

Check and verify if the file is indeed compressed with .tar.gz or not.

root@localhost:~# tar tvf files.tar.gz
-rw-r--r-- 0/0 0 Dec 23 08:15 2019 test.txt
-rw-r--r-- 0/0 0 Dec 23 08:15 2019 hello.txt

7. To bzip the file along with tar compression

If you need to compress the files in .tar.bz2 compression, you need to use tar cvjf command.

root@localhost:~# tar cvjf files.tar.bz2 test.txt hello.txt
Compressing 'files.tar.bz2' with '/usr/bin/bzip2'...
a test.txt 0K
a hello.txt 0K

cj: this switch will be used to compress in tar.bz2 format

Check and verify if the file is indeed compressed with .tar.bz2 or not.

root@localhost:~# tar tvf files.tar.bz2
-rw-r--r-- 0/0 0 Dec 23 08:15 2019 test.txt
-rw-r--r-- 0/0 0 Dec 23 08:15 2019 hello.txt

8. To extract contents from .tar.gz file

If you want to extract contents from .tar.gz conmpression, then you need to use tar xzvf command.

root@localhost:~# tar xzvf files.tar.gz test.txt hello.txt
x test.txt, 3893 bytes, 8 tape blocks
x hello.txt, 3893 bytes, 8 tape blocks

xz: this switch will be used to uncompress file from .tar.gz compression

Check and verify if the files are extracted from files.tar.gz or not.

root@localhost:~# ls -lrt
total 29
-rw-r--r-- 1 root root 3893 Dec 23 09:26 test.txt
-rw-r--r-- 1 root root 3893 Dec 23 09:26 hello.txt
-rw-r--r-- 1 root root 2050 Dec 23 09:52 files.tar.gz

9. To extract contents from .tar.bzip2 file

If you want to extract contents from .tar.bzip2 compression, you need to use tar xjvf command.

root@localhost:~# tar xjvf files.tar.bzip2 test.txt hello.txt
x test.txt, 3893 bytes, 8 tape blocks
x hello.txt, 3893 bytes, 8 tape blocks

Check  and verify the contents of files.tar.bzip2 is extracted or not.

root@localhost:~# ls -lrt
total 28
-rw-r--r-- 1 root root 3893 Dec 23 09:26 test.txt
-rw-r--r-- 1 root root 3893 Dec 23 09:26 hello.txt
-rw-r--r-- 1 root root 1798 Dec 23 09:55 files.tar.bzip2

10. To exclude certain files during tar compression

If you want to exclude certain files while compressing group of files, then you need to use below command:-

root@localhost:~# tar cvfX files.tar test.txt hello.txt
a hello.txt 0K

X: to exclude certain files

Check and Verify if test.txt is excluded during compression or not.

root@localhost:~# tar tvf files.tar
tar: blocksize = 3
-rw-r--r-- 0/0 0 Dec 23 08:15 2019 hello.txt

11. To check the size of each file in tar compression

If you want to check the size of each and every file present in files.tar, you to take the help of awk command.

root@localhost:~# tar tvf files.tar | for i in $(awk '{ print $8 };');do du -sh $i;done
4K hello.txt
4K test.txt

awk '{ print $8};' - It will take a file in input and hold in $i variable in each iteration

du -sh $i - It will show you the size of file contained in variable $i

12. To check the last access time of files without Uncompressing 

If you want to check the last access time of the files present inside files.tar without uncompressing it, you can do that using below command.

root@localhost:~# tar tvf files.tar | for i in $(awk '{ print $8 };');do ls -lrt $i;done
-rw-r--r-- 1 root root 3893 Dec 23 09:26 hello.txt
-rw-r--r-- 1 root root 3893 Dec 23 09:26 test.txt

awk '{ print $8};' - It will take a file in input and hold in $i variable in each iteration

ls -lrt $i - It will show you the last access time of the file contained in variable $i

13. To count the number of files present inside tar file

If you want to count the number of files present inside files.tar, you can use wc -l command to count it.

root@localhost:~# tar tvf files.tar | wc -l
2

Reference: Tar command help page

Here you have seen how to use tar command in Solaris. In the next article, I will show you most frequently used Solaris commands. This is it from how to use tar command in Solaris.

2 thoughts on “How to use tar command in Solaris 11”

    • Hi Aslam,

      You are absolutely correct. There is not much difference in using tar on Solaris 11 and Linux.

      Thanks
      CyberITHub

      Reply

Leave a Comment