13 useful tar commands for your daily uses

13 useful tar commands for your daily uses

List of various tar commands for your daily uses.

  1. Create an (uncompressed) archive.
  • Create: tar cvf data.tar data/
  • Extract: tar xvf data.tar

2. Create and compress in xz format

Note: xz = LZMA = Lempel–Ziv–Markov chain algorithm

  • Create: tar Jcvf data.tar.xz data/
  • Extract: tar xvf data.tar.xz

3. Create and compress in gzip format

  • Create: tar zcvf data.tar.gz data/
  • Extract: tar xvf data.tar.gz

4. Create and compress in bzip2 format

  • Create: tar jcvf data.tar.bz2 data/
  • Extract: tar xvf data.tar.bz2

5. Create and compress in zip format

  • Create: zip data.zip -r data/
  • Extract: unzip data.zip
  • Extract to a directory: unzip data.zip -d data/
  • Create with password: zip -P <password> data.zip -r data/
  • Unzip with password: unzip -P <password> data.zip

6. To encrypt/decrypt a (tarred) file with OpenSSL

Refer to this post.

7. Create and extract files in lzip format

  • Note: lzip is required and can be installed with for example sudo apt install -yq lzip.
  • Create: tar cvf data.tar data && lzip -v data.tar.
  • Extract: tar --lzip -xvf data.tar.lz, or, lzip -d data.tar.lz && tar xvf data.tar && rm data.tar.

Results of compressing 3k text files

Extension Size Size reduction
tar 36MB 0%
tar.xz 6.8MB 81%
tar.gz 11MB 69%
tar.bz2 7.5MB 79%
zip 13MB 64%
tar.lz 6.8MB 81%
Buy Me A Coffee