Remove Original File After Gzip Compression on Ubuntu
When compressing files in Ubuntu using gzip, managing disk space efficiently is often a priority. This article provides a quick overview of how the gzip utility handles original files by default and confirms the specific command usage required to ensure the source file is removed immediately after compression without extra steps.
Default Behavior of Gzip
By default, the gzip command on Ubuntu automatically
removes the original file after successful compression. You do not need
to add a specific flag to delete the source file because this is the
standard behavior of the utility. When you run the compression command,
the original file is replaced by the new compressed version with a
.gz extension.
The Command
To compress a file and remove the original, simply use the following command in your terminal:
gzip filenameReplace filename with the actual name of the file you
wish to compress. For example, running gzip document.txt
will create document.txt.gz and delete
document.txt.
Keeping the Original File
If you ever need to keep the original file instead of removing it,
you must use the -k flag. This is the only scenario where
the original file remains after compression. The command for keeping the
original file is:
gzip -k filenameIf you have already compressed a file using the -k flag
and wish to remove the original manually, you can use the standard
remove command:
rm filenameSummary
For standard compression tasks where disk space is a concern, the
basic gzip command is sufficient to remove the original
file. No additional options are required to enable this deletion
feature.