Science and technology

4 methods to make use of the Linux tar command

When you have got plenty of associated recordsdata, it is typically simpler to deal with them as a single object quite than 3 or 20 or 100 distinctive recordsdata. There are fewer clicks concerned, for example, once you e-mail one file in comparison with the mouse work required to e-mail 30 separate recordsdata. This quandary was solved many years in the past when programmers invented a strategy to create an archive, and so the tar command was born (the title stands for tape archive as a result of again then, recordsdata had been saved to magnetic tape.) Today tar stays a helpful strategy to bundle recordsdata collectively, whether or not it is to compress them in order that they take up much less area in your drive, to make it simpler to cope with a lot of recordsdata, or to logically group recordsdata collectively as a comfort.

I requested authors how they used tar, and associated instruments like zip and gzip, of their day by day work. Here’s what they mentioned.

Backups and logs

I exploit tar and zip every time I have to make a backup or archive of a whole listing tree. For instance, delivering a set of recordsdata to a consumer, or simply making a fast backup of my internet root listing earlier than I make a significant change on the web site. If I have to share with others, I create a ZIP archive with zip -9r, the place -9 makes use of very best compression, and -r will recurse into subdirectories. For instance, zip -9r client-dir makes a zipper file of my work, which I can ship to a consumer.

If the backup is only for me, I in all probability use tar as an alternative. When I exploit tar, I normally use gzip to compress, and I do all of it on one command line with tar czf, the place c will create a brand new archive file, z compresses it with gzip, and f units the archive filename. For instance, tar czf web-backup.tar.gz html creates a compressed backup of my html listing.

I even have internet purposes that create log recordsdata. And to maintain them from taking over an excessive amount of area, I compress them utilizing gzip. The gzip command is an effective way to compress a single file. This generally is a TAR archive file, or simply any common file like a log file. To make the gzipped file as small as potential, I compress the file with gzip -9, the place -9 makes use of the very best compression.

The wonderful thing about utilizing gzip to compress recordsdata is that I can use instructions like zcat and zless to view them later, with out having to uncompress them on the disk. So if I wish to take a look at my log file from yesterday, I can use zless yesterday.log.gz and the zless command routinely uncompresses the info with gunzip and ship it to the much less viewer. Recently, I needed to take a look at what number of log entries I had per day, and I ran that with a zcat command like:

for f in *.log.gz; do echo -n "$f,"; zcat $f | wc -l; finished

This generates a comma-separated record of log recordsdata and a line rely, which I can simply import to a spreadsheet for evaluation.

Jim Hall


I launched the zcat command in my article Getting started with the cat command. Maybe this will act as a stimulus for additional dialogue of “in-place” compressed information evaluation.

Alan Formy-Duval

Zless and lzop

I really like having zless to browse log recordsdata and archives. It actually helps scale back the danger of leaving random outdated log recordsdata round that I have never cleaned up.

When coping with compressed archives, tar -zxf and tar -zcf are superior, however do not forget about tar -j for these bzip2 recordsdata, and even tar -J for the extremely compressed xz recordsdata.

If you are coping with a platform with restricted CPU assets, you would even take into account a decrease overhead resolution like lzop. For instance, on the supply laptop:

tar --lzop -cf - source_directory | nc destination-host 9999

On the vacation spot laptop:

nc -l 9999 | tar --lzop -xf -

I’ve typically used that to compress information between methods the place we have now bandwidth limitations and wish a low useful resource possibility.

Steven Ellis


I’ve discovered myself utilizing the KDE software Ark these days. It’s a GUI software, nevertheless it integrates so properly with the Dolphin file supervisor that I’ve gotten into the behavior of simply updating recordsdata straight into an archive with out even bothering to unarchive the entire thing. Of course, you are able to do the identical factor with the tar command, however for those who’re searching by way of recordsdata in Dolphin anyway, Ark makes it fast and simple to work together with an archive with out interrupting your present workflow.

(Seth Kenlon, CC BY-SA 4.0)

Archives used to really feel just a little like a forbidden vault to me. Once I put recordsdata into an archive, they had been nearly as good as forgotten as a result of it simply is not all the time handy to work together with an archive. But Ark helps you to preview recordsdata with out uncompressing them (technically they’re being uncompressed, nevertheless it does not “feel” like they’re as a result of all of it occurs in place), take away a file from an archive, replace recordsdata, rename recordsdata, and much more. It’s a very nice and dynamic strategy to work together with archives, which inspires me to make use of them extra typically.

Seth Kenlon

Most Popular

To Top