What is tar?

tar is primarily used for combining multiple files and directories into a single archive file (aka tarball), simplifying storage, backup, and transfer. It can also compress these archives to save disk space.

Syntax

tar <operation> [options] <archive-files> <files/directories>
  • operation: The main action (eg:create,extract,list).
  • options: Additional behaviours (eg: compression,verbosity).
  • archive-files: Name of the archive.
  • files/directories: What to include or extract.

Essential tar Options

OptionPurpose
-cCreate a new archive
-xExtract files from an archive
-tList contents of an archive
-fSpecify archive file name
-vVerbose output
-zUse gzip compression
-jUse bzip2 compression
-JUse xz compression

Practical Usage

  1. Create a basic tar archive:
tar -cvf archive.tar file1.txt file2.txt directory1/

Combine file1.txt, file2.txt and directory1/ into a single archive named archive.tar.

  1. Extract an archive:
tar -xvf archive.rar
  1. List contents of an archive:
tar -tvf archive.tar

Displays the contents of the archive without extracting.

Intermediate tar Usage

Compression

  1. Create a gzip-compressed archive:
tar -czvf archive.tar.gz dirName/
  1. Create a bzip2 compressed archive:
tar -cjvf archive.tar.bz2 dirName/

Extracting With Compression

  1. Extract a gzip-compressed archive:
tar -xzvf archive.tar.gz
  1. Extract a bzip2-compressed archive:
tar -xjvf archive.tar.bz2
  1. Extract to a specific directory:
tar -xvf archive.tar /home/iamyaash/extractLocation/
  1. Exclude files when archiving:
tar -cvf archive.tar dirName/ --exclude="*log"

Excludes only .log files when archiving. 5. Extract a single file:

tar -xvf archive.tar /archiveDir/file.txt

If you want to extract only wordpress/xmlrpc.php from archive.tar, you would run the above command. This command will extract only that file, not the entire archive in the current directory.

  1. Extract a single file to a specific directory using the -C option:
tar -xf archive.tar -C /target/directory path/to/file
  1. Extract files matching a pattern:
tar -xvf archive.tar --wildcards "*.php"

Advanced tar Usage

Append or Update Files In An Archive

  1. Append a file:
tar -rvf archive.tar newFile.txt
  1. Update only newer files:
tar -uvf archive.tar updatedFile.txt

Delete Files From An Archive

tat --delete -f archive.tar unwantedFile.txt

Check Archive Size

tar -czf - dirName/ | wc -c

Outputs the size in Bytes.

Verify Archive Integrity

tar -tvg archive.tar

It lists the contents and checks for errors during listing.

Compare Archive With File System

tar -df archive.tar dirName/

Shows differences between the archive and current files.

xz Compression:

tar -cJvf archive.tar dirName/

Split Large Archives

While tar itself doesn’t split files, you can combine it with split command:

tar -cvf - dirName/ | split -b 100M -archive_part_

splits the archive into 100MB parts.

This is really a useful method of using archives, when you archive a single sometimes it get’s way over the size limit to copy to another machine. If a single file exceeds the certain size limit, the operating system won’t let you make the copy/move operation. It happens in Windows as far as I know. I have once tried to copy a games which is archived into a single file, when attempted to make copy operation, the Windows shows a dialog box stating that this file exceed the size limit and the operation can’t be made.

TaskCommand Example
Create archivetar -cvf archive.tar dir1/
Create gzip archivetar -czvf archive.tar.gz dir1/
Create bzip2 archivetar -cjvf archive.tar.bz2 dir1/
Extract archivetar -xvf archive.tar
Extract gzip archivetar -xzvf archive.tar.gz
Extract bzip2 archivetar -xjvf archive.tar.bz2
List archive contentstar -tvf archive.tar
Exclude filestar -cvf archive.tar dir1/ --exclude='*.log'
Append filestar -rvf archive.tar file.txt
Delete from archivetar --delete -f archive.tar file.txt
Extract to directorytar -xvf archive.tar -C /target/dir/