The filesystem and archives

INPUT · Slides

Shrink it (`gzip`)

01 / 06

Writing the same contents more briefly

Last lesson you found that tar does not shrink anything. This is the one that does.

~ $ gzip origin.txt

The mechanism is very plain. It finds the repeats and puts a short mark in their place.

hello hello hello hello ... (2000 times)  |write "hello, 2000 times"

Measured for real, 12000 bytes becomes 61 bytes. A two-hundredth.

Of course it only shrinks that much when there is a lot of repetition.

text and logs with the same words over and over  -> shrinks wellphotos, video, already-compressed things         -> hardly at all

Photos (JPEG) and video are already compressed, so gzip makes them slightly bigger. Bigger because the table of marks has to go in as well, or it could not be put back.

So compression is not magic. It depends on what the contents are like.

02 / 06

The original file disappears

Here is the most surprising thing about gzip.

~ $ lsorigin.txt~ $ gzip origin.txt~ $ lsorigin.txt.gz        <- origin.txt has gone!

It replaces the original file. It does not make a copy the way cp does.

To put it back there is gunzip.

~ $ gunzip origin.txt.gz~ $ lsorigin.txt           <- back (and the .gz has gone)

Both are replacements, so you just go back and forth.

When you want to keep the original, add -k (keep).

~ $ gzip -k origin.txt~ $ lsorigin.txt   origin.txt.gz     <- both there

Three ways of writing it are enough.

What you typeWhat it does
gzip nameshrink it (the original goes)
gunzip name.gzput it back
gzip -d name.gzput it back (the same as gunzip)

gunzip and gzip -d are exactly the same thing: one program called by two names. The same as busybox having many names, from earlier in the chapter.

03 / 06

Read it unopened with zcat

Uncompressing something every time you want to read it is a bother, so there is a tool.

~ $ zcat access.log.gzok /index.htmlerror /missing.html...

A relative of cat. It gives the contents while they stay compressed, and the file stays as .gz.

Being on the way out, it naturally goes into a pipe.

~ $ zcat access.log.gz | grep error | wc -l3

This is the shape you use most in practice. Old logs on a server are nearly always kept compressed.

/var/log/messages         today's log/var/log/messages.1.gz    yesterday's (compressed)/var/log/messages.2.gz    the one before

So an investigation looks like this.

zcat /var/log/messages.*.gz | grep error

With -c you can send gzip itself to the way out.

gzip -c origin.txt > copy.gz     keep the original, compress under another name

-c is used in many commands to mean "to the way out, not to a file". Worth knowing.

04 / 06

What .tar.gz really is

That familiar name should be readable to you now.

docs.tar.gz     ^   ^     |   \- shrunk with gzip     \----- packed with tar

The name records the two tools it went through, in order. So making one takes two moves.

~ $ tar cf docs.tar docs    pack it~ $ gzip docs.tar           shrink it~ $ lsdocs.tar.gz

Undoing it goes the other way round.

~ $ gunzip docs.tar.gz      unwrap it~ $ tar xf docs.tar         open it

The order reverses in the same way that taking clothes off reverses putting them on.

To do it in one move, join them with a pipe.

~ $ gunzip -c docs.tar.gz | tar tf -

The - at the end is the mark for "not a file, take it from the way in". Something like it came up when you learned xargs in chapter 4.

The tar here has no z mark, so splitting it is the only way. On a real Linux this is one command.

tar czf docs.tar.gz docs    make ittar tzf docs.tar.gz         see ittar xzf docs.tar.gz         extract it

05 / 06

Check it is not broken

With a compressed file there is this worry.

the transfer was cut off partwaythe copy stopped halfway

You would not notice until you tried to look inside, so there is a mark for checking.

~ $ gzip -t docs.tar.gz~ $ echo $?0                    <- all well

Broken, it goes like this.

~ $ gzip -t fake.gzgzip: invalid magic~ $ echo $?1                    <- no good

The -t is the t of test. Finishing without a word means it is fine, which is the command line's manner. "Quiet means success", from chapter 4.

The magic in invalid magic is the mark at the head of the file saying "I am a gzip".

read the first few bytes and you know the kind

The file command in chapter 2 guessed the kind from the contents by reading exactly this mark. The extension is decoration for people; the real kind is written inside.

So text merely renamed to .gz is seen through by gzip at once.

06 / 06

Now have a go

Here are the shapes for the lesson.

gzip name               shrink it (the original goes)gzip -k name            shrink it (keep the original)gzip -c name > other.gz to the way out, saved under another namegunzip name.gz          put it backgzip -d name.gz         put it back (the same thing)zcat name.gz            give the contents while compressedgzip -t name.gz         check it is not broken

You also use the trick for making a biggish file to practise on.

~ $ yes hello | head -2000 > origin.txt

yes is the command that gives out the same word for ever. You used it in place of "heavy work" in chapter 8. Here head -2000 cuts off two thousand lines of it.

Being nothing but repetition, it is material that shrinks well. You can make material that does not, too.

~ $ head -c 12000 /dev/urandom > ran.bin

/dev/urandom is the device that gives out random values, a sibling of /dev/zero. With no repeats in it, it does not shrink. Measure both and see the difference with your own eyes.