Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
April 5, 2022 09:29 am GMT

Working With Archives :: Hitchhiker's Guide to Linux

Working with archives in the shell is simple, you just need to understand two concepts: compression and archiving (and how they fit together).

First, let's take a look at the compression part. gzip, bzip2 and xz are the most common compression formats in the Linux world.

gzip, bzip2 & xz

Compressing a file:

gzip myfilebzip2 myfilexz myfile

Uncompressing a file:

gunzip myfile.gzbunzip2 myfile.bz2unxz myfile.xz

If (for some weird reason) you don't already have any of those tools installed, you can install them with something like apt install gzip bzip2 xz-utils tar.

This works great for a single file, but it's not easy to create archives of multiple files/directories this way, so we need tar.

tar

Making a tar archive of multiple files/directories is easy:

tar cvf archive.tar path1 path2

Now that we have a single file, we can easily compress it:

gzip archive.tarbzip2 archive.tarxz archive.tar

We could pipe the result of tar command to gzip/bzip2/xz, but that's still a bit cumbersome, so we can tell tar command directly to combine those two steps for us:

tar czvf archive.tar.gz path1 path2tar cjvf archive.tar.bz2 path1 path2tar cJvf archive.tar.xz path1 path2

(just remember that z switch stands for gzip, j for bzip2, and J for xz compression)

Extracting archives:

tar xvf archive.tartar xzvf archive.tar.gztar xjvf archive.tar.bz2tar xJvf archive.tar.xz
  • with GNU tar, you can omit the switch for the compression type and it will be autodetected from the extension
  • if you want to extract the archive into a different directory, just add -C /destination_dir
  • .tgz, .tbz2 and .txz extensions are just a (less common) shorthands for .tar.gz, .tar.bz2 and .tar.xz

In terms of efficiency, xz generally has the highest compression rate, and it's faster than bzip2 at decompressing (at the expense of the compression time). gzip has the lowest compression rate, but it's fast.

Other formats

These formats are more common on other platforms than Linux, but working them from CLI is generally as easy as with tar archives.

zip

Creating archives:

zip archive.zip path1 path2

Extracting archives:

unzip archive.zip
  • add -d /destination_dir to specify the target directory

Note:

  • in case you don't have them already installed, you can install the required packages with something like apt install zip unzip

rar

Creating archives:

rar a archive.rar path1 path2

Extracting archives:

unrar e archive.rar

To extract the archive to a specific directory, just add it at the end:

unrar e archive.rar /destination_dir

Note:

  • in case you don't have them already installed, you can install the required packages with something like apt install rar unrar

7z

Creating archives:

7z a archive.7z path1 path2

Extracting archives:

7z e archive.7z
  • add -o /destination_dir to specify the target directory

Note:

  • in case you don't have them already installed, you can install the required packages with something like apt install p7zip

Note: this is a snapshot of (WIP) topic from the Understanding Simplicity wiki. All suggestions (and reactions) are welcome. You can find the latest version here: Working With Archives


Original Link: https://dev.to/devsimplicity/working-with-archives-hitchhikers-guide-to-linux-4ifg

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To