May 15, 2025

A Detailed Guide to for f in *; do zip -9r "$f.zip" "$f"; done


Working with files and directories on a Unix-like operating system often involves tasks like compressing files to save space or to prepare them for sharing. ZIP archives are one of the most commonly used formats for file compression. In this blog post, we will explore a powerful shell command that can zip multiple files and directories at once with just a single line of code.

The command for f in *; do zip -9r "$f.zip" "$f"; done is a concise yet potent loop used in bash (a Unix shell). This command will create a separate ZIP archive for each file and directory in the current working directory. Let’s break down each component of this command to understand how it works and how you can use it in your daily tasks.

  1. for f in *; do ...; done:
  • for loop: This initiates a loop in bash.
  • f: A variable that represents each item (file or directory) in the current directory.
  • in *: The * wildcard represents all files and directories in the current directory (except hidden ones, i.e., those that start with a dot ., like .hidden).
  • The loop will execute the commands within do and done for each file or directory (f) it encounters.
  1. zip -9r "$f.zip" "$f":
  • zip: The zip command is a widely-used utility for compressing files in Unix-based systems.
  • -9: This option specifies the highest level of compression. The number ranges from 0 (no compression) to 9 (maximum compression).
  • -r: Stands for “recursive,” meaning it includes all files and directories inside the specified directory. If $f is a directory, this option ensures that everything within it is added to the ZIP file.
  • "$f.zip": The name of the output ZIP file. The $f variable is substituted with the current file or directory’s name, and .zip is appended to it.
  • "$f": The target file or directory to compress.

Let’s walk through an example to illustrate how this command operates:

Example Scenario

Suppose you have the following files and directories in your current directory:

file1.txt
file2.docx
folder1/
folder2/

When you run the command for f in *; do zip -9r "$f.zip" "$f"; done, the following sequence of actions occurs:

  1. First Iteration:
  • f is file1.txt:
  • The command executed is zip -9r "file1.txt.zip" "file1.txt".
  • A ZIP file named file1.txt.zip is created, containing the file1.txt file.
  1. Second Iteration:
  • f is file2.docx:
  • The command executed is zip -9r "file2.docx.zip" "file2.docx".
  • A ZIP file named file2.docx.zip is created, containing the file2.docx file.
  1. Third Iteration:
  • f is folder1:
  • The command executed is zip -9r "folder1.zip" "folder1".
  • A ZIP file named folder1.zip is created, containing all the files and subdirectories within folder1.
  1. Fourth Iteration:
  • f is folder2:
  • The command executed is zip -9r "folder2.zip" "folder2".
  • A ZIP file named folder2.zip is created, containing all the files and subdirectories within folder2.

After all iterations, the current directory will have the following files:

file1.txt
file1.txt.zip
file2.docx
file2.docx.zip
folder1/
folder1.zip
folder2/
folder2.zip
  1. Batch Processing: Automates the task of compressing multiple files and directories into separate ZIP archives, saving time and reducing manual effort.
  2. Maximum Compression: The -9 flag ensures that each file or directory is compressed to the maximum extent possible, which is useful when space-saving is critical.
  3. Recursive Compression: The -r option allows for all files and subdirectories within a directory to be included in the ZIP archive, preserving the directory structure.
  4. Automation-Friendly: This command can easily be included in shell scripts to automate repetitive tasks, making it ideal for use in batch processing or automated backups.
  • Hidden Files/Directories: The command does not include hidden files or directories. If you want to include hidden files as well, modify the wildcard to .* * (e.g., for f in .* *; do ...).
  • Overwriting ZIP Files: If a ZIP file with the same name already exists, it will be overwritten without any warning. Be cautious of this, especially when running the command in directories with existing ZIP archives.
  • Efficiency and Resources: The command uses the maximum compression level, which can be CPU-intensive and slow for very large files. Ensure your system has enough resources to handle the compression.

Conclusion

The for f in *; do zip -9r "$f.zip" "$f"; done command is a powerful tool for anyone needing to compress multiple files or directories in Unix-based systems quickly. Whether you are a developer, system administrator, or someone who regularly deals with large volumes of data, mastering this command can help you automate tasks, save space, and streamline workflows. Give it a try next time you need to create ZIP archives, and you’ll see just how efficient and effective it can be.

Tags

About The Author

Leave a Reply

Your email address will not be published. Required fields are marked *