Creating A tar File in Linux Via Command Line Options

by on December 15, 2007 · 4 comments· LAST UPDATED March 10, 2014

in , ,

How do I create a tar file under Linux using command line options?

You need to use the tar command to create an archive (also known as tar ball) under Linux operating systems. The tar command can create and manipulate archive files. It can even extract files from zip, ar, cpio, shar, ISO 9660 cdrom images, pax and other archives.

What is an archive?

Tutorial details
DifficultyEasy (rss)
Root privilegesNo
RequirementsNone
Estimated completion time5m

An archive is nothing but you bundle (put) many files together into a single file on a single tape or disk. You can restore individual files from the archive on any Unix-like systems.

Linux tar command syntax

The syntax is as follows to create a tar file:

tar -cvf output.tar /dirname

OR

tar -cvf output.tar /dirname1 /dirname2 filename1 filename2

OR

tar -cvf output.tar /home/vivek/data /home/vivek/pictures /home/vivek/file.txt

OR

tar -cvf /tmp/output.tar /home/vivek/data /home/vivek/pictures /home/vivek/file.txt

Where,

  • -c : Create a tar ball.
  • -v : Verbose output (show progress).
  • -f : Output tar ball archive file name.
  • -x : Extract all files from archive.tar.
  • -t : Display the contents (file list) of an archive.

Linux tar Command Examples

Create a tar ball called /tmp/data.tar for /home/vivek/data directory, enter:

tar -cvf /tmp/data.tar /home/vivek/data

To View a Tar Ball Contains (list file inside a tar ball)

Type the following command:

tar -tvf /tmp/data.ta

To Extract a Tar Ball

Type the following command to extract /tmp/data.tar in a current directory, enter:

tar -xvf /tmp/data.ta

Type the following command to extract resume.pdf file from an archive called data-backup.tar in a current directory, enter:

tar -xvf data-backup.tar resume.pdf

Type the following command to extract pic1.jpg, file1.doc, and foo.mov files from an archive called data-backup.tar in a current directory, enter:

tar -xvf data-backup.tar pic1.jpg file1.doc foo.movf

Type the following command to extract /tmp/data.tar in a directory called /home/sales/data, enter:

tar -xvf /tmp/data.tar -C /home/sales/data

How To Backup Files To a Tape Device /dev/st0?

You need to use the combination of mt and tar command to control magnetic tape drive operation.

## Backup /home/ directories to a tape drive called /dev/st0
tar -czf /dev/st0 /home/
 

See our previous tutorial "Linux Tape Backup With mt And tar Command Howto" for more information. I also suggest that you read Linux tar(1) command man page for all other syntax and options.

# Additional correction by Khaled Z; Editing by VG - log #

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 4 comments… read them below or add one }

1 Raj April 2, 2013 at 10:56 pm

To View a Tar Ball Contains (list file inside a tar ball)

Type the following command:

tar -ctvf /tmp/data.ta

This should be tar -tvf /tmp/data.tar

Reply

2 Nix Craft March 10, 2014 at 6:55 am

Thanks for the heads up!

Reply

3 kman November 1, 2013 at 9:46 am

Dear all,
I have problem when creating tar file. I want to zip file in /srv/backup/
tar -czf /mnt/myfile.tar.gz /srv/backup/
but i got error” tar: Removing leading `/’ from member names”

Thanks

Reply

4 gaurav November 6, 2013 at 8:38 am

use -P option with -czf , then you will not have this error.
#tar -Pczf /mnt/myfile.tar.gz /srv/backup/

-P option can be used to create a tar archive without stripping leading ‘/’s from member names . It will not change member names now and use absolute names.

Reply

Leave a Comment

Tagged as: , , , , , , ,

Previous Faq:

Next Faq: