tar

From RaySoft

tar is an archiving program designed to store and extract files from an archive file known as a tarfile. A tarfile may be made on a tape drive, however, it is also common to write a tarfile to a normal file.[1]

Documentation

Syntax

tar [PARAMETER ...] [FILE/DIRECTORY ...]

Parameters

-A, --catenate, --concatenate
Append tar files to an archive.
-c, --create
Create a new archive.
-d, --diff, --compare
Find differences between archive and file system.
--delete
Delete from the archive.
-r, --append
Append files to the end of an archive.
-t, --list
List the contents of an archive.
-u, --update
Only append files that are newer than the existing in archive.
-x, --extract, --get
Extract files from an archive.
--atime-preserve
Don't change access times on dumped files.
-C DIR, --directory=DIR
Change to directory DIR.
--exclude PATTERN
Exclude files based upon PATTERN.
--exclude-vcs
Exclude files and directories used by following version control systems.
-f ARCHIVE, --file=ARCHIVE
Use archive file or device ARCHIVE.
-g BACKUP, --listed-incremental=BACKUP
Handle new GNU-format incremental BACKUP.
-h, --dereference
Don't dump symlinks; dump the files they point to.
-j, --bzip2
Filter archive through bzip2, use to decompress .bz2 files.
-N DATE, --after-date DATE, --newer DATE
Only store files newer than DATE.
--one-file-system
Stay in local file system when creating archive.
-p, --same-permissions, --preserve-permissions
Extract all protection information.
-S, --sparse
Handle sparse files efficiently.
-v, --verbose
Verbosely list files processed.
-z, --gzip, --ungzip
Filter the archive through gzip.

Examples

Unpack a gzipped tarfile
tar --extract --ungzip --file='test.tar.gz' "${HOME}/tmp"
Get / store a tar file from / on a remote machine using SSH
tar --create --bzip2 --file=- "${HOME}/office" "${HOME}/private" \
| ssh 'melman@neon.raysoft.loc' "cat >${HOME}/backup/$(date +%F).tar.bz2"
ssh 'melman@neon.raysoft.loc' "tar --create --bzip2 --file=- \
   ${HOME}/office ${HOME}/private" >"${HOME}/backup/$(date +%F).tar.bz2"
Create a backup of an operating system using tar
tar --create --gzip --file="/mnt/backup/$(date '+%F').tar.gz" \
  --exclude='/backup.tar.gz' --exclude='/dev' --exclude='/proc' \
  --exclude='/sys' --exclude='lost+found' '/'
Create full & incremental backup of an operating system using tar
tar --create --gzip --file="/mnt/backup/$(date '+%F').tar.gz" \
  --listed-incremental='/mnt/backup/current.snar' \
  --exclude='/dev' --exclude='/mnt' --exclude='/proc' \
  --exclude='/sys' --exclude='lost+found' '/'

cp '/mnt/backup/current.snar' "/mnt/backup/$(date '+%F').snar"
tar --create --gzip --file="/mnt/backup/$(date '+%F').tar.gz" \
  --listed-incremental='/mnt/backup/current.snar' \
  --exclude='/dev' --exclude='/mnt' --exclude='/proc' \
  --exclude='/sys' --exclude='lost+found' '/'

cp '/mnt/backup/current.snar' "/mnt/backup/$(date '+%F').snar"
Restore full & incremental backup using tar
for file in $(ls -1); do
  tar --extract --listed-incremental='/dev/null' --file="${file}"
done

References

  1. man 1 'tar'