💾 Archived View for do.hedy.dev › drewdevault-gemini › tar-is-good-actually.gmi captured on 2024-06-16 at 12:17:11. Gemini links have been rewritten to link to archived content

View Raw

More Information

⬅️ Previous capture (2024-05-10)

-=-=-=-=-=-=-

Tar is good actually

This content is sourced from Drew DeVault's blog from before he removed the gemini capsule and content from his site.

This is an attempt to archive his gemini-only posts, as I found them to be of value, at least to me and wished to refer to them via an accessible URL.

Note that relative links within the content *will not work*. Links to static assets with extensions (jpg, webm, png) have been converted to git.sr.ht download URLs.

Page source

View on WWW

---

Let's talk about tar, that tool that everyone loves to hate.

xkcd: tar

For my part, I think tar is a really useful and under-rated tool. And unlike Cueball, I could defuse that bomb 😉

tar is a shell utility for making archives, often called "tarballs", which store several files. tar is a Unix-native file format, and includes information about file ownership and modes. This makes it very useful for moving groups files around on Unix systems.

The general usage of tar, like most Unix programs, is the following:

$ tar [-options] <files...>

The options you specify must include at least one operation, and optionally some number of flags. The most useful operations are create (-c), extract (-x), and list (-t). The most common flags are -C (to change the working directory first), -z (to add gzip compression), and -v (verbose mode, prints out each file name it archives or extracts).

To create a tar archive which contains the files "a.txt", "b.txt", and "c.txt", you can do the following:

$ tar -c a.txt b.txt c.txt > files.tar

You can add compression in one of two ways:

$ tar -c a.txt b.txt c.txt | gzip > files.tar.gz
$ tar -cz a.txt b.txt c.txt > files.tar.gz

Shell globbing is often useful here. To create an archive of all of the text files in documents:

$ tar -c documents/*.txt > documents.tar

Getting the path right is often important: tar uses the paths which appear on the command line. In this example, the tarball contains a directory called "documents" which is full of text files. Extracting this tarball somewhere else will create a "documents" directory and fill it with these files. If you want to store the files in the root of the tarball, you need to change to the "documents" directory first. You can do this in your shell, with cd, or you can use the -C flag:

$ tar -C documents/ -c text/ index.txt > documents.tar

This creates a tarball of the files at "documents/text/" and "documents/index.txt", but roots them in the tarball as "text/" and "index.txt".

You'll note that I'm consistently using | in each of these examples to form a pipeline. Another important use-case for tar is to move several files at once through a Unix pipeline. For instance, you can transfer files without scp or rsync like so:

$ ssh hostname tar -C sources/ -cz linux | pv | tar -xzv

This SSHes into "hostname", tars up "sources/linux/", and extracts it to linux/ on the client machine. The -xzv flags extracts, unzips, and prints each file name. Combined with pv, this gives me a nice progress indication for the transfer.

pv: I install it on all of my machines

That's all for today. I think tar is pretty cool and gets an unfairly bad rep. You should mess around with it sometime. Cheers!

---

Content captured with git commit from 2023-06-12.

All posts

CC-BY-SA