Tags

,

1. Creating an archive using tar command
Creating an uncompressed tar archive using option cvf
This is the basic command to create a tar archive.

[root@localhost home]# tar cvf archive_name.tar dirname/

In the above command:
c – create a new archive
v – verbosely list files which are processed.
f – following is the archive file name

2. Creating a tar gzipped archive using option cvzf
The above tar cvf option, does not provide any compression. To use a gzip compression on the tar archive, use the z option as shown below.

[root@localhost home]# tar cvzf archive_name.tar.gz dirname/
  • z – filter the archive through gzip

Note: .tgz is same as .tar.gz
I like to keep the ‘cvf’ (or tvf, or xvf) option unchanged for all archive creation (or view, or extract) and add additional option at the end, which is easier to remember. i.e cvf for archive creation, cvfz for compressed gzip archive creation, cvfj for compressed bzip2 archive creation etc., For this method to work properly, don’t give – in front of the options.

3. Creating a bzipped2 tar archive using option cvjf

[root@localhost home]# tar cvfj archive_name.tar.bz2 dirname/
  • j – filter the archive through bzip2

Note: gzip vs bzip2: bzip2 takes more time to compress and decompress than gzip. bzip2 archival size is less than gzip.
.tbz and .tb2 is same as .tar.bz2

4. Extract a *.tar file using option xvf

[root@localhost home]# tar xvf archive_name.tar
  • x – extract files from archive

5. Extract a gzipped tar archive ( *.tar.gz ) using option xvzf
Use the option z for uncompressing a gzip tar archive.

[root@localhost home]# tar xvfz archive_name.tar.gz

6. Extracting a bzipped tar archive ( *.tar.bz2 ) using option xvjf
Use the option j for uncompressing a bzip2 tar archive.

[root@localhost home]# tar xvfj archive_name.tar.bz2

7. View the tar archive file content without extracting using option tvf
You can view the *.tar file content before extracting as shown below.

[root@localhost home]# tar tvf archive_name.tar

8. View the *.tar.gz file content without extracting using option tvzf
You can view the *.tar.gz file content before extracting as shown below.

[root@localhost home]# tar tvfz archive_name.tar.gz

9. View the *.tar.bz2 file content without extracting using option tvjf
You can view the *.tar.bz2 file content before extracting as shown below.

[root@localhost home]# tar tvfj archive_name.tar.bz2

10. Extract a single file from tar, tar.gz, tar.bz2 file
To extract a specific file from a tar archive, specify the file name at the end of the tar xvf command as shown below. The following command extracts only a specific file from a large tar file.

[root@localhost home]# tar xvf archive_file.tar /path/to/file
[root@localhost home]# tar xvfz archive_file.tar.gz /path/to/file
[root@localhost home]# tar xvfj archive_file.tar.bz2 /path/to/file

Use the relevant option z or j according to the compression method gzip or bzip2 respectively as shown below.

11. Extract a single directory from tar, tar.gz, tar.bz2 file
To extract a single directory (along with it’s subdirectory and files) from a tar archive, specify the directory name at the end of the tar xvf command as shown below. The following extracts only a specific directory from a large tar file.

[root@localhost home]# tar xvf archive_file.tar /path/to/dir/

To extract multiple directories from a tar archive, specify those individual directory names at the end of the tar xvf command as shown below.

[root@localhost home]# tar xvf archive_file.tar /path/to/dir1/ /path/to/dir2/

Use the relevant option z or j according to the compression method gzip or bzip2 respectively as shown below.

[root@localhost home]# tar xvfz archive_file.tar.gz /path/to/dir/
[root@localhost home]# tar xvfj archive_file.tar.bz2 /path/to/dir/

12. Extract group of files from tar, tar.gz, tar.bz2 archives using regular expression.
You can specify a regex, to extract files matching a specified pattern. For example, following tar command extracts all the files with pl extension.

[root@localhost home]# tar xvf archive_file.tar --wildcards '*.pl'
  • –wildcards *.pl – files with pl extension

13. Adding a file or directory to an existing archive using option -r.
You can add additional files to an existing tar archive as shown below. For example, to append a file to *.tar file do the following:

[root@localhost home]# tar rvf archive_name.tar newfile

This newfile will be added to the existing archive_name.tar. Adding a directory to the tar is also similar,

[root@localhost home]# tar rvf archive_name.tar newdir/

Note: You cannot add file or directory to a compressed archive. If you try to do so, you will get “tar: Cannot update compressed archives” error as shown below.

[root@localhost home]# tar rvfz archive_name.tgz newfile
tar: Cannot update compressed archives
Try `tar --help' or `tar --usage' for more information.

14. Verify files available in tar using option -W
As part of creating a tar file, you can verify the archive file that got created using the option W as shown below.

[root@localhost home]# tar cvfW file_name.tar dir/

If you are planning to remove a directory/file from an archive file or from the file system, you might want to verify the archive file before doing it as shown below.

[root@localhost home]# tar tvfW file_name.tar
Verify 1/file1
1/file1: Mod time differs
1/file1: Size differs
Verify 1/file2
Verify 1/file3

If an output line starts with Verify, and there is no differs line then the file/directory is Ok. If not, you should investigate the issue.
Note: for a compressed archive file ( *.tar.gz, *.tar.bz2 ) you cannot do the verification.
Finding the difference between an archive and file system can be done even for a compressed archive. It also shows the same output as above excluding the lines with Verify.

Finding the difference between gzip archive file and file system

[root@localhost home]# tar dfz file_name.tgz

Finding the difference between bzip2 archive file and file system

[root@localhost home]# tar dfj file_name.tar.bz2

15. Estimate the tar archive size.
The following command, estimates the tar file size ( in KB ) before you create the tar file.

[root@localhost home]# tar -cf - /directory/to/archive/ | wc -c
20480

The following command, estimates the compressed tar file size ( in KB ) before you create the tar.gz, tar.bz2 files.

[root@localhost home]# tar -czf - /directory/to/archive/ | wc -c
2480
[root@localhost home]# tar -cjf - /directory/to/archive/ | wc -c
480
Advertisements