Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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)
Distribution: PCLinuxOS2023 Fedora38 + 50+ other Linux OS, for test only.
Posts: 17,511
Rep:
#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.
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
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.
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.
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.
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
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.