Bird
Raised Fist0
Linux CLIscripting~10 mins

zip and unzip in Linux CLI - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - zip and unzip
Start with files/folders
↓
Use zip command
↓
Create .zip archive
↓
Use unzip command
↓
Extract files from archive
↓
End
This flow shows how files are compressed into a zip archive and then extracted back using unzip.
Execution Sample
Linux CLI
zip archive.zip file1.txt file2.txt
unzip archive.zip
Compress file1.txt and file2.txt into archive.zip, then extract them.
Execution Table
StepCommandActionOutput/Result
1zip archive.zip file1.txt file2.txtCompress files into archive.zip adding: file1.txt (deflated 50%) adding: file2.txt (deflated 45%)
2unzip archive.zipExtract files from archive.zipArchive: archive.zip inflating: file1.txt inflating: file2.txt
3unzip archive.zip -lList contents of archive.zipArchive: archive.zip Length Date Time Name -------- ---- ---- ---- 1024 2024-06-01 10:00 file1.txt 2048 2024-06-01 10:00 file2.txt -------- 3072 2 files
4unzip archive.zip -d extracted_folderExtract files into extracted_folderArchive: archive.zip inflating: extracted_folder/file1.txt inflating: extracted_folder/file2.txt
5zip -r archive.zip folder/Compress folder recursively adding: folder/file1.txt (deflated 50%) adding: folder/file2.txt (deflated 45%)
6unzip archive.zip -tTest integrity of archive.zipNo errors detected in compressed data of archive.zip
7unzip archive.zip -oExtract and overwrite existing filesArchive: archive.zip inflating: file1.txt inflating: file2.txt
8unzip archive.zip -x file2.txtExtract all except file2.txtArchive: archive.zip inflating: file1.txt
9zip archive.zipNo files specifiedzip error: Nothing to do! (try: zip archive.zip -r .)
10unzip nonexist.zipTry to unzip non-existing archiveunzip: cannot find or open nonexist.zip, nonexist.zip.zip or nonexist.zip.ZIP.
11Execution ends as commands complete
💡 Commands stop after all files are compressed or extracted, or on error if archive/file not found.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 4After Step 5Final
archive.zipdoes not existcreated with file1.txt and file2.txtexists with compressed filesexists with compressed filesupdated with folder contentsexists with latest contents
file1.txtexistsunchangedextracted from archiveextracted in extracted_folderunchangedunchanged
file2.txtexistsunchangedextracted from archiveextracted in extracted_folderunchangedunchanged
extracted_folder/does not existdoes not existdoes not existcreated with extracted filesexistsexists
Key Moments - 3 Insights
Why does 'zip archive.zip' without files give an error?
Because zip needs files or folders to compress. The execution_table row 9 shows the error 'Nothing to do!' when no files are specified.
What happens if you unzip a non-existing archive?
The unzip command shows an error that it cannot find the archive, as seen in execution_table row 10.
How do you extract files into a specific folder?
Use 'unzip archive.zip -d foldername' to extract files into that folder, shown in execution_table row 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output of 'zip archive.zip file1.txt file2.txt'?
AExtracting files from archive.zip
Badding: file1.txt (deflated 50%) and adding: file2.txt (deflated 45%)
CNo files to compress error
DCannot find archive.zip
💡 Hint
Check execution_table row 1 under Output/Result.
At which step does the unzip command extract files into a new folder?
AStep 6
BStep 2
CStep 4
DStep 9
💡 Hint
Look at execution_table row 4 for unzip with -d option.
If you run 'unzip archive.zip -x file2.txt', what happens?
AAll files except file2.txt are extracted
Bfile2.txt is extracted only
CNo files are extracted
DError: file2.txt not found
💡 Hint
See execution_table row 8 for unzip with -x option.
Concept Snapshot
zip: compress files/folders into a .zip archive
unzip: extract files from a .zip archive
Use 'zip archive.zip files...' to create archive
Use 'unzip archive.zip' to extract
Options: -d to extract to folder, -l to list, -x to exclude files
Full Transcript
This visual execution shows how to use zip and unzip commands in Linux. First, files are compressed into a zip archive using 'zip archive.zip file1.txt file2.txt'. The output confirms files added with compression rates. Then, 'unzip archive.zip' extracts these files back. You can list contents with 'unzip -l' or extract to a specific folder with 'unzip -d folder'. Errors occur if no files are given to zip or if the archive does not exist. The variable tracker shows the archive and files' existence changing after commands. Key moments clarify common confusions like missing files or extraction locations. The quiz tests understanding of outputs and command options. This helps beginners see step-by-step what happens when compressing and extracting files with zip and unzip.

