Linux Classes
Share This With a Friend  
LINUX CLASSES - COMPRESSION, ENCODING AND ENCRYPTION

Linux Tar Command

How Can I Do Archiving With Tar?

The tar Command

The tar (tape archive) command bundles a bunch of files together and creates an archive (commonly called a tar file or tarball) on a tape, disk drive, or floppy disk. The original files are not deleted after being copied to the tar file. To create an archive using tar, use a command like this, which bundles all the files in the current directory that end with .doc into the alldocs.tar file:

tar cvf alldocs.tar *.doc

Here's a second example, which creates a tar file named panda.tar containing all the files from the panda directory (and any of its subdirectories):

tar cvf panda.tar panda/

In these examples, the c, v, and f flags mean create a new archive, be verbose (list files being archived), and write the archive to a file. You can also create tar files on tape drives or floppy disks, like this:

tar cvfM /dev/fd0 panda Archive the files in the panda directory to floppy disk(s).
tar cvf /dev/rmt0 panda Archive the files in the panda directory to the tape drive.

The /dev/fd0 entry is Linux-ese for "floppy drive zero" (your A drive under DOS), and /dev/rmt0 means "removable media tape zero," or your primary tape drive. The M flag means use multiple floppy disks--when one disk is full, tar prompts you to insert another.

To automatically compress the tar file as it is being created, add the z flag, like this:

tar cvzf alldocs.tar.gz *.doc

In this example, I added the .gz suffix to the archive file name, because the z flag tells tar to use the same compression as the gzip command.

To list the contents of a tar file, use the t (type) flag in a command, like this:

tar tvf alldocs.tar List all files in alldocs.tar.

To extract the contents of a tar file, use the x (extract) flag in a command, like this:

tar xvf panda.tar Extract files from panda.tar.

This will copy all the files from the panda.tar file into the current directory. When a tar file is created, it can bundle up all the files in a directory, as well as any subdirectories and the files in them. So when you're extracting a tar file, keep in mind that you might end up with some new subdirectories in the current directory.

We've used several different flags in the sample tar commands so far. Here's a list of the most common flags:

c Create a new archive.
t List the contents of an archive.
x Extract the contents of an archive.
f The archive file name is given on the command line (required whenever the tar output is going to a file)
M The archive can span multiple floppies.
v Print verbose output (list file names as they are processed).
u Add files to the archive if they are newer than the copy in the tar file.
z Compress or decompress files automatically.

For more information on the tar command, see the tar manual.

Previous Lesson: Linux File Compression
Next Lesson: Compression With Gzip

[ RETURN TO INDEX ]


   

Comments - most recent first
(Please feel free to answer questions posted by others!)

Clenbuterol     (30 Jul 2013, 08:04)
I want good looking arms, like arms that have veins going up and down the arm. I have a few weights at home but not many nothing fancy either. Main point of this is that I dont just want to be strong (Im already that) I want a look strong. Key of this is something that shows veins. And please tell me straight from here not just a link for another website I have to search. Thanks guys..
escorts Lebanon     (25 Jul 2013, 12:34)
Has anyone ever sent their child on a fight by themselves?
IGF 1 R3     (24 Jul 2013, 11:35)
I am getting a testosterone level blood test to see if I will need to get it done. I wanted to know how many days or weeks that this procedure usually takes..
Winstrol 2 mg     (05 Jul 2013, 08:23)
I am very skinny. I want to tone up, gain muscle(legs, chest, and arms), and get a six-pack. What is the quickest way to do this without purchasing exercising equipment? Please be descriptive. ;) ;)
Appliance repair Orange County     (23 May 2013, 17:18)
A person essentially lend a hand to make seriously articles I might state. This is the very first time I frequented your website page and up to now? I surprised with the research you made to make this actual submit extraordinary. Excellent activity!
Amit Klaynia     (19 Apr 2012, 07:27)
Want to some examples for useful linux commands
paulbeartil     (13 Apr 2012, 23:20)
hi heres the site just say paul put you onto them
beartils
mayur saswade     (24 Mar 2012, 07:45)
hi sir i got your site which is the more helpful to complition of my project will you have commands and the usful notes to learn the basic shell svript programming,

