How To Use zip command in Linux

How To Use zip command in Linux






Zip packs files. Saves disk space. Makes sharing easy. The command works on Windows, Linux, Mac.

Shrink large files for email. Mix files for backups. Cut file sizes. Add pass with flags.

Example: unzip Command

Pull files from ZIP. Pulls all files to current dir.

Syntax:

unzip file_name.zip

Pull files from a pack:

$ unzip case.zip

Use ls.

Syntax

zip [flags] archive_name.zip file1 file2 dir/
  • zip: Make packed file
  • archive_name.zip: Name for your zip file
  • file1 file2: Files and dirs to pack

Main zip Options

1) -d Option (Drop File)

Drop a file from a zip pack. No need to open it first.

Syntax:

zip -d file_name.zip files_name

Drop a file from a pack with eight files:

Run:

$ zip -d myfile.zip hello7.c
deleting: hello7.c
Note: Use sudo if you see access errors.

Check by opening the pack. Use ls to list files.

2) -u Option (Add Files)

Add new files to a pack. Swap old files with new ones.

Syntax:

zip -u file_name.zip files_name

Add a new file to a pack:

Run:

$ zip -u myfile.zip hello9.c
adding: hello9.c (packed 60%)

Use vi to check it was put in.

3) -m Option (Move Files to the Pack)

Move files to a zip pack. Files are dropped from their old spot.

Syntax:

zip -m file_name.zip files_name

Move all C files to a pack:

Run:

$ zip -m myfile.zip *.c
adding: hello1.c (packed 60%)
adding: hello2.c (packed 60%)
Warning: Files are dropped after moving to the pack.

Use ls to check.

4) -r Option (Pack a Whole Folder)

Pack a dir. All subdirs go in.

Syntax:

zip -r file_name.zip directory_name

Pack a dir with all files:

Run:

$ zip -r myfile.zip project_folder/
adding: project_folder/ (stored 0%)
adding: project_folder/file1.c (packed 60%)
adding: project_folder/subfolder/ (stored 0%)

Check pack items with vi or unzip -l.

5) -x Option (Skip Files)

Skip files when making a pack.

Syntax:

zip -r file_name.zip . -x files_name

Pack all files but skip some:

Run:

$ zip -r myfile.zip . -x a.txt
adding: file1.txt (packed 60%)
adding: file2.txt (packed 60%)

Use -r flag adds files from all subdirs. The dot means this dir. Use -x flag skips the file named.

6) -v Option (Show Info)

Show info about file size.

Syntax:

zip -v file_name.zip file_name

View info about all C files:

Run:

$ zip -v myfile.zip *.c
adding: hello1.c (in=245) (out=128) (packed 60%)
adding: hello2.c (in=312) (out=156) (packed 50%)

Output shows file sizes before and after.

Zip Command Options

Use these flags to change how zip works. Each flag helps:

Option What It Does Example Command
-d Drop a file from zip pack zip -d myfiles.zip notes.txt
-u Change the zip file with new files zip -u myfiles.zip report.txt
-m Move files to zip and drop old ones zip -m myfiles.zip backup.txt
-r Pack a dirs and all items zip -r myproject.zip myfolder/
-x Skip files or dirs when packing zip -r myfiles.zip * -x *.log
-v Show info while packing zip -v myfiles.zip sample.txt

Zip in Linux lets you pack and shrink files. Use -r to pack dirs. Use -x to skip files. Use -u to add new files. Use -d to drop files from packs. Use -m to move files. Use -v flag shows you what goes. Use unzip to get files.

Tip: You can mix flags. Try zip -rv myarchive.zip dir/ to pack dirs and see info at once.

Common Querys