Using tar to Archive, Extract, Compress, and Uncompress Files
![](/media/posts/linux-unix/tar-compress/library-shelves-pexels.jpeg)
The Commands
# 1: tar (create uncompressed archive) all files and directories in the current working directory recursively into an uncompressed tarball
tar cvf filename.tar *
# 2: Untar (extract uncompressed archive) all files and directories in an uncompressed tarball recursively into the current working directory
tar xvf filename.tar
# 3: tar (create gzipped archive) all files and directories in the current working directory recursively into a tarball compressed with gzip
tar cvzf filename.tar.gz *
# 4: Untar (extract gzipped archive) all files and directories in a tarball compressed with gzip recursively into the current working directory
tar xvf filename.tar.gz # Note: same options as 2 above
# 5: tar (create bzip2'ed archive) all files and directories in the current working directory recursively into a tarball compressed with bzip2
tar cvjf filename.tar.bz2 * # Note: little 'j' in options
# 6: Untar (extract bzip2'ed archive) all files and directories in an tarball compressed with bzip2 recursively into the current working directory
tar xvf filename.tar.bz2 # Note: same options as 2 and 4 above
# 7: tar (create xz'ed archive) all files and directories in the current working directory recursively into a tarball compressed with xz
tar cvJf filename.tar.xz * # Note: capital 'J' in options
# 8: Untar (extract xz'ed archive) all files and directories in an tarball compressed with xz recursively into the current working directory
tar xvf filename.tar.xz # Note: same options as 2, 4, and 6 above
Introduction
Here I show how to use the Linux tar
command to create uncompressed tarballs (archives) and tarballs compressed with various
compression algorithms of all files and directories in the current working directory. I also show how to simultaneously uncompress (if necessary)
and extract all the files into the current working directory.
Discussion
All the above commands were tested on an Ubuntu 18.04.4 LTS (bionic)
. For complete details, see the Ubuntu man page for tar
.
Here is a breakdown of all the options used above directly from the man page:
-c, –create
create a new archive
-x, –extract, –get
extract files from an archive
-v, –verbose
verbosely list files processed
-z, –gzip
filter the archive through gzip
-j, –bzip2
filter the archive through bzip2
-J, –xz
filter the archive through xz
-f, –file=ARCHIVE
use archive file or device ARCHIVE
You can combine these options together without the need for any dashes as I have done above. There is no need to have an =
sign between the f
option and the filename for the archive.
Thanks to Pexels for the free header image.