Tar

From Wiki
Revision as of 19:26, 28 September 2021 by Scott (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

[options: c=create, x=expand, v=verbose, f=filename, z=zip]

To create a tar archive:

tar cvf new-archive.tar directory-to-archive

To create a tar archive but exclude a single directory:

tar cf new-archive.tar --exclude bad_dir directory-to-archive

To create a compressed tar archive:

tar czvf new-archive.tar.gz directory-to-archive

To decompress a .tar file:

tar xvf file.tar

To decompress a .tar.Z or .tar.gz file:

tar xzvf file.tar.Z
tar xzvf file.tar.gz

To append to a tar file:

tar rf existing.tar new_dir

To list the contents of a tar file:

tar tf file.tar

To extract a specific file or directory from a tar file:

tar xf file.tar file-or-directory-name

(file-or-directory-name should exactly match the output of tar t output)

tar under older Unix:

zcat file.tar.gz | tar xvf -

or

gunzip file.tar.gz; tar xf file.tar

To move a directory from one host to another (must have full permissions on both ends):

tar cf - my_data | ssh barney@myserver " ( cd /data/backup; tar xf - ) "

To see a progress bar (may need to install the pv tool):

tar cf - /folder-with-big-files | pv -s $(du -sb /folder-with-big-files | awk '{print $1}') | gzip > big-files.tar.gz