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:
- The first argument is the directory you want to begin in,
. - Then we will restrict it to find files only (
-type f) - The
-execdiroption allows us to run a command on each file found, executing it from the file's directory - This command is evaluated as
zip file.txt.zip file.txt, for example, since all occurrences of{}are replaced with the actual file name. This command needs to be ended with\;
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.
No comments to display
No comments to display