Zip All Files Into Each Individual Zip File

The solution is pretty easy. If you want to do this for every file, recursively, use find. It will list all files and directories, descending into subdirectories too.

All Subdirectories:

find . -type f -execdir zip '{}.zip' '{}' \;

Explanation:


Of course, find has more options. If instead you just want to stay in your current directory, not descending into subdirectories:

Current Directory Only:

find . -type f -maxdepth 1 -execdir zip '{}.zip' '{}' \;

If you want to restrict it to certain file types, use the -name option (or -iname for case-insensitive matching):

Restrict To Specific Filetypes:

find . -type f -name "*.txt" …

Anything else (including looping with for over the output of ls *) is pretty ugly syntax in my opinion and likely to break, e.g. on files with spaces in their name or due to too many arguments.


Revision #2
Created 2024-05-30 05:04:33 UTC by Ryan
Updated 2025-02-12 01:11:07 UTC by Ryan