Reputation: 1193
So I want to create a script that enables me to compresses each folder into its own zip archive and not into a one big zip file.
As an example, I've got a directory:
+ MyDirectory/
| |
| + Folder_01/
| |
| + Folder_02/
| |
| + Folder_03/
|
When I'm done running the script under MyDirectory
then I would have a zip file of each Folder which is inside MyDirectory
: Folder_01.zip
, Folder_02.zip
and Folder_03.zip
.
I know some BASH but this is something I can't figure out.
How can this be done?
Kind regards.
Upvotes: 46
Views: 33802
Reputation: 29
I personally use
fd . -t d -x zip -r '{}.zip' '{}'
(fd
might be fd-find
depending on your OS)
Upvotes: 0
Reputation: 2530
I take the old solutions and get back with another good solution that works for any directory in any place in you drive
This code in Bash
#!/bin/bash
folderPath=/home/ahmed/test/
for folder in $folderPath*
do
folderName=$(basename $folder)
cd $folderPath && zip -r $folderName.zip $folderName
done
So all folders inside test folder will be zipped in the same folder
Upvotes: 0
Reputation: 73738
Consider using xz
algorithm if you require smaller output (at the cost of longer process):
for d in */ ; do
outName=$d;
outName=${outName// /\-};
outName=${outName//[!0-9a-z-]};
dirName=$d;
dirName=${dirName//\/}
tar -c "$dirName" | xz -e > $outName.tar.xz
done
This code will sanitise folder names and produce .tar.xz
for every folder in the current directory.
Upvotes: 1
Reputation: 64563
for i in *
do
[ -d "$i" ] && zip -r "$i.zip" "$i"
done
You walk through all the directories and create zip for each of them.
Or even more concise:
for i in */; do zip -r "${i%/}.zip" "$i"; done
(thanks to damienfrancois for suggestion).
Upvotes: 82