How To: Compress Each Folder/Directory to Separate Archives in Linux
r3dux | June 27, 2009Lets say you’ve got a bunch of folders taking up a large swathe of space which you never really use but want to keep, just not taking up stacks of your NAS… How can you easily compress them all up to individual archives of each folder? Dead easy:
1 2 3 4 5 | #!/bin/bash for folder in */ do 7z a -mx9 -mmt "${folder%/}.7z" "$folder" done |
Save that to a file, chmod +x it and run in the location you want to compress the folders. Every folder (and all contents within) will be compressed to its own foldername.7z archive.
With 7z, -mx9 is the flag for maximum compression, and -mmt says to use multiple CPUs to speed up compression, so omit that part if you’re on a single core machine.











Great example and very simple. However I have one question. Im trying to create the folder archives in a different folder e.g.
All folders in
/backup/live/*
are archived and sent to:
/backup/archive
im sure this would be quite simple but im not sure how to do this.
Thanks
try:
=D
Thanks that seems so much simpler than what i came up with.
Well, yours seems pretty comprehensive with the date and all that – good job, and thanks for sharing.
Bash scripting is such fiddly stuff