0
0
Linux CLIscripting~10 mins

gzip and gunzip in Linux CLI - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - gzip and gunzip
Start with file
Run gzip
File compressed to .gz
Run gunzip
File decompressed back
End
This flow shows compressing a file with gzip and then decompressing it with gunzip.
Execution Sample
Linux CLI
echo "Hello World" > file.txt
gzip file.txt
ls
gunzip file.txt.gz
ls
Create a text file, compress it with gzip, list files, decompress with gunzip, then list files again.
Execution Table
StepCommandActionFile StateOutput
1echo "Hello World" > file.txtCreate file.txt with textfile.txt exists
2gzip file.txtCompress file.txt to file.txt.gz and remove originalfile.txt.gz exists, file.txt removed
3lsList filesfile.txt.gz existsfile.txt.gz
4gunzip file.txt.gzDecompress file.txt.gz to file.txt and remove .gzfile.txt exists, file.txt.gz removed
5lsList filesfile.txt existsfile.txt
💡 All commands executed successfully; file compressed and decompressed as expected.
Variable Tracker
FileInitialAfter gzipAfter gunzip
file.txtexistsremovedexists
file.txt.gzdoes not existexistsremoved
Key Moments - 2 Insights
Why does file.txt disappear after running gzip?
gzip compresses the file and replaces the original with the compressed .gz file, so file.txt is removed as shown in step 2 of the execution_table.
What happens if you run gunzip on a file that is not compressed?
gunzip expects a .gz file; if the file is not compressed or missing .gz extension, it will give an error and not decompress, unlike step 4 where file.txt.gz exists.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, after which step does file.txt.gz first appear?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
Check the 'File State' column for file.txt.gz presence after each step.
At which step does file.txt reappear after being compressed?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Look at the 'File State' column to see when file.txt exists again.
If you skip the gunzip command, what will the final ls output show?
Afile.txt only
Bboth file.txt and file.txt.gz
Cfile.txt.gz only
Dno files
💡 Hint
Refer to step 3 where gzip has compressed the file but gunzip has not run yet.
Concept Snapshot
gzip compresses files by replacing the original with a .gz file.
gunzip decompresses .gz files back to original and removes the .gz.
Use gzip filename to compress and gunzip filename.gz to decompress.
After gzip, original file is gone; after gunzip, original file returns.
Check files with ls to see changes.
Full Transcript
This lesson shows how gzip compresses a file by creating a .gz file and removing the original. Then gunzip decompresses the .gz file back to the original file and removes the compressed version. We start by creating a file with echo, compress it with gzip, list files to see the .gz file, then decompress with gunzip and list files again to see the original file restored. gzip replaces the original file with the compressed one, so the original disappears after compression. gunzip reverses this. If you try to gunzip a file that is not compressed, it will error. This visual trace helps understand the file state changes step-by-step.