Dec 082014
 

As a Red Hat Linux server administrator, one command I’ve frequently resorted to is tar.

Tar (the command originates from tape archive) is also one of the first commands a Linux newbie must quickly learn.

Uses of Tar

Now why would anyone need to learn to use tar?

Tar is used to combine multiple files/directories into a single file that can then be moved, downloaded or copied.

Tar is also a convenient tool to backup key files on the server to your local system.

I’ve relied on tar to backup multiple web sites on my Linux server and download them to my local computer.

You can use tar with or without compression.

The benefit of compression is that in most cases it reduces file size significantly and thus facilitates faster downloads.

Tar – Basic Syntax

Before jumping into the nitty gritty of creating a tar file, let’s try to understand the basic syntax of creating and extracting a compressed tar file.

Create a tar of [directory-name]

$ tar -zcvf archive-name.tar.gz directory-name

-z: Compress archive using gzip program
-c: Create archive
-v: Verbose i.e display progress while creating archive
-f: Archive File name

Extract a tar file

$ tar -zxvf archive-name.tar.gz

-z: Uncompress archive using gzip program
-x: Extract archive
-v: Verbose i.e display progress while creating archive
-f: Archive File name

Use of the v option (above) is not compulsory.

Here are a bunch of tar commands you’ll find useful as a Linux administrator or if you’re just tinkering on the command line:

1. Create a Tar file without zipping

$ tar -cvf tommy.tar inputfile1 inputfile2 inputfile3 inputfile4

In the above example, we’re creating an unzipped (uncompressed) tar file called tommy.tar that includes four input files. We will continue to use our fictional tommy.tar for the other tar commands too.

2. Extract an uncompressed tar file

$ tar -xvf tommy.tar

where tommy.tar is the tar file we created earlier.

3. Create a Tar file with compression (zipping)

$ tar -zcvf tommy-december-2014.tar.gz december-2014

Above command will create an archive called tommy-december-2014.tar.gz in current working directory

4. Extract (Restore) a Compressed (zipped) Tar file

$ tar -zxvf tommy-december-2014.tar.gz

The above command will restore files in current working directory

5. Extract into Different Directory
There are two ways to extract files into a particular directory (for example in /Bobby):
a) Here’s the first method:

$ tar -xvf tommy.tar.gz -C /Bobby

To test if the files have been extracted properly, head over to the “bobby” directory and then run the ls command:
cd /Bobby
ls

b) cd to the destination directory /Bobby and then untar our tommy.tar.gz file there

$ tar -zxvf tommy.tar.gz

Peek Inside Tar Files

Now for some reason or the other you might want to see what’s inside a tar file before extracting it.

You can peer inside regular (unzipped) tar files as well as inside zipped tar files before extracting them.

6. To see, what’s inside a plain (unzipped) tar file, run the below command:

$ tar -tf tommy.tar

where tommy.tar is the tar file you want to look inside and option -t lists the files in the archive.

7. To see what’s inside a zipped tar file, the below command should do the trick:

$ tar -ztf tommy.tar.gz

where tommy.tar.gz is the tar file you want to look inside

8. But what if you’re playing with a tar.bz2 file.

Not to worry, sweetie. To peer inside a bzip2 tar file, you’d run the following command:

$ tar -jtf tommy.tar.bz2

Tar – Adding Files

There might be certain circumstances where you’ve already created a tar file but want to add a new file or directory.

Here’s what you’d do in such situations: Use the option -r.

9. For instance, to add a new (additional) file to an existing tar archive like tommy.tar, you’d run the below command:

$ tar rvf tommy.tar newfile

10. To add a new (additional) directory to an existing tar archive, run the below command:

$ tar rvf tommy.tar newdir/

Caveat – You cannot add a file or directory to a compressed archive. If you try to do so, you will get the following error messages:

tar: Cannot update compressed archives
tar: Error is not recoverable: exiting now

Tar – Everything

What if you want to tar everything in a folder(s) including hidden files.

11. Run the below command to tar all files in a folder including the hidden files starting with a dot:

$ tar -zcvf tommy.tgz ./

