On Unix platform, tar command is the primary archiving utility. Understanding various tar command options will help you master the archive file manipulation.
In this article, let us review various tar examples including how to create tar archives (with gzip and bzip compression), extract a single file or directory, view tar archive contents, validate the integrity of tar archives, finding out the difference between tar archive and file system, estimate the size of the tar archives before creating it etc.,
1. Creating an archive using tar command
Creating an uncompressed tar archive using option cvf
This is the basic command to create a tar archive.
$ tar cvf archive_name.tar dirname/
In the above command:
- c – create a new archive
- v – verbosely list files which are processed.
- f – following is the archive file name
Creating a tar gzipped archive using option cvzf
The above tar cvf option, does not provide any compression. To use a gzip compression on the tar archive, use the z option as shown below.
$ tar cvzf archive_name.tar.gz dirname/
- z – filter the archive through gzip
Note: .tgz is same as .tar.gz
Note: I like to keep the ‘cvf’ (or tvf, or xvf) option unchanged for all archive creation (or view, or extract) and add additional option at the end, which is easier to remember. i.e cvf for archive creation, cvfz for compressed gzip archive creation, cvfj for compressed bzip2 archive creation etc., For this method to work properly, don’t give – in front of the options.
Creating a bzipped tar archive using option cvjf
Create a bzip2 tar archive as shown below:
$ tar cvfj archive_name.tar.bz2 dirname/
- j – filter the archive through bzip2
gzip vs bzip2: bzip2 takes more time to compress and decompress than gzip. bzip2 archival size is less than gzip.
Note: .tbz and .tb2 is same as .tar.bz2
2. Extracting (untar) an archive using tar command
Extract a *.tar file using option xvf
Extract a tar file using option x as shown below:
$ tar xvf archive_name.tar
- x – extract files from archive
Extract a gzipped tar archive ( *.tar.gz ) using option xvzf
Use the option z for uncompressing a gzip tar archive.
$ tar xvfz archive_name.tar.gz
Extracting a bzipped tar archive ( *.tar.bz2 ) using option xvjf
Use the option j for uncompressing a bzip2 tar archive.
$ tar xvfj archive_name.tar.bz2
Note: In all the above commands v is optional, which lists the file being processed.
3. Listing an archive using tar command
View the tar archive file content without extracting using option tvf
You can view the *.tar file content before extracting as shown below.
$ tar tvf archive_name.tar
View the *.tar.gz file content without extracting using option tvzf
You can view the *.tar.gz file content before extracting as shown below.
$ tar tvfz archive_name.tar.gz
View the *.tar.bz2 file content without extracting using option tvjf
You can view the *.tar.bz2 file content before extracting as shown below.
$ tar tvfj archive_name.tar.bz2
4. Listing out the tar file content with less command
When the number of files in an archive is more, you may pipe the output of tar to less. But, you can also use less command directly to view the tar archive output, as explained in one of our previous article Open & View 10 Different File Types with Linux Less Command — The Ultimate Power of Less.
5. Extract a single file from tar, tar.gz, tar.bz2 file
To extract a specific file from a tar archive, specify the file name at the end of the tar xvf command as shown below. The following command extracts only a specific file from a large tar file.
$ tar xvf archive_file.tar /path/to/file
Use the relevant option z or j according to the compression method gzip or bzip2 respectively as shown below.
$ tar xvfz archive_file.tar.gz /path/to/file $ tar xvfj archive_file.tar.bz2 /path/to/file
6. Extract a single directory from tar, tar.gz, tar.bz2 file
To extract a single directory (along with it’s subdirectory and files) from a tar archive, specify the directory name at the end of the tar xvf command as shown below. The following extracts only a specific directory from a large tar file.
$ tar xvf archive_file.tar /path/to/dir/
To extract multiple directories from a tar archive, specify those individual directory names at the end of the tar xvf command as shown below.
$ tar xvf archive_file.tar /path/to/dir1/ /path/to/dir2/
Use the relevant option z or j according to the compression method gzip or bzip2 respectively as shown below.
$ tar xvfz archive_file.tar.gz /path/to/dir/ $ tar xvfj archive_file.tar.bz2 /path/to/dir/
7. Extract group of files from tar, tar.gz, tar.bz2 archives using regular expression
You can specify a regex, to extract files matching a specified pattern. For example, following tar command extracts all the files with pl extension.
$ tar xvf archive_file.tar --wildcards '*.pl'
Options explanation:
- –wildcards *.pl – files with pl extension
8. Adding a file or directory to an existing archive using option -r
You can add additional files to an existing tar archive as shown below. For example, to append a file to *.tar file do the following:
$ tar rvf archive_name.tar newfile
This newfile will be added to the existing archive_name.tar. Adding a directory to the tar is also similar,
$ tar rvf archive_name.tar newdir/
Note: You cannot add file or directory to a compressed archive. If you try to do so, you will get “tar: Cannot update compressed archives” error as shown below.
$ tar rvfz archive_name.tgz newfile tar: Cannot update compressed archives Try `tar --help' or `tar --usage' for more information.
9. Verify files available in tar using option -W
As part of creating a tar file, you can verify the archive file that got created using the option W as shown below.
$ tar cvfW file_name.tar dir/
If you are planning to remove a directory/file from an archive file or from the file system, you might want to verify the archive file before doing it as shown below.
$ tar tvfW file_name.tar Verify 1/file1 1/file1: Mod time differs 1/file1: Size differs Verify 1/file2 Verify 1/file3
If an output line starts with Verify, and there is no differs line then the file/directory is Ok. If not, you should investigate the issue.
Note: for a compressed archive file ( *.tar.gz, *.tar.bz2 ) you cannot do the verification.
Finding the difference between an archive and file system can be done even for a compressed archive. It also shows the same output as above excluding the lines with Verify.
Finding the difference between gzip archive file and file system
$ tar dfz file_name.tgz
Finding the difference between bzip2 archive file and file system
$ tar dfj file_name.tar.bz2
10. Estimate the tar archive size
The following command, estimates the tar file size ( in KB ) before you create the tar file.
$ tar -cf - /directory/to/archive/ | wc -c 20480
The following command, estimates the compressed tar file size ( in KB ) before you create the tar.gz, tar.bz2 files.
$ tar -czf - /directory/to/archive/ | wc -c 508 $ tar -cjf - /directory/to/archive/ | wc -c 428
Comments on this entry are closed.
You missed few really useful examples, like ie.
tar cjf archive-$(date +%Y%m%d).tbz2 dir/
tar cjf archive-$(date +%Y%m%d).tbz2 dir/ -N ./archive-$(date -d yesterday +%Y%m%d)
for daily backups.
I want to add up an important command too which most of us do face regularly:
Writing an archive in a different directory
You may also want to write an archive lto a different directory, like this:
tar czvf /tmp/mydirectory.tgz .
tej@tej-desktop:~$ tar -czf – Music/ | wc -c
1358
tej@tej-desktop:~$ tar -cjf – Music/ | wc -c
1471
Why it is so. file tarred with j should be less in size????
Tar command always seemed confusing to me. Your writeup clears any mis understanding. Nice work.
dear Ramesh,
first of all i’d like to thank you very much for your excellent work. being a newbie to BSD/linux thegeekstuff is my #1 choice to have a look at when it comes to help, understanding, and how-to… =:-) thank you very much again.
but right now, i am a bit confused: in 1. Creating an archvie using tar command, Note #2, you wrote that you like to keep the ‘cvf’ option unchanged for easy remembering, and you prefer to add additional options at the end. but if so, shouldn’t it be ‘cvfz’ for compressed gzip’s, and ‘cvfj’ for compressed bzip2’s? i mean to have ‘cvf’ together as you wrote one sentence before? did i miss something? or is it a simple spelling mistake?
again, sorry if it’s a stupid question, but keep in mind i’m still very new to tar (and all the other non-MS stuff), let’s call it doubtful, uncertain and precarious.
No tar tutorial is complete without an example that does a cross-network copy.
This example uses GNU tar, with –numeric-owner and so can, as in this example, be used to copy an entire system onto a system with a blank disk drive that’s been put into another computer. The target system needs to have the new blank disk partitioned (fdisk, cfdisk, gparted, etc.), and filesystems created (mke2fs, etc.) and mounted (mount) in the appropriate places under /mnt/. It may be handy to boot the target system using a “live” Linux cd, usb key, etc.
If you are logged in, as root, on the target system, the one with the new drive, you use:
ssh sourcesystem.example.com ‘tar -C / –numeric-owner -czf – .’ | tar -C /mnt -xzpf –
If you are logged in, as root, on the source system, the one being copied, you use:
tar -C / –numeric-owner -czf – . | ssh targetsystem.example.com ‘tar -C /mnt -xzpf -‘
Note that the above command, or any other sort of whole system backup and restore, should _not_ be used to “clone” a system because of the many little bits of supposed-to-be-unique data that a modern system contains. Use your package management system to “clone”.
Tar is common. But tar doesn’t generate fully POSIX-compliant archives. A better tool is “pax”. It’s also more reliable in reconstructing file access rights. (And it handles both, tar and cpio formats.)
@tej parkash: The compressed size depends on the content of your Folder.
with latest version of tar, the options z or j does not need to be specified for listing or extracting.
Thanks for the useful article. The one suggestion I’d make is that the code to estimate final archive file size isn’t very useful in real life. The example for estimating the final size of the archive actually creates the archive, (in memory at least) so the estimate will take almost as long as actually doing the command in the first place. For small file sets you really don’t care about the size of the final output. For larger file sets where you are likely to care, the time to get the estimate is prohibitive. However that example is a good demonstration of the power of combining command line utilities by piping data between them. Great job!
Nice explanation.
tar does not use regular expressions (or regexp). it uses globbing – please do not mix these terms
Thanks – I’m always forgetting how to run tar to create a .tar.gz
Really cool Tutorial.
Hello,
I was wondering if anybody here could help with a little tar issue. I work for a post production company and we regularly back up full reels onto LTO4s. We are talking about 25-30k sequential .dpx frames. Recently we have been asked to start writing them in 500 frame blocks. This at the moment is an extremely time consuming effort…
We move 500 frames into a temp folder and create an order file that we tar. Then we move those 500 back into the main folder and grab the next 500 and so on…
So my question is… if anybody knows how to make a tar script that would write up to 34 thousand frames sequentially in 500 frame blocks.
Thank you
Excellent notes on Tar with appropriate examples and apt explanations. Easy to refere and remember.Very Very Handy.
Thank you very much for a great tutorial.
describe in detail what the following command does..tar czvf tarfile.tgz /etc/sysconfig.
Can some one tell me what this means..im new to linux..stil trying to learn it…really confused..what sites do you reccomend…
@Michael,
tar czvf tarfile.tgz /etc/sysconfig
c = create
z = compress
v = verify
f = filename to create
/etc/sysconfig everything in that directory will go into the tar file.
There are a ton of resources. I tend to just google what I need when the time comes. Also, typing ‘man tar’ on the commandline would give you lots more info.
very good super … wondeful thanks a lot
Hello thank you for examples –
Can you specify proper example/syntax to create a tar of a folder&subfolders on one server and send to a different server….
Source folder/s to TAR /home/222/TRLIST2
destination server= myserver.xyz.com
destination folder home/new/
Also do i need to uzip on other server…..
Can this all be done in one command line or does it require a
batch or script file
thank you for any help on this
Golf
Hello, thank-you very much, and the peeps who posted useful comments
This is a great guide and really helped me.
I would only add one command which is extremely helpfull and that is the -C option to push all files or just one file extracted to a certain directory of your choice
tar xvgj test.tar.bz2 -C /opt/test Being an example
Very Nice Ramesh Ji very good article I understand this complete to perform any compression task in any organization……………….. cool article
nice article, Is it possible to enter a command relating to a tar.gz file will tell you relative information about the file such as when it was created, its compression ratio and how many files it contains? cheers
I have an old round reel, need to extract data. When I type in command tar xvf /dev/rmt/0m one tape gives me data, another tape gives me error msg: tar: blocksize=4
How do I extract the second tape?
Thanks mate, just what l needed!
Hi , any one can you Please give me the commanlines to create tar file in Windows …?
how to create a tar in windows as well as tar in to GZip ?
Regards,
Raghav
As per example no [10] Estimate the tar archive size :
estimates the tar file size ( in KB ) before you create the tar file.
So, I ran this command to estimates the compressed tar file size :
[root@server1 ~]# tar -czf – My-selected/ | wc -c
397373440
I had doubt on size. Whether it is in Bytes or in KB? So I ran below command to know the exact size :
[root@server1 ~]# tar -zcf selected.tar.gz My-selected/
[root@server1 ~]# du selected.tar.gz
388440 selected.tar.gz
[root@server1 ~]# du -h selected.tar.gz
380M selected.tar.gz
Now what you say on size?
Who can help with TAR????????????
I got a TAR archive from Unix user and i need to get 1 small file out of it for modification.
Ok i managed to take the file of tar and by ZIP-7 get the directory out of it
I managed to modify the file and now i want to pack back this folder into a TAR file (NOT TAR ARCHIVE i mean the file inside of it withour extantion)
I dont know how to do it. Every time i try to use TAR commands i get TAR Archive with folder inside not with sigle FILE
Does any one knows how or what format are this files inside archive? i use Windows and know how to add a FILE to TAR archive but not how to convert a FOLDER to a file that later ill Archive!!!!
Helpppppppppp 2 bloody weeks already and my boss on my case all day!!!!
Thx
There is a way to do this tar -zxv -f /some/dir/* -C /another/dir
lots of tgzs in /some/dir and I want extract all to /another/dir
Using vars and subshells is the only way ?
ps. sorry for my english
it is very good examples and more descriptive ..
@Paulo: the command that will get you what you are looking for is this:
ls -1 /some/dir/*.tar | xargs -L1 -I{} tar zvf {} /another/dir/
assuming you have xargs installed on your machine; and you should, it is common. To see what the commands will look like before they execute, run the command like this:
ls -1 /some/dir/*.tar | xargs -L1 -I{} echo tar xvf {} /another/dir/
and if they are what you are looking for, remove the echo command, and it will run. This will extract all the files that have .tar extensions in the source directory to the target directory. that is an ‘EL’ and an ‘EYE’ btw, in the xargs command, those switches inform xargs to execute each line of the ls output separately (-L1), and to replace the symbols {} with the output file (-I{}). Assume proper extensions, etc, etc… mileage may vary.
@Ramesh: tar does not use regular expressions, your example is for globbing, not regex, they are two different things.
@Gil: it sounds like you are just decompressing the file with 7z. if you take that file, the one without the extension, and run THAT through tar, you will get your files. tar’s purpose is to concatenate single files (and folders) recursively into a single file, like a portable file system, that can then be easily moved between machines, but it does not compress anything itself, it relies on outside libraries to do that, like gzip or bzip2. So, if your file has an extension like tgz, ot tb2, etc., that is a file that is first tarballed, then it is compressed, so, you need to reverse the process, first decompress, then un-tar. if the extensions give you hints to the compression type, and they are gzip or bzip2, you can use the switches and tar will handle the decompression for you, else, you need to first decompress, then un-tar. You can use anything to compress a tar, once it is tarred, but only the ‘b’ and ‘j’ switches are available for in-process decompression (as far as I know). So, the short of it, if the compression type is hinted in the file extension, use the switches to decompress, and the command will look like this:
tar xv[zj]f file.[tgz|tb2] /output/folder
(the [] groups are your options; j with tb2 or z with tgz)
or, if the compression is known to be outside the scope of tar, such as rar, then the command would look like this (if using, say rar):
unrar file.trar | tar xvf – /output/folder
HTH
PS – since you are using 7z:
perhaps your command would look like
7z e file.t7z | tar xvf – /output/folder
Hopefully, that gives you the idea.
@travis (33)
Thanks a lot, travis. I’m just starting to learn about shell, my next readings
will be man xargs. I was just curious if there is a way to do that with tar
directly. In my shell script a simple loop did the job 🙂
Thanks again, thegeekstuff.com is in my shell script bookmark.
very nice examples
It is very helpful .Thanks lot.
Hi,
Compress the file using ‘tar (file should be replaced by tar file)
Could you please help me to find out this answer?
Thanks.
@Indhumathi
Tar is a multi-file archiver, so a 1 to 1 correspondence between original and compressed file is not assumed. gzip/gunzip will do this. if you want to use tar, you can as a second step use the -t option to read the files in the tar archive and delete the originals.
tar -tzf compressed.tgz | while read line; do rm `echo $line | awk ‘{print $6}’` ; done
Thank you Roger
Hi
in wc command the -c option shows amount of data in byte not KB according to its man page:
———————————————————-
-c, –bytes print the byte counts
———————————————————-
I am user the following command to take my data backup in AIX:
gtar cvf – folder | compress -> filename.tar.Z
I need the command output redirected to a text file.
How do I do that?
@Bijesh Lal Nyachhyon:
Try gtar cvf – folder | compress – > filename.tar.Z 2>logfile.txt
Second stream is STDERR
Thanks a lot.
No matter now many times I’ve used tar I’ve always forgotten the correct switches to use. Thanks for this – it suddenly all seems so simple.
Thanks a lot.. It is very useful and handy
Thank you so much, it is very helpful for a beginner like me.
You r doing just nice job…
just im learning linux…you helped me a lot…about all commamds…because they r confusing to me
thank you…
this is about the 10th time I google and open this article for a hint on tar … hope one day I finally learn the commands by heart 🙂
thanks!
Hi Ramesh,
I want to learn Linux but i am not from technical background, could you please help me how to start this.
I will be very thankful to you if you can help me in understanding the basics of Linux.
Just for information the option -f needs to be at the ends of the options otherwise the tar wont create the proper file you get an error
Nice tutorial … thanks very much.
Hope to see many stuffs from geekstuff moving forward
your tutorials are #1 source for me 🙂
good job
Quite useful commands…I was directed here through other commands page….all of these are explained beautifully !!!
Thanks a lot for the clear explanation of the commands.It helped a lot. 🙂
if a file is following the options, the f-parameter needs to be the last one
so
tar -cvljf file
not
tar -cvlfj file
Can someone explain to me the difference between tar and gtar?
Somewhere I found that tar only supports up to 2GB while gtar to 8GB
Is this the only difference?