This tutorial explains Linux “tar” command, options and its usage with examples.
Synopsis:
tar [OPTION…] [FILE]…
Examples:
1. Creating an uncompressed archive using tar command
$ tar cvf archive_name.tar dirname/
Create archive_name.tar from all files in directory dirname
OR
$ tar cvf archive_name.tar abc def
Create archive_name.tar from files abc, def in directory dirname
Options mean
c – create a new archive
v – verbosely list files which are processed.(optional)
f – following is the archive file name
Say if there is a directory in which we have abc and def files and we make a tar file out of them, so the following procedure will be followed
$ ls abc def $ tar cvf compressed.tar abc def abc def $ ls abc compressed.tar def
2. Creating a compressed archive using tar command
(i) gzipped tar – To use a gzip compression on the tar archive, use the z option as shown below
$ tar cvzf archive_name.tar.gz dirname/
Note: .tgz is same as .tar.gz
Note: For this method to work properly, don’t give – in front of the options.
(ii)bzipped tar-To use a bzip2 compression on the tar archive, use the z option as shown below
$ tar cvfj archive_name.tar.bz2 dirname/
Note: .tbz and .tb2 is same as .tar.bz2
Note: gzip vs bzip2: bzip2 takes more time to compress and decompress than gzip. bzip2 archival size is less than gzip.
3. Extracting a tar archive
Extract a tar file using option x as shown below:
$ tar xvf archive_name.tar
It extracts the files in the present directory where archive is located.
4. Extracting a compressed tar archive
(i) gzipped tar-Use the option z for uncompressing a gzip tar archive.
$ tar xvfz archive_name.tar.gz
(ii)bzipped tar-Use the option j for uncompressing a gzip tar archive.
$ tar xvfj archive_name.tar.bz2
5. Listing an uncompressed archive using tar command
You can view the *.tar file content before extracting as shown below.
$ tar tvf archive_name.tar
6. Listing a compressed archive using tar command
(i) gzipped tar-Use the option z for uncompressing a gzip tar archive.
$ tar tvfz archive_name.tar.gz
(ii)bzipped tar-Use the option j for uncompressing a gzip tar archive.
$ tar tvfj archive_name.tar.bz2
Note: When the number of files in an archive is more, you may pipe the output of tar to less.
7. 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
$ tar xvf archive_file.tar /path/file
Use the relevant option z or j according to the compression method gzip or bzip2 respectively
$ tar xvfz archive_file.tar.gz /path/file OR $ tar xvfj archive_file.tar.bz2 /path/file
Note: For multiple files with same extension use wildcard option
$ tar xvf archive_file.tar --wildcards '*.c'
8. Extract directory from tar, tar.gz, tar.bz2 file
Specify the directory name at the end of the tar xvf command as shown below.
$ tar xvf archive_file.tar /path/dir/
To extract multiple directories from a tar archive,specify all the individual directories at the end.
$ tar xvf archive_file.tar /path/dir1/ /path/dir2/
Use the relevant option z or j according to the compression method gzip or bzip2 respectively .
9. Adding a file or directory to an existing archive
This can be done with the help of -r option.
$ tar rvf archive.tar abc
This appends the file abc to archive.tar
Adding a directory to the tar is also similar,
$ tar rvf archive.tar dir/
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
10. Estimate the tar archive size
The following command, estimates the tar file size ( in KB ) before you create the tar file.
$ tar -cf - /directory/to/archive/ | wc -c
The result of tar command is piplined to wc command(wordcount with -c option to count bytes).
Use the relevant option z or j according to the compression method gzip or bzip2 respectively .
Sanfoundry Global Education & Learning Series – 1000 Linux Tutorials.
- Practice Programming MCQs
- Apply for Programming Internship
- Buy Information Technology Books
- Buy Linux Books
- Apply for Linux Internship