becayuse i am woking on the project of log module which is totaly based on the shell programming..

thank you and regards

mayur saswade
jzqiaxrxiq     (21 Mar 2012, 08:17)
The other day and baeing In german Creek Lin arrived
In spite of the cloudy day time if the bad weather when, they were adament around the journey after this, the last 90 kms aside, the trail needed 8 . 5 several hours. They had been combined with his / her associate Wu Lin along with the various other a The german language. Just before that, they begin via Yunnan Shangri-La, entirely over the Yangtze River using, riding 4,500 kms throughout Sixty six nights.
German born participant present his or her travel records for that Water Lin, the present 4,Thousand kilometers experience four to five instances a year in just his or her journey soon enough.
electrothumb     (04 Mar 2012, 01:48)
Low fat linux is fabulous for biginner.
thanx for such a good technique to teach us.
I am big fan dr.
Soundar     (29 Feb 2012, 09:52)
tks, simple and clear
Vikas Naik     (07 Feb 2012, 23:49)
Thank u sir.....
Thank u very much
legacy     (21 Jan 2012, 15:26)
Very help!It is a linux tutorial indeed.
Steve Biro     (09 Jan 2012, 15:58)
The only site I found that mentions the "M" option for spanning multiple floppies.
Paul     (05 Jan 2012, 04:42)
mksysb usage in creating bootable image on the same LPAR or on another partation?
Josiah Luscher     (21 Dec 2011, 20:45)
An article on how to archive with tar, and you didn't mention the -p option to preserve file permissions? - Your no doctor...
Sonny S. Sy     (17 Dec 2011, 03:04)
Command tar to CDROM Drive exmaple Command on Linux Red Hat ent. Version 5.4 ?.
passerby     (15 Dec 2011, 21:21)
Hi Dr. Bob.
I find your website really useful, but I'd like to know why when I click 'return to index' it goes to the comment sections?
Dilip     (02 Dec 2011, 09:44)
Thank You.
Anil     (20 Nov 2011, 11:31)
Ya its very nice and help full .........
Daniel C     (02 Nov 2011, 18:55)
Thanks a lot for this post/article Bob!!!
Very objective and didactic!!!
sanat     (28 Sep 2011, 07:23)
do all redhat commands work in ubuntu??
Sudhanshu     (28 Sep 2011, 02:37)
Great work...very helpful!!!
debabrata mandal     (07 Aug 2011, 13:24)
tar uses to compress the file even its takes also backup of bunch of file and gzip helps to zip the file only no backup.
santosh     (06 Jun 2011, 10:27)
helped me lot
fran     (27 May 2011, 09:56)
thank you from Spain.
mallikarjunreddy     (10 May 2011, 04:55)
Hi can anyone please tell me the difference between tar and gzip command in linux.iam little bit of confused.

Thanks and regards
Ravi     (22 Apr 2011, 06:35)
How to tar files with name more than 200 characters
Dinesh Panwar     (03 Mar 2011, 23:00)
How to check logs mail logs in RHEL 5 or Fedora

Can anyone who can help me
Nadim     (02 Feb 2011, 02:26)
This page helped a lot.
Thankx Bob ...
sanket     (28 Jan 2011, 16:59)
Hi, what is the use of tar and why do we use it?
Rudi     (21 Jan 2011, 10:48)
Dr Bob,

You are simply the best. Being a novice Linux user, these pages are a great help!

