linux

Helpers and common usage for Tar

Reading time of 521 words
3 minutes
Reading time of 521 words ~ 3 minutes


Did you find this article helpful?
Please consider tipping me a coffee as a thank you.
Ko-fi Buy Me a Coffee
Did you find this article helpful? Please consider tipping me a coffee or three as a thank you.
Tip using Ko-fi or Buy Me a Coffee

tar is a feature rich but often confusing archiving tool most commonly used on Linux and BSD systems. One of the great benefits of tar over other more modern tools such as 7z, RAR or ZIP is that it is open source, free and platform agnostic. Even more importantly for Linux users tar archives preserve user and file permissions allowing for easy directory backups and restoration.

Tar - Compress (backup) files and directories

Common arguments

-c create an archive

-f file.tar filename of archive

-v verbose

--totals statistics for archive creation

Total bytes written: 103157760 (99MiB, 1000MiB/s)

-z archive using gzip, quick but offers less compression

-zf file.tar.gz filename of gzip archive

-j archive using bzip2, slow but offers more compression

-jf file.tar.bz2 filename of bzip2 archive

--remove-files moves files into archive instead of copy

-p preserves file and directory permissions

--owner user override the ownership of files when placed into an archive

--exclude='/directory' --exclude='file' --exclude='*pattern*' exclude directory names, filenames or pattern matches

--ignore-case ignore case when using –exclude=

-h follow and include the content of symlinks

--no-recursion ignore all sub-directories and their content

Archive

A file in the current directory.

tar -cf archive.tar filename

A directory.

tar -cf archive.tar /directory/to/backup/*

A set of files and directories.

tar -cf archive.tar file1.txt /path/to/file2.log /different/directory/to/backup/*
Copy and compress a website for sharing and store it in the home directory
tar --totals -czvf ~/archive.tar.gz /var/www/*
Backup and compress the home directory by keeping permissions, but exclude hidden files and directories
tar --totals --exclude='.*' -czvpf ~/archive.tar.gz ~/*
Create an archive that is split over multiple files, 1000 MB in size

| split pipes the output of tar into split

-d uses numerical instead of alphabetical naming suffixes on the split archives

-b split size

- hyphen placeholder for what would normally be an input filename used by split

tar -cvpz /directory/to/backup/* | split -d -b 1000m - ~/archive.tar.gz
Create an archive that is stored on a remote server via SSH
tar -cvpz /directory/to/backup/* | ssh [email protected]:/directory/to/store/backup/ "( cat > archive.tar.gz )"

Add files into an existing archive

tar -rvf archive.tar /directory/or/path/to/files/*

Or you can use the update argument that will only append files that are newer than the copies contained within the archive.

tar -uvf archive.tar /directory/or/path/to/files/*

Join/Merge/Concatenate two archives together

The following will merge the content of archive2.tar into archive1.tar

tar -Avf archive1.tar archive2.tar

While this copy the archive archive2.tar into archive1.tar

tar -rvf archive1.tar archive2.tar

List the content of an archive

tar -tf archive.tar

Restore (extract) files or directories from an archive

Common arguments

-x extract an archive -f file.tar archive’s filename -v verbose

-C /directory/to/restore/to place restored files into the specified directory -p preserves file and directory permissions

Extract the archive to the active directory
tar -xf archive.tar
Extract the file named README from the archive to the active directory
tar -xf archive.tar README
Extract a gzip compressed archive to the active directory
tar -xvzf archive.tar.gz
Extract the archive to a specified directory
tar -xvf archive.tar -C ~/downloads
Extract a b2zip compressed archive to a specified directory
tar -xvjf archive.tar.bz2 -C ~/downloads
Learn more.

Written by Ben Garrett

Did you find this article helpful?
Please consider tipping me a coffee as a thank you.
Ko-fi Buy Me a Coffee
Did you find this article helpful? Please consider tipping me a coffee or three as a thank you.
Tip using Ko-fi or Buy Me a Coffee