tar Command in Linux with Examples
tar Command
tar Command stands for Tape Archive which is used to merge a collection of files and directories into highly compressed archive file. The tar command is commonly called as tarball or tar, gzip and bzip.
Basic Syntax
tar
To create archive file
To create a new tar archive file, use the following tar command.
Syntax
tar -cvf [tar file name.tar] [directory path]
Example
[user1@linuxhelp1 Desktop]$ tar -cvf files.tar files files/ files/Day-day-Activities-index File.rtf files/Logical Volume Management on Linux.docx files/Raid Concept(Redundant Array of Independent).docx [user1@linuxhelp Desktop]$ ll total 276 drwxrwxr-x. 2 user1 user1 4096 Feb 24 12:46 files -rw-rw-r--. 1 user1 user1 276480 Feb 24 12:55 files.tar
To create tar.gz archive file
To create a compressed gzip archive file use the ‘ z’ option.
Syntax
tar -cvzf [filename.tar.gz] [directory path]
Example
[user1@linuxhelp Desktop]$ tar -cvzf files.tar.gz files files/ files/Day-day-Activities-index File.rtf files/Logical Volume Management on Linux.docx files/Raid Concept(Redundant Array of Independent).docx [user1@linuxhelp Desktop]$ ll total 276 drwxrwxr-x. 2 user1 user1 4096 Feb 24 12:46 files -rw-rw-r--. 1 user1 user1 276480 Feb 24 12:55 files.tar
To create tar.bz2 archive file
The bz2 compress and create archive file less than the size of the gzip. The bz2 compression takes more time to compress and decompress files as compared to gzip which takes less time. To create highly compressed tar file we use option ‘ j’ .
Syntax
tar -cvjf [filename.tar.bz2] [directory path]
Example
[user1@linuxhelp Desktop]$ tar -cvjf files.tar.bz2 files files/ files/Day-day-Activities-index File.rtf files/Logical Volume Management on Linux.docx files/Raid Concept(Redundant Array of Independent).docx [user1@linuxhelp Desktop]$ ll total 276 drwxrwxr-x. 2 user1 user1 4096 Feb 24 12:46 files -rw-rw-r--. 1 user1 user1 276480 Feb 24 12:55 files.tar
To untar archive file in the same location
To untar or extract a tar file, just use following tar command with the option ‘ x’ (extract). It extracts the contents in the current working directory.
Syntax
tar -xvf [filename.tar]
Example
[user1@linuxhelp1 Desktop]$ tar -xvf files.tar files/ files/Day-day-Activities-index File.rtf files/Logical Volume Management on Linux.docx files/Raid Concept(Redundant Array of Independent).docx [user1@linuxhelp Desktop]$ ls -l total 628 drwxrwxr-x. 2 user1 user1 4096 Feb 24 12:46 files drwxrwxr-x. 2 user1 user1 4096 Feb 24 12:46 files 1 -rw-rw-r--. 1 user1 user1 276480 Feb 24 12:55 files.tar -rw-rw-r--. 1 user1 user1 178657 Feb 24 13:13 files.tar.bz2 -rw-rw-r--. 1 user1 user1 171431 Feb 24 13:12 files.tar.gz drwxrwxr-x. 2 user1 user1 4096 Feb 24 13:19 new
To untar archive files in specified Directory
To untar archive files in a specific directory, use the following tar command.
Syntax
tar -xvf [filename.tar] -C [directory path]
Example
[user1@linuxhelp Desktop]$ tar -xvf files.tar -C /home/user1/Desktop/new/ files/ files/Day-day-Activities-index File.rtf files/Logical Volume Management on Linux.docx files/Raid Concept(Redundant Array of Independent).docx [user1@linuxhelp Desktop]$ cd new [user1@linuxhelp new]$ ll total 4 drwxrwxr-x. 2 user1 user1 4096 Feb 24 12:46 files
To uncompress tar.gz archive file
To uncompress tar.gz archive file, just use following tar command. If would like to untar in different directory just use option -C and the path of the directory.
Syntax
tar -xvf [filename.tar.gz]
To extract to a specific path
tar -xvf [filename.tar.gz] -C [directory path]
Example
[user1@linuxhelp1 Desktop]$ tar -xvf files.tar -C /home/user1/Desktop/new/ files/ files/Day-day-Activities-index File.rtf files/Logical Volume Management on Linux.docx files/Raid Concept(Redundant Array of Independent).docx [user1@linuxhelp Desktop]$ cd new [user1@linuxhelp new]$ ll total 4 drwxrwxr-x. 2 user1 user1 4096 Feb 24 12:46 files
To uncompress tar.bz2 archive file
To uncompress highly compressed tar.bz2 file, just use the following tar command.
Syntax
tar -xvf [filename.tar.bz2]
To extract to a specific path
[user1@linuxhelp]$ tar -xvf [filename.tar.bz2] -C [directory path] files/ files/Day-day-Activities-index File.rtf files/Logical Volume Management on Linux.docx files/Raid Concept(Redundant Array of Independent).docx [user1@linuxhelp Desktop]$ tar -xvf files.tar.bz2 -C /home/user1/Desktop/new/ files/ files/Day-day-Activities-index File.rtf files/Logical Volume Management on Linux.docx files/Raid Concept(Redundant Array of Independent).docx [user1@linuxhelp Desktop]$ cd new [user1@linuxhelp new]$ ll total 4 drwxrwxr-x. 2 user1 user1 4096 Feb 24 12:46 files [user1@linuxhelp new]$ cd files [user1@linuxhelp files]$ ll total 264 -rw-rw-r--. 1 user1 user1 83069 Dec 31 10:17 Day-day-Activities-index File.rtf -rw-rw-r--. 1 user1 user1 88328 Dec 16 10:12 Logical Volume Management on Linux.docx -rw-rw-r--. 1 user1 user1 93610 Dec 16 11:00 Raid Concept(Redundant Array of Independent).docx
Listing the Contents of tar archive file
To list the content of tar archive file, use the following tar command with option‘ t’ .
Syntax
tar -tvf [filename.tar]
Example
[user1@linuxhelp Desktop]$ tar -tvf files.tar
drwxrwxr-x user1/user1 0 2016-02-24 12:46 files/
-rw-rw-r-- user1/user1 83069 2015-12-31 10:17 files/Day-day-Activities-index File.rtf
-rw-rw-r-- user1/user1 88328 2015-12-16 10:12 files/Logical Volume Management on Linux.docx
-rw-rw-r-- user1/user1 93610 2015-12-16 11:00 files/Raid Concept(Redundant Array of Independent).docx
To untar single file from tar file
To extract a single file from tar.gz archive file, use the following tar command.
Syntax
tar -xvf [filename.tar] [file path]
Example
[user1@linuxhelp Desktop]$ tar -xvf files.tar files/day.docx files/day.docx [user1@linuxhelp Desktop]$ cd files [user1@linuxhelp files]$ ll total 348 -rw-rw-r--. 1 user1 user1 83069 Dec 31 10:17 Day-day-Activities-index File.rtf -rw-rw-r--. 1 user1 user1 83069 Dec 31 10:17 day.docx -rw-rw-r--. 1 user1 user1 88328 Dec 16 10:12 Logical Volume Management on Linux.docx -rw-rw-r--. 1 user1 user1 93610 Dec 16 11:00 Raid Concept(Redundant Array of Independent).docx
To extract single directory from tar file
To extract a single directory from tar.gz file, use the following tar command
Syntax
tar -xvf [filename.tar.gz] [directory path]
Example
[user1@linuxhelp Desktop]$ tar -xvf files1.tar files/new files/new/ [user1@linuxhelp Desktop]$ cd files [user1@linuxhelp files]$ ll total 4 drwxrwxr-x. 2 user1 user1 4096 Feb 24 14:36 new
To untar multiple files from tar File
To extract multiple files from the tar archive file, use the following tar command
Syntax
tar -xvf [filename.tar] " file 1" " file 2"
Example
[user1@linuxhelp Desktop]$ tar -xvf files.tar " files/day.docx" " files/logical.docx" files/day.docx files/logical.docx [user1@linuxhelp Desktop]$ cd files [user1@linuxhelp files]$ ll total 176 -rw-rw-r--. 1 user1 user1 83069 Dec 31 10:17 day.docx -rw-rw-r--. 1 user1 user1 88328 Dec 16 10:12 logical.docx drwxrwxr-x. 2 user1 user1 4096 Feb 24 14:36 new
To Extract multiple files from tar.gz file
To untar multiple files from the tar.gz archive file, use the following tar command
Syntax
tar -zxvf [filename.tar.gz] " file 1" " file 2"
Example
[user1@linuxhelp Desktop]$ tar -xzvf files.tar.gz " files/index.docx" " files/raid.docx" files/index.docx files/raid.docx [user1@linuxhelp Desktop]$ cd files [user1@linuxhelp files]$ ll total 180 -rw-rw-r--. 1 user1 user1 83069 Dec 31 10:17 index.docx drwxrwxr-x. 2 user1 user1 4096 Feb 24 14:36 new -rw-rw-r--. 1 user1 user1 93610 Dec 16 11:00 raid.docx
To Extract multiple files from tar.bz2 file
To extract multiple files from the tar.bz2 archive file, use the following tar command
Syntax
tar -jxvf [filename.tar.bz2] " file 1" " file 2"
Example
[user1@linuxhelp Desktop]$ tar -jxvf files.tar.bz2 " files/volume.docx" " files/array.docx" files/volume.docx files/array.docx [user1@linuxhelp Desktop]$ cd files [user1@linuxhelp files]$ ll total 184 -rw-rw-r--. 1 user1 user1 93610 Dec 16 11:00 array.docx drwxrwxr-x. 2 user1 user1 4096 Feb 24 14:36 new -rw-rw-r--. 1 user1 user1 88328 Dec 16 10:12 volume.docx
To Extract Group of Files using Wildcard in tar archive file
To extract a group of all files using pattern in tar archive file, use the following tar command.
Syntax
tar -xvf [filename.tar] --wildcards [*.file format]
Example
[user1@linuxhelp Desktop]$ tar -xvf files.tar --wildcards " *.docx" files/day.docx files/logical.docx [user1@linuxhelp Desktop]$ cd files [user1@linuxhelp files]$ ll total 176 -rw-rw-r--. 1 user1 user1 83069 Dec 31 10:17 day.docx -rw-rw-r--. 1 user1 user1 88328 Dec 16 10:12 logical.docx drwxrwxr-x. 2 user1 user1 4096 Feb 24 14:36 new
To Extract Group of Files using Wildcard in tar.gz archive file
To extract a group of all files using pattern in tar.gz archive file, use the following command.
Syntax
tar -zxvf [filename.tar] --wildcards [*.file format]
Example
[user1@linuxhelp Desktop]$ tar -zxvf files.tar.gz --wildcards " *.docx" files/index.docx [user1@linuxhelp Desktop]$ cd files [user1@linuxhelp files]$ ll total 260 -rw-rw-r--. 1 user1 user1 83069 Dec 31 10:17 day.docx -rw-rw-r--. 1 user1 user1 83069 Dec 31 10:17 index.docx -rw-rw-r--. 1 user1 user1 88328 Dec 16 10:12 logical.docx drwxrwxr-x. 2 user1 user1 4096 Feb 24 14:36 new
To Extract Group of Files using Wildcard in tar.bz2 archive file
To extract a group of all files using pattern in tar.bz2 archive file, use the following command.
Syntax
tar -xvjf [filename.tar] --wildcards [*.file format]
Example
[user1@linuxhelp Desktop]$ tar -jxvf files.tar.bz2 --wildcards " *.docx" files/array.docx [user1@linuxhelp Desktop]$ cd files [user1@linuxhelp files]$ ll total 352 -rw-rw-r--. 1 user1 user1 93610 Dec 16 11:00 array.docx -rw-rw-r--. 1 user1 user1 83069 Dec 31 10:17 day.docx -rw-rw-r--. 1 user1 user1 83069 Dec 31 10:17 index.docx -rw-rw-r--. 1 user1 user1 88328 Dec 16 10:12 logical.docx drwxrwxr-x. 2 user1 user1 4096 Feb 24 14:36 new
Adding Files to tar Archive File
To add files to existing tar archive file we use the option ‘ r’ .
Syntax
tar -rvf [filename.tar] [filename]
Example
[user1@linuxhelp Desktop]$ tar -rvf files.tar new.txt new.txt [user1@linuxhelp Desktop]$ tar -tvf files.tar drwxrwxr-x user1/user1 0 2016-02-24 12:46 files/ -rw-rw-r-- user1/user1 83069 2015-12-31 10:17 files/day.docx -rw-rw-r-- user1/user1 88328 2015-12-16 10:12 files/logical.docx -rw-rw-r-- user1/user1 93610 2015-12-16 11:00 files/Raid Concept.odt -rw-rw-r-- user1/user1 0 2016-02-24 15:30 new.txt
Adding directories to tar Archive File
To add directories to existing tar archive file we use the option ‘ r’ .
Example
[user1@linuxhelp Desktop]$ tar -rvf files.tar new new/ [user1@linuxhelp Desktop]$ tar -tvf files.tar drwxrwxr-x user1/user1 0 2016-02-24 12:46 files/ -rw-rw-r-- user1/user1 83069 2015-12-31 10:17 files/day.docx -rw-rw-r-- user1/user1 88328 2015-12-16 10:12 files/logical.docx -rw-rw-r-- user1/user1 93610 2015-12-16 11:00 files/Raid Concept.odt -rw-rw-r-- user1/user1 0 2016-02-24 15:30 new.txt drwxrwxr-x user1/user1 0 2016-02-24 15:35 new/
To check the integrity of tar,tar.gz,tar.bz2 files
To check the integrity of the tar archive file, use the following command.
Syntax
tar -tvfW [filename.tar]
Example
[user1@linuxhelp Desktop]$ tar tvfW files.tar
tar: This does not look like a tar archive
tar: Skipping to next header
tar: VERIFY FAILURE: 160 invalid headers detected
Verify ---------- 509/514 88328 1970-01-01 05:30 files/Logical Volume Management on Linux.docx
files/Logical Volume Management on Linux.docx: Mode differs
files/Logical Volume Management on Linux.docx: Uid differs
files/Logical Volume Management on Linux.docx: Gid differs
files/Logical Volume Management on Linux.docx: Mod time differs
Verify ---------- 509/514 93610 1970-01-01 05:30 files/Raid Concept(Redundant Array of Independent).docx
files/Raid Concept(Redundant Array of Independent).docx: Mode differs
files/Raid Concept(Redundant Array of Independent).docx: Uid differs
files/Raid Concept(Redundant Array of Independent).docx: Gid differs
files/Raid Concept(Redundant Array of Independent).docx: Mod time differs
Integrity check is not possible in compressed tar.gz and tar.bz2 files
Delete a file from tar file
To delete a file from tar archive file, use the following command.
Syntax
tar --delete -f [filename.tar] [file path]
Example
[user1@linuxhelp Desktop]$ tar --delete -f files.tar new.txt
[user1@linuxhelp Desktop]$ tar -tvf files.tar
drwxrwxr-x user1/user1 0 2016-02-24 13:18 files/
-rw-rw-r-- user1/user1 83069 2015-12-31 10:17 files/Day-day-Activities-index File.rtf
-rw-rw-r-- user1/user1 88328 2015-12-16 10:12 files/Logical Volume Management on Linux.docx
-rw-rw-r-- user1/user1 93610 2015-12-16 11:00 files/Raid Concept(Redundant Array of Independent).docx
drwxrwxr-x user1/user1 0 2016-02-24 15:35 new/
To know the size of the tar,tar.gz,tar.bz2 archive files
To know the size of the tar archive file, use the following command.
Syntax
tar -cf - [filename.tar] | wc -1
Example
[user1@linuxhelp Desktop]$ tar -cf - files.tar | wc &ndash 1
1125 7304 286720 total
To know the size of the tar.gz archive file
Syntax
tar -czf -cf - [filename.tar.gz] | wc -1
Example
[user1@linuxhelp Desktop]$ tar -zcf - files.tar.gz | wc - 1 683 3709 171555 total
To know the size of the tar.gz archive file
Syntax
tar -cjf - [filename.tar.bz2] | wc -1
Example
[user1@linuxhelp Desktop]$ tar -jcf - files.tar.bz2 | wc &ndash 1
708 3875 179885 total
Comments ( 0 )
No comments available