Tar


From Linuxconfig.org

Jump to: navigation, search

Name

tar - The GNU version of the tar archiving utility

Synopsis - man page

tar  [ - ] A --catenate --concatenate | c --create | d --diff --compare
     | --delete | r --append | t --list | u --update | x --extract  --get  [
     options ] pathname [ pathname ... ]

Frequently used options

-f, --file [HOSTNAME:]F
      use archive file or device F (default "-", meaning stdin/stdout)
-j, --bzip2
      filter archive through bzip2,  use  to  decompress  .bz2  files.
-N, --after-date DATE, --newer DATE
      only store files newer than DATE
-v, --verbose
      verbosely list files processed
-w, --interactive, --confirmation
      ask for confirmation for every action
-z, --gzip, --gunzip, --ungzip
      filter the archive through gzip

Examples

For the sake of simplicity we show linux tar examples without using a tape drive. Many of the tar examples can be translated to use tape drive. For example: Create a archive of /etc/ directory to a file etc.tar

tar -cf etc.tar /etc

to create a archive to the tape (/dev/st0 ) instead of archive file we can translate this command to:

tar -cf /dev/st0 /etc

During this tar tutorial we will use "archivewithtar/" directory as sample directory. Here is a content of this sample directory:

Image:sample_tar_dir.gif

Create an archive to linuxtarfile.tar file of the archivewithtar/ directory:

tar cf linuxtarfile.tar archivewithtar/

Image:create_tar_archive_to_file.gif

To see what files are being archived by tar command we can use - v ( tar verbose mode ):

tar cf linuxtarfile.tar archivewithtar/

Image:create_tar_archive_to_file_verbose_mode.gif

To instruct tar command to list all files within a archive file we can use command:

tar tf linuxtarfile.tar

Image:make_tar_to_list_allfiles_within_file.gif

To extract just particular files from a tar archive file we need to replace "c" option with "x". Here we extract archivewithtar/dmesg to a restore/ directory.

tar xvf linuxtarfile.tar archivewithtar/dmesg

Image:tar_extract_single_file.gif

Extract entire contents of the tar archive linuxtarfile.tar file to restore/ directory:

tar xvf linuxtarfile.tar archivewithtar/dmesg

Image:tar_extract_entire_contents_from_archive.gif

Linux tar archive command does not use compression by default. Most commonly used compression types with tar command are -z ( gzip ) and -j ( bzip2 ).

Create compressed archive of archivewithtar/ with gzip and bzip2:

tar czf linuxtarfile.tar.gz archivewithtar/
tar cjf linuxtarfile.tar.bz2 archivewithtar/

Compare the size of all three tar archives files: no compression, gzip and bzip2:

Image:tar_create_compressed_archive_gzip_bzip2.gif

The approach to extract files from compressed tar archive is the same as to extract data from non-compressed tar archive. Here we extract files from gzip compressed tar archive file:

tar xvzf linuxtarfile.tar.gz

Image:extract_from_compressed_tar_archive.gif

Linux tar archive command can create simple incremental or differential backup with -N or --newer option. Let's consider two files in our archivewithtar/ which have different change time:

Image:files_with_different_change_time.gif

Use tar to archive files newer than "7.2.2008 13:54:00" ( make sure to you use correct tar date format !):

tar cvf linuxtarfile.tar --newer  "2008-02-07 13:54:00" archivewithtar/ 

Image:tar_archive_files_newer_date_format.gif

Tar command in interactive mode gives us an option which files to extract:

tar xvjwf linuxtarfile.tar.bz2

Image:tar_interactive_mode_w.gif

Use -p ( --preserve-permissions )tar option to preserve permissions file permission:

tar czfp linuxtarfile.tar.gz archivewithtar/
Personal tools
Linux Commands