Selective Extraction of Tar Files

There might arise situations where you want to extract only some files from a tar file, particularly when the tar includes hundreds of files.

Single File Extraction
12. To extract a single file called bobby.txt from tommy.tar use the following command.

$ tar -xvf tommy.tar bobby.txt

You can even untar a single file from a tar.gz file i.e. a compressed file. Here’s an example:

13. To extract a single file jupiter.html from tommy.tar.gz archive file, use the below command:

$ tar -zxvf tommy.tar.gz jupiter.html

14. If you’re using bzip, the command will be different. For instance, to extract mercury.txt from tommy.tar, run the below command:

$ tar -jxvf tommy.tar.bz2 mercury.txt

Multiple Files Extraction
You can extract multiple files from a tar file.

Here are the commands for tar, tar.gz and tar.bz2 archive files respectively:

15. How to extract multiple files from a tar file:

$ tar -xvf tommy.tar "file 1" "file 2"

16. Use the below command to extract multiple files from a compressed tar file:

$ tar -zxvf tommy.tar.gz "file 1" "file 2"

17. Run the below command to extract multiple files from a tar.bz2 file:

$ tar -jxvf tommy.tar.bz2 "file 1" "file 2"

Wildcard Extraction
You can extract a bunch of files using Wildcard

In the below example, we extract a group of files whose extension begins with .php from a tar, tar.gz and tar.bz2 archive file respectively:

18. Use the below command to extract all php files from a tar file:

$ tar -xvf tommy.tar --wildcards '*.php'

19. Run the below command to extract all php files from a compressed tar.gz file:

$ tar -zxvf tommy.tar.gz --wildcards '*.php'

20. Use the below command to extract all php files from a compressed tar.bz2 file:

$ tar -jxvf tommy.tar.bz2 --wildcards '*.php'

Common Tar Issues

One of the issues you might encounter when you extract a tar file is that it includes the leading parent directory too.

21. Here’s how you can avoid the inclusion of leading parent directory in your tar file by using the C option while creating a tar file:

$ tar -C /home/mike-pc -cvf tommy.tar Documents

Above command will create a tar archive without including parent directory

22. Below command will extract the files you want without leading parent directory:

$ tar -xaf tommy.tar.gz --strip-components=1

Permissions

Tar files preserve permission.

But some folks insist we must use the -p option when extracting the tar file.

23. If you want to be sure on permissions, run the below command:

$ tar -xvpf file.tar.gz

Verifying Tar Files

24. When you are creating a new tar file, use the -W option to verify the archive file by using the below command:

$ tar cvfW archivefile.tar dir/

Should you see something similar to following output, something has changed:

Verify 1/filename
1/filename: Mod time differs
1/filename: Size differs

But if you just see the Verify line by itself, then the tar file or directory is fine:

Verify 1/filename

Note: The -W option will not work for compressed files.

25. For gzip compressed files, find the difference between the gzip archive and the file system by running below command:

$ tar dfz 1/filename.tgz

26. If you’re using bzip2 compressed files, find the difference between the bzip2 archive and the file system by issuing the following command:

$ tar dfj 1/filename.tbz

Find Tar File Size

On the command line, you can find the size of a compressed tar file or an uncompressed tar file.

Here are three ways to find the size of a compressed tar file. Use any of them:

27. First method to find size of a tar.gz file:

$ du -h tommy.tar.gz

28. Second method to find size of a tar.gz file:

$ wc -c tommy.tar.gz

29. Third method to find size of a tar.gz file:

$ ls -l tommy.tar.gz |awk '{print $5}'

30. To get the size of an uncompressed tar file without extracting it, run the following command:

$ zcat tommy.tar.gz | wc -c

31. The below command will provide the size of both compressed and uncompressed tar file:

$ gzip -l compressed.tar.gz

Note: I’ve used tommy merely as an example. Replace tommy with your preferred name while practising any of the above commands.

There’s a lot more to tar than what I have described above.

Learning the above commands is a solid start to mastering the tar command.

Related Tar Posts:
GNU tar Manual
 Posted by at 9:25 pm

Sorry, the comment form is closed at this time.