Reputation: 1845
Can you use gzip to just compress text that you type into the terminal?
If yes, then how?
If no, then what does this line mean?:
When you do not specify any files to be compressed or specify - as a file name, gzip reads from the standard input, compresses what is read, and writes the result out to the standard output.
I've read it on many web pages, including this one: http://www.mkssoftware.com/docs/man1/gzip.1.asp
Also, when I type gzip -f
in my terminal, cursor goes to newline and starts accepting input but I just have to Ctrl+C it out and then nothing happens.
Upvotes: 0
Views: 1206
Reputation: 263497
Yes, you can, but it's not a common thing to do.
For example, this is perfectly legal (I typed "Hello, world.
", then Enter, then Ctrl-D).
$ gzip > input.gz
Hello, world.
$ ls -l input.gz
-rw-r--r-- 1 kst kst 34 Oct 5 23:18 input.gz
$ gzip -d < input.gz
Hello, world.
(The compressed file is actually bigger than the input; this is common for very small inputs.)
But remember that standard input doesn't have to come from the keyboard. For example, the du
command prints a summary of disk usage, and can generate a lot of output if you run it on a large directory. If you want to save the output directly in compressed form, you might type:
du -a / | gzip > du-a.out.gz
The standard input of the gzip
command comes from the output of the du -a /
command; its standard output is written to the new file du-a.out.gz
.
The tar
command is another common example:
tar cf - some_dir | gzip > some_dir.tar.gz
(but many versions of tar
have options to do this without having to explicitly invoke the gzip
command).
As for the last part of your question:
Also, when I type gzip -f in my terminal, cursor goes to newline and starts accepting input but I just have to Ctrl+C it out and then nothing happens.
Typing Ctrl-C kills the gzip
process, and may not allow it to finish writing its output. You need to type Ctrl-D to trigger an end-of-file condition; this lets the gzip -f
command finish normally. But you don't generally want to do that; it will write the compressed output to your terminal, which can include control characters that can mess up your terminal settings.
Upvotes: 2