Gzip
缩写为GUN zip,它是Linux中经常使用的命令之一。
gzip
命令用于压缩文件以减少文件的大小。
如果文件在不同的系统之间传输,这将节省带宽。此外,减少的大小取决于文件的内容,如果内容是文本,它将减少60%,对于图像,它应该是80%。
如果我们想复制多个文件,文件应该被压缩,这样文件的带宽就会减少。
Gzip命令的例子
让我们看看gzip命令的不同例子
如何在压缩文件的同时删除原始文件
这将用
linuxFileName.gz
替换一个 linuxFileName,该文件在当前目录中的大小减少到 80%。通过这个命令,文件名的大小被减少了。一旦gz文件被创建,linuxFileName应该被删除。
压缩文件并保留原始文件
这个命令的行为和上面的命令一样,希望原始文件不被删除。所以原始文件应该保持原样。你将在当前目录下有两个文件linuxFileName和linuxFileName.gz。
解压缩/解压gz文件
这将在使用gzip命令之前解压filename.gz并获得原始文件。
压缩一个目录中的多个文件
Gzip -r directoryname
```using -r option, recursively traverse all the files, meaning all the files in the current directory including all the files subdirectory and create a directoryname.gz which contains all the files in the current directory and subdirectory
After compression, the total size of the files is approximately 20% less gz file.
## Uncompress/decompress the gz file into multiple files
Gunzip -r fileName.gz
## Compression files fastly
Gzip -1 filename.txt Gzip -fast filename.txt
Gzip -9 filename.txt Gzip -best filename.txt
The both above options compress filename.txt files slowly and create filename.txt.gz folder
**Advanced gzip examples**
## zip each file in the current directory and creating separate gz
for filename in *.txt; do gzip -c "
let us say we have file1.txt,file2.txt,file3.txt in the current directory /tmp/directory. To do this, we have to iterate each file and do the gzip command redirect(>) the output as gz file
The above command create file1.txt.gz,file2.txt.gz,file3.txt.gz
\-c option keep all the original files (file1.txt,file2.txt,file3.txt) and give the file to stdout console.
if we don't specified any option, it will remove all the files, and create a gz file
Hope you got basic start for gzip with examples.
Please feel free to comment and if you have any questions, leave a comment, i would get back to you.
复制代码