LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-24-2011, 01:54 PM   #1
Ribo01
Member
 
Registered: Aug 2010
Posts: 103

Rep: Reputation: 5
using the tar command


Hello y'all. I kinda of need help using the tar command. Yeah, u'll say go read man page, but have done that and can't seem to get an understanding. All I need the usage, and basic explanation. Let's assume I have a directory with files that I need to tar, how do I go about it. Thanks in anticipation.
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 02-24-2011, 01:56 PM   #2
pljvaldez
LQ Guru
 
Registered: Dec 2005
Location: Somewhere on the String
Distribution: Debian Wheezy (x86)
Posts: 6,094

Rep: Reputation: 281Reputation: 281Reputation: 281
http://lmgtfy.com/?q=basic+tar+linux

EDIT: Sorry, I couldn't resist. Someone sent me one of those "Let Me Google that for you" links and I thought it was hilarious.

Last edited by pljvaldez; 02-24-2011 at 01:57 PM.
 
1 members found this post helpful.
Old 02-24-2011, 02:07 PM   #3
Cultist
Member
 
Registered: Feb 2010
Location: Georgia
Distribution: Slackware64 14.2
Posts: 779

Rep: Reputation: 107Reputation: 107
Code:
tar xvzf packagename.tar.gz
or

Code:
tar xvjf packagename.tar.bz2
depending on if the package is gzipped or bzipped.
 
1 members found this post helpful.
Old 02-24-2011, 02:52 PM   #4
z1p
Member
 
Registered: Jan 2011
Location: the right coast of the US
Distribution: Ubuntu 10.04
Posts: 80

Rep: Reputation: 23
Well, just for good measure here is the man page for tar http://linux.die.net/man/1/tar (found it through pljvaldez's google search


Here are some pertinent items from the man page.
First, some examples... the create archive being the one of interest for you. That will do what you want with the result being a
bzipped file.

What format the resulting file is depends on you options. So the next piece of the man page to look at is the Function letters. as you are creating a new archive file you need to use -c as in the example.

Next there are the common options, as the example shows you need to supply -f followed by the name of the archive file you want to create. The example names it foo.tar.bz2. You can name your archive anything you want, but it is best to use the standard extensions. (tar, tar.bz2, tar.gz)

If you don't provide a compression option, tar will just create a tar file. If you also want to compress the data, then you need to specify a compression option. The example uses -j for bzip. Instead you could use -Z for compress or -z for zipped.
These are found under the All Options section.


Code:
[snippet from above man page]

Examples

tar -xvf foo.tar
    verbosely extract foo.tar 
tar -xzf foo.tar.gz
    extract gzipped foo.tar.gz
 
tar -cjf foo.tar.bz2 bar/
    create bzipped tar archive of the directory bar called foo.tar.bz2 


tar -xjf foo.tar.bz2 -C bar/
    extract bzipped foo.tar.bz2 after changing directory to bar 
tar -xzf foo.tar.gz blah.txt
    extract the file blah.txt from foo.tar.gz


Function Letters

One of the following options must be used:
-A, --catenate, --concatenate
    append tar files to an archive 
-c, --create
    create a new archive 


Common Options
-C, --directory DIR
    change to directory DIR 
-f, --file [HOSTNAME:]F
    use archive file or device F (default "-", meaning stdin/stdout) 

Last edited by z1p; 02-24-2011 at 02:54 PM.
 
1 members found this post helpful.
Old 02-24-2011, 04:28 PM   #5
knudfl
LQ 5k Club
 
Registered: Jan 2008
Location: Copenhagen DK
Distribution: PCLinuxOS2023 Fedora38 + 50+ other Linux OS, for test only.
Posts: 17,511

Rep: Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641
#3 - #4
Quote:
depending on if the package is gzipped or bzipped.
Valid seven years ago. Still works, but not required :
From /usr/doc/tar-1.15.1/NEWS , slack 10.2
Quote:
version 1.15 - Sergey Poznyakoff, 2004-12-20

