DronaBlog

Thursday, August 23, 2018

How to use 'tar' commands in Unix?



Are you looking for various commands in the Unix environment? Are you also looking for what are structures and samples for each command in the Unix system? If so, then this article provides detailed information about command details with its usage. In this article we will focus on the 'tar' command.


'tar' command:

What is the 'tar' command and why is it used? In Unix, 'tar' is the abbreviation for Tape ARchive.  This command is used to store entire file systems onto the magnetic tape. The command 'tar' is also commonly used to combine the multiple files into a single file for easy storage and distribution.

Below are commonly used 'tar' commands -




A.Commands dealing with .tar files

1. The command 'tar cvf' is used to create a new tar archive file. With the 'tar' command, the files are not compressed. The files are only combined and grouped into a single archive file.
$ tar cvf archive_abc.tar dirabc/

Here, 
c - Create a new archive
v – Verbosely list files
f – Following is the archive file name

2. The command 'tar xvf' is used to extract an existing archive file.
$ tar xvf archive_abc.tar

Here,
x – Extract files from the archive file

3. The command 'tar xvf' is used to extract a single file 'file_abc' from the existing archive file.
$ tar xvf archive_abc.tar /location/to/file_abc

4. The command 'tar xvf' is used to extract a single directory 'abcdir' from the existing archive file. The subdirectories and files are extracted with this command.
$ tar xvf archive_abc.tar /location/to/abcdir/

To extract multiple directories use the command below 
$ tar xvf archive_abc.tar /location/to/abcdir1/ /location/to/abcdir1/

5. The command 'tar xvf' is used to extract multiple files from the .tar file. We can use Regular Expression to achieve it. The command below will list all files with extension 'txt'
$ tar xvf archive_abc.tar --wildcards '*.txt'

6. The command 'tar tvf' is used to view the archive file. This will list the files without extracting files.
$ tar tvf archive_abc.tar

7. The command 'tar rvf' is used to add a new file in an existing .tar file. With the command below we can add 'file_pqr' file to archive_abc.tar file.
$ tar rvf archive_abc.tar file_pqr

We can use the command below to add the directory to the existing archive (.tar) file 
$ tar rvf archive_abc.tar dir_xyz/

Note: We cannot add files or directories to compressed files such as .gz or .bz2 file. If we try to add then we will get the 'Cannot update compressed archives' error message.

8. The command 'tar tvfW' is used to verify the archive (.tar) files. Normally we use this command before removing any file from the archive file.
$ tar tvfW file_xyz.tar

Note:
a. If an output line starts with Verify with no differs line then the file/directory is Ok. If that is not the case, then we need to fix the issue.
b. We cannot verify files in a compressed the archive file. (e.g. .gz, .bz2)

9. The command 'tar -cf' is used to determine the size of the .tar file. The size returned is in KB.
$ tar -cf - /location/to/archive/ | wc -c
20660




B.Commands dealing with .gz files

1. The command 'tar cvzf' is used create the gzipped archive file. With the help of this command the files are compressed and grouped into a single file. The file extension .tar.gz and .tgz are both the same.
$ tar cvzf archive_abc.tar.gz dirabc/
Here,
z - Archive through gzip

2. The command 'tar xvfz' is used to extract the existing gzipped file.
$ tar xvfz archive_abc.tar.gz

3. The command 'tar xvfz' is used to extract a single file 'file_abc' from the existing gzipped file.
$ tar xvfz archive_abc.tar.gz /location/to/file_abc

4. The command 'tar xvfz' is used to extract a single directory 'abcdir' from the existing gzipped file. The subdirectories and files are extracted with this command.
$ tar xvfz archive_abc.tar.gz /location/to/abcdir/

To extract multiple directories use the command below 
$ tar xvfz archive_abc.tar.gz /location/to/abcdir1/ /location/to/abcdir1/

5. The command 'tar xvfz' is used to extract multiple files from the .gz file. The command below will list all files with the extension 'txt' with the help of RegExp.
$ tar xvfz archive_abc.tar.gz --wildcards '*.txt'

6. The command 'tar tvfz' is used to view the .gz archive file. This will list the files without extracting files.
$ tar tvfz archive_abc.tar.gz

7. The command 'tar -czf' is used to determine the size of .gz file. The size returned is in KB.
$ tar -czf - /location/to/archive/ | wc -c
200


C.Commands dealing with .bz2 files

1. The command 'tar cvfj' is used to create a bzipped tar archive file. The size of bzip2 is lesser than gzip. Normally the creation of the bzip2 file takes more time to compress and decompress than gzip.
$ tar cvfj archive_abc.tar.bz2 dirabc/

Here, 
j – Filter the archive through bzip2
Bz2 - It is the abbreviation for Burrows-Wheeler compression 

2. The command 'tar xvfj' is used to extract the existing bzipped file.
$ tar xvfj archive_abc.tar.bz2

3. The command 'tar xvfj' is used to extract a single file 'file_abc' from the existing bzipped file.
$ tar xvfj archive_abc.tar.bz2 /location/to/file_abc

4. The command 'tar xvfj' is used to extract a single directory 'abcdir' from the existing bzipped file. The subdirectories and files are extracted with this command.
$ tar xvfj archive_abc.tar.bz2 /location/to/abcdir/

To extract multiple directories use the command below 
$ tar xvfj archive_abc.tar.bz2 /location/to/abcdir1/ /location/to/abcdir1/

5. The command 'tar xvfj' is used to extract multiple files from .bz2 file. The command below will list all files with the extension 'txt' with the help of RegExp.
$ tar xvfj archive_abc.tar.bz2 --wildcards '*.txt'

6. The command 'tar tvfj' is used to view the .bz2 archive file. This will list the files without extracting files.
$ tar tvfj archive_abc.tar.bz2

7. The command 'tar -cjf' is used to determine the size of .bz2 file. The size returned is in KB.
$ tar -cjf - /location/to/archive/ | wc -c
200 

The video below provides details about commands in the Unix environment:





No comments:

Post a Comment

Please do not enter any spam link in the comment box.

What is CRM system?

  In the digital age, where customer-centricity reigns supreme, businesses are increasingly turning to advanced technologies to manage and n...