Thursday, March 16, 2017

Compress multiple files with 7zip command line

Just a quick shell command to use 7zip to individually compress multiple files using the same password. Wow, that was a mouthful.

From windows shell:
for f in [files]; do 7z a -tzip -mem=AES256 -mx=9 -p[password] "/[destination directory]/`basename $f`.zip" "$f"; done;

Example:
for f in /export/home/me/*.txt; do 7z a -tzip -mem=AES256 -mx=9 -pPa55w0rd! "/export/home/me/zips/`basename $f`.zip" "$f"; done;

So, in this example, a new password protected zip file will be created for every text file in /export/home/me. The new zip file will have the same name as before, just with a .zip extension instead of a .txt extension. So "/export/home/me/a.txt" becomes "/export/home/me/zips/a.zip". The password will be "Pa55w0rd!". Just remove the obvious options if you don't want your archive to be password protected.

No comments: