• Categories
    Category
  • Categories
    Category
  • News
  • Tutorials
  • Forums
  • Tags
  • Users
Tutorial Comments FAQ Related Articles

tar Command in Linux with Examples

360

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

Tags:
caden
Author: 

Comments ( 0 )

No comments available

Add a comment

Frequently asked questions ( 5 )

Q

What does the command"tar cvf - . "do ? Complete command is something like below. Command : tar cvf - . | ( ssh pppdc9prd7s0 "cd/oracle/product/11.2.0.3/db ; tar xvf -" )

A

It creates(c) new archive from current directory(.) and writes it to the standard output (f -).

Q

I had a look at the options, but nothing seemed obvious as a manner in which to turn off the output when uncompressing a file. The below is the code I am currently using... I just need the option to switch off the output. tar -zxvf tmp.tar.gz -C ~/tmp1

A

Just drop the option v. -v is for verbose. If you don't use it then it won't display: tar -zxf tmp.tar.gz -C ~/tmp1

Q

Can someone help me understand this command: tar -czf - -T ./tarFileList.tmp -C ./test_folder/ | ssh -l musthero voserver95.myserver.com -x "umask 002; cd /disk0/test_untar/ ; tar -xzf - "

A

The very first dash is unnecessary, you could equally write: tar cvf ... The second dash belongs with the f option and it says "instead of creating a named file in the filesystem, write the

Q

what does -zxvf mean in tar -zxvf ? [closed]

A

The following options are mentioned below



z means (un)z̲ip.

x means ex̲tract files from the archive.

v means print the filenames v̲erbosely.

f means the following argument is a f̱ilename.

Q

I have seen on many websites commands like this what does the "-zxvf" or the similar commands stand for?

tar -zxvf
tar xvf

A

z means (un)z̲ip.

x means ex̲tract files from the archive.

v means print the filenames v̲erbosely.

f means the following argument is a f̱ilename.

Related Tutorials in tar Command in Linux with Examples

Related Tutorials in tar Command in Linux with Examples

tar Command in Linux with Examples
tar Command in Linux with Examples
Mar 22, 2016

Related Forums in tar Command in Linux with Examples

Related Forums in tar Command in Linux with Examples

tar/Tape Archive
sebastian class=
tar: This does not look like a tar archive tar: Exiting with failure status due to previous errors
May 12, 2017
tar/Tape Archive
brayden class=
How to extract the .bz2 file using tar command
Feb 11, 2017
Back To Top!
Rank
User
Points

Top Contributers

userNamenaveelansari
135850

Top Contributers

userNameayanbhatti
92510

Top Contributers

userNamehamzaahmed
32150

Top Contributers

1
userNamelinuxhelp
31040

Top Contributers

userNamemuhammadali
24500
Can you help Lucas ?
Various options in Top command

Am using Top command only to view the load average, what are the various options in Top command..??

Networking
  • Routing
  • trunk
  • Netmask
  • Packet Capture
  • domain
  • HTTP Proxy
Server Setup
  • NFS
  • KVM
  • Memory
  • Sendmail
  • WebDAV
  • LXC
Shell Commands
  • Cloud commander
  • Command line archive tools
  • last command
  • Shell
  • terminal
  • Throttle
Desktop Application
  • Linux app
  • Pithos
  • Retrospect
  • Scribe
  • TortoiseHg
  • 4Images
Monitoring Tool
  • Monit
  • Apache Server Monitoring
  • EtherApe 
  • Arpwatch Tool
  • Auditd
  • Barman
Web Application
  • Nutch
  • Amazon VPC
  • FarmWarDeployer
  • Rukovoditel
  • Mirror site
  • Chef
Contact Us | Terms of Use| Privacy Policy| Disclaimer
© 2025 LinuxHelp.com All rights reserved. Linux™ is the registered trademark of Linus Torvalds. This site is not affiliated with linus torvalds in any way.