Linux Classes
Linux Training
* Linux Classes *

LINUX CLASSES - COMPRESSION, ENCODING AND ENCRYPTION

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)

Adam Human     (31 Aug 2010, 03:45)
Hi,

I've lost the direction of my life. Could you help me set it up with an example, too?

Thanks in advance!
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

*Name:
Email:
Notify me about new comments on this page
Hide my email
*Text:
 

Ask Bob Rankin - Free Tech Support
<Send This Link to a Friend>         <Bookmark This Page>


Copyright © by Bob Rankin
All rights reserved - Redistribution is allowed only with permission.