Compressing files using the zip command on Linux

$ zip -q bin bin/*
$

If you are zipping a directory that contains subdirectories, those subdirectories, but not their contents will be added to the zip file unless you add the -r (recursive). Here’s an example:

$ zip bin bin/*
updating: bin/FindFiles (deflated 54%)
updating: bin/shapes (deflated 63%)
updating: bin/shapes2 (deflated 62%)
updating: bin/shapes3 (deflated 45%)
updating: bin/NOTES/ (stored 0%)

Here’s an example that adds -r and, as a result, includes the NOTES subdirectory’s files in the bin.zip file is it creating.

$ zip -r bin bin/*
updating: bin/FindFiles (deflated 54%)
updating: bin/shapes (deflated 63%)
updating: bin/shapes2 (deflated 62%)
updating: bin/shapes3 (deflated 45%)
updating: bin/NOTES/ (stored 0%)
  adding: bin/NOTES/finding_files (deflated 5%)
  adding: bin/NOTES/shapes_scripts (deflated 35%)

Using encryption passwords

To add a password that will need to be used to extract the contents of a zip file, use a command like the one shown below. Notice that it prompts twice for the password, though it does not display it.

$ zip -e -r bin bin/*
Enter password:
Verify password:
updating: bin/FindFiles (deflated 54%)
updating: bin/shapes (deflated 63%)
updating: bin/shapes2 (deflated 62%)
updating: bin/shapes3 (deflated 45%)
updating: bin/NOTES/ (stored 0%)
updating: bin/NOTES/finding_files (deflated 5%)
updating: bin/NOTES/shapes_scripts (deflated 35%)

Extracting file from a zip file

To extract the contents of a zip file, you would use the unzip command. Notice that, because the zip file below was encrypted with a password, that password needs to be supplied to extract the contents.

$ unzip bin.zip
Archive:  bin.zip
[bin.zip] bin/FindFiles password:
  inflating: bin/FindFiles
  inflating: bin/shapes
  inflating: bin/shapes2
  inflating: bin/shapes3
   creating: bin/NOTES/
  inflating: bin/NOTES/finding_files
  inflating: bin/NOTES/shapes_scripts

If you want to extract the contents of a zip file to a different directory, you don’t need to cd to that directory first. Instead, you can simply add the -d option followed by the target directory to specify the new location.

$ unzip bin.zip -d /tmp
Archive:  bin.zip
[bin.zip] bin/FindFiles password:
  inflating: /tmp/bin/FindFiles
  inflating: /tmp/bin/shapes
  inflating: /tmp/bin/shapes2
  inflating: /tmp/bin/shapes3
   creating: /tmp/bin/NOTES/
  inflating: /tmp/bin/NOTES/finding_files
  inflating: /tmp/bin/NOTES/shapes_scripts

You can extract a single file from a zip file if you specify its name as listed in the zip file. Here’s an example command where the original file (maybe it’s been damaged in some way) is replaced after confirming that this is what you want.



Source link