* Compressed archives are recognised automatically, it is no longer
necessary to specify -Z, -z, or -j options to read them. Thus, you can
now run `tar tf archive.tar.gz'.

To the OP : 'tar -cf <new-name>.tar <directory>/' : will "tar" your files.

gzip <new-name>.tar : will gzip the newly created tarball. Can be done in one run,
but this way you can check the uncompressed file size.

..
 
1 members found this post helpful.
Old 02-24-2011, 04:33 PM   #6
Cultist
Member
 
Registered: Feb 2010
Location: Georgia
Distribution: Slackware64 14.2
Posts: 779

Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by knudfl View Post
#3 - #4

Valid seven years ago. Still works, but not required :
From /usr/doc/tar-1.15.1/NEWS , slack 10.2



To the OP : 'tar -cf <new-name>.tar <directory>/' : will "tar" your files.

gzip <new-name>.tar : will gzip the newly created tarball. Can be done in one run,
but this way you can check the uncompressed file size.

..
I had no idea. that saves me a bit of time
 
1 members found this post helpful.
Old 02-25-2011, 06:32 AM   #7
Ribo01
Member
 
Registered: Aug 2010
Posts: 103

Original Poster
Rep: Reputation: 5
Correct me if am wrong. First you have to create an archive before anything can be done and you create a tar archive using the -c option. After that has been. Done and a tarball has been created, then to compress, you using any of the options -j or -z. To compress your archives in a directory. Right? So let me know if got anything wrong ? Thanks
 
Old 02-25-2011, 06:46 AM   #8
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by Ribo01 View Post
Correct me if am wrong. First you have to create an archive before anything can be done and you create a tar archive using the -c option. After that has been. Done and a tarball has been created, then to compress, you using any of the options -j or -z. To compress your archives in a directory. Right? So let me know if got anything wrong ? Thanks
That's correct. Take in mind what tar has been created for: tar means Tape Archive and it's one of the most ancient Unix commands. It was originally created to store data on magnetic tapes, especially for backups. So the basic idea is to store a bunch of files and the related directory structure in a stream where files are saved sequentially one after another. The tar archive contains information to rebuild the original directory structure, then the data itself. Once you have a single file you can gzip or bzip2 it. The -z and -j option of tar let you spare time, by making a compressed archive in one single step.
 
1 members found this post helpful.
Old 02-25-2011, 08:21 AM   #9
MCinAZ
LQ Newbie
 
Registered: Jun 2008
Location: Arizona
Distribution: Debian
Posts: 22

Rep: Reputation: 2
A comment regarding compression. The most common compression algorithms used for tar files are gzip and bzip2. While the latter does indeed result in a smaller file, the time it takes to run compared to gzip can be substantially longer, and the difference in resulting file sizes is usually small. As a consequence, unless I'm creating an large archive which will have to go across a transmission line multiple times, or will just barely fit on a transport medium (e.g. a CD-ROM), I generally use the 'z' option over 'j'. I'm not familiar with the performance of LZMA compression, so I can't comment about that option. The 'Z' option selects the ancient and obsolete "compress" algorithm. You don't want this one.

A second comment regarding compression. If your archive is primarily composed of already-compressed data, such as JPEG images or MP3 files, there's minimal to no benefit to selecting a compression algorithm. You may get a "compressed" tar file which is actually larger than one generated without a compression option, due to the overhead of the algorithm. If in doubt, create both in "/tmp" and use the smaller of the two.

To expand a little on what colucix said above, a big advantage to tar is that it will leave in place a directory structure essentially identical to the original. All file permissions, ownership and modification dates are retained. Of course, this is also true with zip/unzip and cpio, but among UNIX/Linux people, it's tar that you'll see used most often.
 
2 members found this post helpful.
Old 02-25-2011, 10:24 AM   #10
Ribo01
Member
 
Registered: Aug 2010
Posts: 103

Original Poster
Rep: Reputation: 5
hello y'all again. would now really like to understand how extraction works -x option. after an archive has been compressed, does the extraction UN-compresses it, or how do you uncompress a compressed archives? thanks again.
 
Old 02-25-2011, 10:27 AM   #11
jmc1987
Member
 
Registered: Sep 2009
Location: Oklahoma
Distribution: Debian, CentOS, windows 7/10
Posts: 893

Rep: Reputation: 119Reputation: 119
tar <option> <file>

tar is the command
-c is to create a archive
-v verbose for what is going on
-f the file name
-x to extract a file
-j filter through bzip to compress or decompress file
-z filter through gunzip to compress or decompress a file
-p preserve permissions

Examples
files.tar
untar it would be

tar xf files.tar

now you have file called files
to retar it or create a archive

tar cf files.tar tar

if you have files.tar.gz to untar would be

tar xzf files.tar.gz

to retar and compress with gunzip file called files would be

tar czf files.tar.gz files

you can also use the v option to see it do its thing as

tar xvf file
tar cvf file.tar file
tar cvzf file.tar.gz file
tar cvjf file.tar.bzip file

and so on

Hope this helps
 
1 members found this post helpful.
Old 02-26-2011, 11:54 AM   #12
Ribo01
Member
 
Registered: Aug 2010
Posts: 103

Original Poster
Rep: Reputation: 5
Hello all. For you all that take out time to replying my posts and request, can't thanks you enough. Really greatful qnd appreciate all the love. You guys are helping buildio a great network of computer professionals. Thanks a lot and so much love. Cheers.
 
Old 02-26-2011, 11:56 AM   #13
jmc1987
Member
 
Registered: Sep 2009
Location: Oklahoma
Distribution: Debian, CentOS, windows 7/10
Posts: 893

Rep: Reputation: 119Reputation: 119
Quote:
Originally Posted by Ribo01 View Post
Hello all. For you all that take out time to replying my posts and request, can't thanks you enough. Really greatful qnd appreciate all the love. You guys are helping buildio a great network of computer professionals. Thanks a lot and so much love. Cheers.

You can always give people +1 Rep
 
1 members found this post helpful.
Old 02-26-2011, 12:02 PM   #14
repo
LQ 5k Club
 
Registered: May 2001
Location: Belgium
Distribution: Arch
Posts: 8,529

Rep: Reputation: 899Reputation: 899Reputation: 899Reputation: 899Reputation: 899Reputation: 899Reputation: 899
Quote:
You can always give people +1 Rep
Don't you think a honest "thank you" is enough?
I don't like this begging for rep's.

Kind regards

Last edited by repo; 02-26-2011 at 12:03 PM.
 
1 members found this post helpful.
Old 02-26-2011, 12:03 PM   #15
corp769
LQ Guru
 
Registered: Apr 2005
Location: /dev/null
Posts: 5,818

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
Quote:
Originally Posted by repo View Post
Don't you think a honest "thank you" is enough?

Kind regards
I was thinking the same thing...
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
where is TAR ? bash: tar: command not found Juicyapple Linux - Newbie 7 06-10-2009 08:32 AM
tar-command not found while compiling glibc-libidn-2.7.tar.bz2 of lfs6.3 aditya_gpch Linux From Scratch 1 05-13-2008 11:27 PM
tar command alias command rlg Linux - Newbie 0 03-11-2008 07:21 AM
BackUp & Restore with TAR (.tar / .tar.gz / .tar.bz2 / tar.Z) asgarcymed Linux - General 5 12-31-2006 02:53 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:43 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration