Using GNU Tar

Shared

VPS

Dedicated

 

WP Professional

WP Professional Plus

The tar command is used to join many files together into one, and optionally compress them. The process is totally reversible. This is used for archival purposes, backups or for sending on to others.

Creating a Tar file

The tar command has two main options relating to creating an archive. These are -c and -z. The format of a tar command is tar [options] file/directory. The -c option means create and the -z option mean gzip the resulting file. The other option we will look at here is the -f option, which specifies the output file. Putting all this together the most common use of the tar command to create an archive would be tar -czf output.tar.gz directory. This command will tar all the files together and then gzip them.

Extracting a Tar file

To extract a tar file you would use the -x option. You also need to specify the file to extract using the -f option. If the archive has a .gz extension you will also need to use the -z option to let tar know to unzip the contents. The final option that you may find useful is -v, this will output to the screen all the files it is extracting.

Putting all this together the most common two tar extraction commands are tar xvzf filname.tar.gz and tar xvf filename.tar

Advanced usage

Even though the commands above cover most regular usage of the tar command there is more that tar can do. To concatenate two archives use the -A option, to list the contents of an archive use -t, to append a file use the -r option and to update a file use the -u option. Here are some examples of each one:
tar -Af output.tar file1.tar file2.tar
tar -tfz filename.tar.gz
tar -rf filename.tar file-to-delete

Conclusion

For more information on tar please see the man pages man tar.

Updated on February 6, 2019

Was this article helpful?

Related Articles

Need Support?
Can't find the answer you're looking for?
Contact Support