tar – Save many files together to a single tape or disk archive, and restore the desired files individually from the archive

The tar command is used to create and manipulate archive files. It can be used to compress and decompress files, as well as to store multiple files in a single archive file. The resulting archive file can be used for backup purposes or for transferring files between different systems.

The basic syntax for creating an archive file is as follows:

tar -cvf archive.tar file1 file2 file3

This command will create a new archive file called archive.tar and add file1, file2, and file3 to it. The -c option tells tar to create a new archive file, the -v option is used to display the progress of the operation, and the -f option specifies the name of the archive file.

To extract files from an archive, use the following command:

tar -xvf archive.tar

This command will extract all files from the archive.tar file to the current directory. The -x option tells tar to extract files from an archive, and the -v option is used to display the progress of the operation.

To view the contents of an archive file, use the following command:

tar -tvf archive.tar

This command will display a list of all files contained in the archive.tar file. The -t option tells tar to display the contents of an archive file.

To compress an archive file, use the following command:

tar -czvf archive.tar.gz file1 file2 file3

This command will create a new archive file called archive.tar.gz and compress file1, file2, and file3 before adding them to the archive file. The -z option tells tar to compress the files using gzip compression.

To decompress a compressed archive file, use the following command:

tar -xzvf archive.tar.gz

This command will extract all files from the archive.tar.gz file and decompress them to the current directory. The -x option tells tar to extract files from an archive, the -z option tells tar to decompress the files using gzip compression, and the -v option is used to display the progress of the operation.

Options

Option Description
-c Create a new archive file
-x Extract files from an archive
-t Display the contents of an archive file
-v Display the progress of the operation
-f Specify the name of the archive file
-z Compress or decompress files using gzip compression
-j Compress or decompress files using bzip2 compression
-p Preserve file permissions and ownership
-r Append files to an existing archive
-u Add new files to an archive only if they are newer than the existing ones
-k Do not overwrite existing files
-C Change to a different directory before performing the operation

Troubleshooting Tips

  • If you receive an error message stating that the archive file is not found, check the spelling and location of the file.
  • If you receive an error message stating that the archive file is corrupted, try decompressing it with a different tool or download the file again.
  • If you receive an error message stating that there is not enough disk space, free up some disk space before attempting to create or extract the archive file.

Notes

  • The tar command can be used in conjunction with other commands, such as find and grep, to create more complex operations.
  • The tar command is available on most Linux distributions and can also be used on other Unix-like systems.