My Cheat sheet for the Tar Command
I often make a lot of little cheat sheets that I often use as a quick reminder of the tools I use often enough that they can find there way into my daily workflow, but sometimes I need a little refresher.
It’s not uncommon that I’m tasked to work on a server that is kinda bare-bones. Running Linux locally means you can choose what terminal software works best for you but when given a random CentOS or AlmaLinux box you might be left with only legacy tools. I’ve decided it would be a smart move for me to get to know these tools and I decided that tar
would be the tool I learn to love.
Tape Archive
tar
is an old Unix command and was originally used for archiving on to magnetic tape. It’s easy to forget that some of these tools where designed in the 60s and 70s.
tar cf archive.tar directory
This will create a tar archive called archive.tar that contains the contents of the directory directory. The “c” option tells tar to create a new archive, and the “f” option specifies the name of the archive file.
tar xf archive.tar
This will spit out all the contents of the archive.tar into the current directory. The “x” option tells tar to extract the contents of the archive, and again the “f” option specifies the name of the archive file.
Compress that tar
There are a few compression algorithms you can use to help make the .tar file more manageable.
- gzip
- bzip2
- xz
These are commands that you can already run on a single file to compress but they work best with the tar
command.
The tar
command can be used in conjunction with any of the above compression utilities to create a compressed tar archive like so:
tar czf archive.tar.xz directory
On most modern versions of tar
you only need to specify the extension .tar.gz, .tar.bz2, or .tar.xz.
What’s in the box?
Probably the command that saves me the most amount of time is “t”. This simply displays the contents of a tar.
tar tf archive.tar.xz
Because of the quickness of the tar
application it’s usually fairly instant. Neato.