Skip to content

Instantly share code, notes, and snippets.

@bengarrett
Created April 18, 2015 03:16
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save bengarrett/33c4d16cddc9cda3b744 to your computer and use it in GitHub Desktop.
Save bengarrett/33c4d16cddc9cda3b744 to your computer and use it in GitHub Desktop.
Helpers and common usage for tar

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 user-account@example.net:/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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment