Linux‎ > ‎

tar command examples

Thank you for visiting this page, this page has been update in another link tar command examples
Tar is the most popular archive tool on linux and unix, it arvhive files together into a single tape or disk archive, and can restore individual files from the archive.

How to use tar command

Usage: tar [OPTION...] [FILE]...
  tar -cf archive.tar foo bar  # Create archive.tar from files foo and bar.
  tar -tvf archive.tar         # List all files in archive.tar verbosely.
  tar -xf archive.tar          # Extract all files from archive.tar.
This syntax is easy to start, we start from this, then gradually introduce other options in.

Create a tar archive file

This command archive whole /var/lib/pg directory to pg_backup.tar
tar cvf /home/backup/pg_backup.tar /var/lib/pg
or you can use the following command to create a archive file for list of files/dirs
tar cvf /home/backup/ccc.tar file1 file2 file3 dir1 dir2 ...

-c -- verbosely list files processed
-v -- verbosely list files processed
-f, --file=ARCHIVE    use archive file or device ARCHIVE

Create a compressed tar archive file

To create a  gzip compressed tar archive file

tar cvfz archive.tar.gz file1 file2 ...

You can also use archive.tar as archive name, but most use case is to use .gz, easier to identify the file compressed type.

To create a bzip2 compressed tar archive file

tar cvfj archive.tar.bz2 file1 file2

Same as gzip compression type, you can also use archive.tar as archive name.

To create a xz compressed tar archive file

tar cvfJ archive.tar file1 file2

Same as gzip compression type, you can also use archive.tar as archive name.

Create a tape archive file

To users, there is no big different between writing files to disk and tape.

tar cvf </dev/tapedev> file1 file2

The different from writing disk is that tape device is a sequencial device, you need to move tape head to proper position, then run the command, otherwise, the previous archive could be overwriten.

Create a tape archinve file with specifed block size

For your application or performance need, you might need to specify tape block size

tar -b 8192 cvf </dev/tapedev> file1 file2

-b, --blocking-factor=BLOCKS
              BLOCKS x 512 bytes per record

Create multiple volumes tape archive file

If archive file is bigger than a single tape volume, you need let tar know

tar -ML <unit in 1024bytes> cf <device> mybigfile

List files in a  tar arvhive file

tar tvf archive.tar or tar tvf archive.tar.gz
Note: there is no different on listing between compress and uncompressed archive file.

extract a file from tar archive file

tar xvf archive.tar

Note: you don't have to necessarily specify the compression type, tar knows it

Extract a group files from tar archive file

To extract a list of files from archive

tar xvf archive.tar file1 file2 ...

or tar xvf archive.tar *.log

In the example above, it extract all .log files.

Add files to a tar archive file

To add a file to a tar archive, use -r option

tar rvf archive.tar fileN
Note: for compressed tar archive file, you can not append any new file to it

Update file to a tar archive file

update files that modified after archive, use -u option
       -u, --update
              only append files newer than copy in archive
tar uvf archive.tar dir1

Extract old files from archive

 If you have modified file appened to tar archive, when you extract the file, the newer one get extracted by default. To extract old version, run command
tar xvkf archive.tar file1
The tricky part is that when you extract the file, both old and newer versions are extracted, and the newer one always overwrit the old one. with -k option, it keeps old version.