为什么在 stdin 上压缩文件会产生比作为参数给出的相同文件更小的输出?

Mic*_*alH 13 gzip

当我做:

# gzip -c foo > foo1.gz 
# gzip < foo > foo2.gz
Run Code Online (Sandbox Code Playgroud)

为什么foo2.gz最终的尺寸小于foo1.gz

Bra*_*ley 19

因为它正在保存文件名和时间戳,以便在您稍后解压缩后尝试恢复它们。由于在您的第二个示例中foo提供给gzipvia <stdin>,因此它无法存储文件名和时间戳信息。

从联机帮助页:

   -n --no-name
          When compressing, do not save the original file name and time stamp by default. (The original name is always saved if the name had
          to  be truncated.) When decompressing, do not restore the original file name if present (remove only the gzip suffix from the com-
          pressed file name) and do not restore the original time stamp if present (copy it from the compressed file). This  option  is  the
          default when decompressing.

   -N --name
          When compressing, always save the original file name and time stamp; this is the default. When decompressing, restore the original
          file name and time stamp if present. This option is useful on systems which have a limit on file name  length  or  when  the  time
          stamp has been lost after a file transfer.
Run Code Online (Sandbox Code Playgroud)

我在这里重新创建了这个问题:

[root@xxx601 ~]# cat /etc/fstab > file.txt
[root@xxx601 ~]# gzip < file.txt > file.txt.gz
[root@xxx601 ~]# gzip -c file.txt > file2.txt.gz
[root@xxx601 ~]# ll -h file*
-rw-r--r--. 1 root root  465 May 17 19:35 file2.txt.gz
-rw-r--r--. 1 root root 1.2K May 17 19:34 file.txt
-rw-r--r--. 1 root root  456 May 17 19:34 file.txt.gz
Run Code Online (Sandbox Code Playgroud)

在我的例子中,file.txt.gz相当于你的foo2.gz. 使用-n选项禁用此行为,当它,否则有机会获得信息:

[root@xxx601 ~]# gzip -nc file.txt > file3.txt.gz
[root@xxx601 ~]# ll -h file*
-rw-r--r--. 1 root root  465 May 17 19:35 file2.txt.gz
-rw-r--r--. 1 root root  456 May 17 19:43 file3.txt.gz
-rw-r--r--. 1 root root 1.2K May 17 19:34 file.txt
-rw-r--r--. 1 root root  456 May 17 19:34 file.txt.gz
Run Code Online (Sandbox Code Playgroud)

正如你可以在上面看到,文件尺寸file.txtfile3.txt比赛,因为他们现在都遗漏的名字和日期。