How to use tar to backup and restore folders and servers

The tar command, also known as a "tape archive", is used to compress files and folders in Linux operating systems. It creates a tar archive by converting a group of files or directories into a single compressed file. It allows you to decompress the tar archive, prints a list of all files included in the archive, and add the file to the archive. Generally, the system administrator uses the tar command to create and restore a backup on the server.

This tutorial will show you how to compress and decompress files and directories using the tar command in Linux.

Table Of Contents

Tar Command Syntax

The basic syntax of the tar command is shown below:

tar [option] [archive-name] [files or directories]

A brief explanation of each option is shown below:

  • -c: Used to create a tar archive.

  • -x: Used to extract from the tar archive.

  • -t: Used to display a list of files inside the tar archive.

  • -r: Used to add an additional file in the tar archive.

  • -W: Used to verify a tar archive.

  • -z: Used to create a tar archive using gzip.

  • -j: Used to create a tar archive using bzip.

  • -v: Used to display verbose information.

  • -f: Used to specify archive file name.

Create a Tar Archive

To create a tar archive named compress-file.tar from a single file named file1.txt, run the following command:

tar -cf compress-file.tar file1.txt

To create a tar archive named compress-file.tar from multiple files called file1.txt, file2.txt and file3.txt, run the following command:

tar -cf compress-file.tar file1.txt file2.txt file3.txt

If you want to create a tar archive of any directory, run the following command:

tar -cf compress-dir.tar dir1

This will create a tar archive named compress-dir.tar from a directory dir1.

Create a Tar Bz2 Archive

You can also compress a tar file using a bz2 algorithm. You will need to specify tar.bz2 at the end of the archive name.

The following command will create a tar.bz2 archive from the directory /etc.

tar -cjf compress.tar.bz2 /etc

You can also specify the -v option to display the compression process verbose.

Create a Tar Gzip Archive

You can use the -z option with the tar command to create a tar archive in gzip format.

The following command will create a tar.gz archive from the /usr directory.

tar -czf compress.tar.gz /use

List Content of Tar Archive

You can use the option -t with the tar command to display a list of content available in the tar archive file.

The following example will display a list of content inside the compress-dir.tar file:

tar -tvf compress-dir.tar

Extract a Tar Archive

You can use the option -x with the tar command to uncompress the tar archive.

For example, run the following command to uncompress a compress-dir.tar file:

tar -xvf compress-dir.tar

If you want to extract a single file named file1.txt from the compress-dir.tar file, run the following command:

tar -xvf compress-dir.tar file1.txt

Extract a Tar Gzip Archive

You can use the option -z with the tar command to extract or uncompress the Gzip archive file.

For example, run the following command to extract the compress.tar.gz archive file:

tar -xzvf compress.tar.gz

Extract a Tar Bz2 Archive

You can use option -j with the tar command to extract from the Bz2 archive.

For example, run the following command to extract the compress.tar.bz2 archive file.

tar -xjvf compress.tar.bz2

Extract a Tar Archive in a Different Directory

When you extract any tar archive, it will extract the archive contents in the current directory. You can use option -C with the tar command to extract the tar archive to the specified directory.

For example, run the following command to extract the compress-dir.tar content to the /opt directory:

tar -xvf compress-dir.tar -C /opt

Add and Remove a File from a Tar Archive

You can also add and remove specific files and directories to an existing tar archive. You can use the option -r to add a file to a tar archive and the option --delete to delete a file from the tar archive.

For example, to add a new file named file6.txt to the archive name compress-dir.tar, run the following command:

tar -rvf compress-dir.tar file6.txt

To remove a file named file1.txt from the archive named compress-dir.tar, run the following command:

tar --delete -f compress-dir.tar file1.txt

Extract Multiple Files from the Tar Archive

You can use a wildcard to extract a group of files from the tar archive.

For example, extract all files that match the pattern .html from the archive compress.tar.gz use the following command:

tar -xvzf compress.tar.gz --wildcards '*.html'

Conclusion

The above guide taught you how to compress and extract files and directories with the tar command. I hope you can now you the tar command to backup and restore files and directories.

Does Windows support tar file?

With WinZip, users of Windows and macOS may open files with the tar GZ file extension, mainly used on UNIX-based operating systems. Most frequently, Tar GZ files are used for archiving several files together. Bigger files can be sent and received in a compressed format.

Does tar remove original files?

By default, the original files we add to an archive will stay on the file system. The remove-files option allows us to omit the original files after adding them to the tar file. Then, it will remove the original files from the file system, but they will still be in the tar file.

Backup one server, database, or application for free forever.

No credit card required. Cancel anytime!
Was this page helpful?

Thank you for helping us improve!