tar
creates a compressed archive. the file extension of the resulting archive is .tar
and the tar archives are called tarballs
..
GNU ‘tar’ saves many files together into a single tape or disk archive, and can restore individual files from the archive.
A .tar
file is an archive, but it’s not compressed. .tar.gz
is a an archive that is compressed.
c
is for create, –create a new archivev
is for –verbose, verbosely list files processed, list out files as they are added to the archivef
is for –file=ARCHIVE, use archive file or device ARCHIVE, basically, specify the tar file that you wanna create. Since f
lets you take an input it should always be the last flag..tar
can also work off of STDIN and STDOUT, f
lets you specify if you wanna work with an existing tar file or if you wanna create one.
z
is for compression, –compress (gzip) and –uncompress (gunzip)x
is for –extractt
is for –list, list the contents of an archiver
is for –append, add more files to an existing archiveu
is for –update-p
restore permissions (can only be ysed with x
)-P
preserve pathnames-k
Keep (don’t overwrite) existing filestar
commands:compress a dir:
1tar -cvf archive.tar dir-to-archive
compress multiple files
1tar -cvf archive.tar file1 file2 file3
compress multiple files and dirs
1tar -cvf archive.tar dir1 dir2 file2 file3
You can compress on the fly with the z
flag
1tar -czvf archive.tar.gz dir-to-archive
You can also use gzip
on a tarball to compress it.
1gzip archive.tar
Use the x
flag instead of c
Extract all files from archive.tar.
1tar -xf archive.tar
Extract a compressed tarball
1tar -xzvf archive.tar.gz
Use the -p
flag. It’s to
Restore permissions (including ACLs, owner, file flags)
Use the t
or --list
flag
1tar -tzvf archive.tar.gz
Handy when you wanna know what’s in a tarball, or to see if it’ll extract in the current dir or in it’s own subdir. Sometimes, you get a tar bomb when a tarball starts extracting all the files in your current dir instead of making its own folder.. Potentially clobbering files in your current dir, like README or something, that you might already have in the folder you are working in.
Use the r
flag
Use the u
flag. It updates the archive with new files, it also updates the existing files to their newer versions.
only append files newer than copy in archive
cat
in a compressed file is zcat
. Use zcat
to read files in an archive..
f
should be the last flag because it takes an optionc
, x
and t
so it’s clear right away if you are creating, extracting or listing..