Rudi
poli     (16 Dec 2010, 04:17)
hi i have encounter the same problem and i found a solution by the following steps...
1). Unzip the *.tar.gz file by the command --- gunzip file.tar.gz
which will create a file.tar file
2).Now extract all files by the following command
tar -zxf file.tar
Robi     (13 Dec 2010, 03:10)
Thank you
Rob     (27 Nov 2010, 15:33)
Hi Scott -
According to the man page ("man tar"), the -z option compresses or decompresses the file with gzip (mentioned above), whereas the -j option uses bzip instead. I think bzip files are traditionally given the ending "bz2", whereas gzip files are given the ending "gz", so that it's easy to tell what compression program was used. I also vaguely recall that bzip usually creates a slightly smaller file (~10%?), but takes longer - sometimes noticeably longer. As for the "joomla" directory (I don't know what that is), could you just use "tar -cvzf joomla.tgz /path/to/joomla_directory/" to create a tarballed file, transfer it to the directory where you want the new directory to be, then untarball it with "tar -xvzf joomla.tgz"? Hope that helps.
Yaser     (09 Nov 2010, 04:23)
Good description
I want to know why the '-' is using with the tar cammand
Eg:- tar -cvf/dev/st0 tata.sql
Pls help me
Thanking u
Scott     (01 Oct 2010, 15:04)
Can you help me determine the difference between "tar -zxvf" and "tar -jxvf"? And what would be the best way to tar a joomla directory to install it elsewhere? Thanks.
sai     (29 Aug 2010, 06:46)
quite helpful
Gowrishankar     (19 Aug 2010, 22:58)
Good Stuff to refer..
nik     (13 Aug 2010, 20:58)
thank you so much....
p.n.subbarao     (11 Aug 2010, 07:20)
subbarao

hai sir good eveing please suggest me that how i take backup of data and tar and cpio and dd and dump back up what is the difference between commands.
IBRAHIM     (10 Aug 2010, 22:24)
how to configure the DNS,DHCP server in RHEL 5.0 please give the figure example also.
Bob Rankin     (05 Aug 2010, 09:26)
@lavanya - I'm not sure where to start... of course there's a difference between tar and other commands. Otherwise tar would be the only command! You'll find detailed descriptions of MANY other unix commands here.
lavanya     (05 Aug 2010, 09:04)
gud evening sir,
i am b.tech student and was just learning the basics of unix actually i learnt that there is a difference between tar and other commands what does it meant by?
Zakee     (13 Jul 2010, 05:10)
Sir How to install Sql Server 2000 on Linux Ubunto 10.04 version
goverdhan nath     (22 Jun 2010, 05:24)
please sir you send me all method how to compress file and folder in linux command base
Bob Rankin     (10 Jun 2010, 11:12)
Looks like it's harmless. See
http://forums.gentoo.org/viewtopic-p-6301733.html
Aditya     (10 Jun 2010, 10:15)
HI,

I am great fan of low fat Linux. I often refer to it and so far it has never really disappointed me :)

I have one doubt though. I get

tar: ./POS: Cannot utime: Operation not permitted

error quite frequently while doing tar xvf filename.

Can it be safely ignored ? What does it mean any ways.

Regards,

Aditya
Mentwab     (08 Jun 2010, 08:15)
good evening sir, actully i'm workin on a project whic is cd/dvd writer. iwant to include BACKUP feature in this tool. i need ur help.....
please suggest me that how i take backup of data??
please help me
Matt     (14 May 2010, 14:54)
Thanks a lot Bob, was looking for a quick guide to various tar commands and came across this in a search, very helpful!
Sourav Das     (25 Apr 2010, 09:36)
1. full from DCPROMO
2. tar file extact command (linux)
abhi     (14 Apr 2010, 03:30)
how can i proper create yum server.pls. solve my problem.
Bob Rankin     (02 Apr 2010, 15:39)
You lost me... sorry. Could you be a little more specific?
manoj gupta     (02 Apr 2010, 06:48)
good evening sir, actully i'm workin on a project whic is cd/dvd writer. iwant to include BACKUP feature in this tool. i need ur help.....
please suggest me that how i take backup of data??
please help me

I welcome your comments. However... I am puzzled by many people who say "Please send me the Linux tutorial." This website *is* your Linux Tutorial! Read everything here, learn all you can, ask questions if you like. But don't ask me to send what you already have. :-)

NO SPAM! If you post garbage, it will be deleted, and you will be banned.
*Name:
Email:
Notify me about new comments on this page
Hide my email
*Text:
 
 


Ask Bob Rankin - Free Tech Support


Copyright © by - Privacy Policy
All rights reserved - Redistribution is allowed only with permission.