Practice

(1/5)
1. What does the zip command do in Linux?
easy
A. It extracts files from a compressed archive.
B. It compresses one or more files into a single archive file.
C. It lists the contents of a directory.
D. It deletes files permanently.

Solution

  1. Step 1: Understand the purpose of zip

    The zip command is used to compress files into one archive to save space or organize files.
  2. Step 2: Differentiate from other commands

    unzip extracts files, ls lists files, and rm deletes files, so these are not correct.
  3. Final Answer:

    It compresses one or more files into a single archive file. -> Option B
  4. Quick Check:

    zip = compress files [OK]
Hint: Zip means pack files together into one compressed file [OK]
Common Mistakes:
  • Confusing zip with unzip command
  • Thinking zip lists files
  • Assuming zip deletes files
2. Which of the following is the correct syntax to create a zip archive named archive.zip including all files in the folder docs recursively?
easy
A. zip -r archive.zip docs
B. zip -x archive.zip docs
C. unzip -r archive.zip docs
D. zip archive.zip docs

Solution

  1. Step 1: Identify the option for recursive zip

    The -r option tells zip to include folders and their contents recursively.
  2. Step 2: Check command correctness

    zip -r archive.zip docs correctly creates the archive including all files inside docs. Other options are incorrect or for unzip.
  3. Final Answer:

    zip -r archive.zip docs -> Option A
  4. Quick Check:

    Use -r to zip folders recursively [OK]
Hint: Use -r option with zip to include folders recursively [OK]
Common Mistakes:
  • Using unzip instead of zip to create archive
  • Forgetting -r to include folders
  • Using -x which excludes files
3. What will be the output of the command unzip -l archive.zip?
medium
A. It lists the contents of archive.zip without extracting.
B. It deletes archive.zip after listing contents.
C. It extracts all files from archive.zip to current directory.
D. It compresses files into archive.zip.

Solution

  1. Step 1: Understand the -l option in unzip

    The -l option lists the files inside the zip archive without extracting them.
  2. Step 2: Differentiate from other actions

    Extracting files uses unzip without -l, deleting is not done by unzip, and compressing is done by zip, not unzip.
  3. Final Answer:

    It lists the contents of archive.zip without extracting. -> Option A
  4. Quick Check:

    unzip -l lists archive contents [OK]
Hint: Use unzip -l to peek inside zip without extracting [OK]
Common Mistakes:
  • Thinking unzip -l extracts files
  • Confusing zip and unzip commands
  • Assuming unzip deletes files
4. You run unzip archive.zip but get an error saying "cannot find or open archive.zip". What is the most likely cause?
medium
A. The zip archive is corrupted and cannot be opened.
B. You used the wrong command; unzip cannot extract files.
C. The archive.zip file does not exist in the current directory.
D. You need to use the -r option with unzip to extract.

Solution

  1. Step 1: Analyze the error message

    "cannot find or open archive.zip" means the file is missing or path is wrong.
  2. Step 2: Check command and options

    unzip is correct to extract files; no -r option is needed. Corruption would give a different error.
  3. Final Answer:

    The archive.zip file does not exist in the current directory. -> Option C
  4. Quick Check:

    File missing causes cannot find error [OK]
Hint: Check file exists in current folder before unzip [OK]
Common Mistakes:
  • Assuming unzip command is wrong
  • Using -r option with unzip incorrectly
  • Ignoring file location or name typos
5. You want to create a zip archive named backup.zip that includes all files and folders inside project, but exclude all .log files. Which command will do this correctly?
hard
A. zip backup.zip project/*.log
B. unzip -r backup.zip project -x '*.log'
C. zip -r backup.zip project --exclude '*.log'
D. zip -r backup.zip project -x '*.log'

Solution

  1. Step 1: Use recursive zip with exclusion

    The -r option includes folders recursively, and -x '*.log' excludes all .log files.
  2. Step 2: Validate command correctness

    zip -r backup.zip project -x '*.log' uses correct syntax. zip backup.zip project/*.log only zips .log files, C uses invalid --exclude, and D uses unzip which cannot create archives.
  3. Final Answer:

    zip -r backup.zip project -x '*.log' -> Option D
  4. Quick Check:

    Use -r to recurse and -x to exclude files [OK]
Hint: Use -x '*.ext' to exclude files when zipping recursively [OK]
Common Mistakes:
  • Using unzip instead of zip to create archive
  • Wrong exclude syntax like --exclude
  • Not using -r to include folders
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard