7z add all files to a password protected archive and process files individually

dvdom

New Member
Joined
Mar 1, 2022
Messages
8
Reaction score
0
Credits
77
Hello all,
I've been playing around with different ways to accomplish this with no avail...

Files to archive: home/me/Desktop/to_archive/
Destination for archives: home/me/Desktop/archived/

normal 7z command to archive all files into 1 archive:
7z a -pMyPassword -mhe home/me/Desktop/archived/ home/me/Desktop/to_archive/

I need to get the files in to_archive to be recusively archived with a password, to the archived directory.
Can any of you guys think of a way to accomplish this?
Much thanks in advance!
 


The main problem that I can see is - you haven't specified a filename for your .7z file.

Also, for the -mhe variable (used for enccrypting the header) - you haven't specified whether it is to be turned on or off. According to the man page for 7z, the default is off.

So your command should look more like this:
Bash:
7z a -pMyPassword -mhe=on /home/me/Desktop/archived/archive.7z /home/me/Desktop/to_archive
Above will create a password protected .7z file called archive.7z in your ~/Desktop/archived/ directory, containing whatever is in the ~/Desktop/to_archive/ directory.

Or perhaps, if you're going to regularly back up the contents of the to_archive directory, you might want to include a time-stamp on your backup 7z's.
e.g.
Bash:
7z a -pMyPassword -mhe=on /home/me/Desktop/archived/archive-"$(date +%F)".7z /home/me/Desktop/to_archive
The above will create a password protected file called archive-YYYY-MM-DD.7z (where YYYY-MM-DD is today's date) in the ~/Desktop/archived/ directory.

If you want to use a different time-stamp format for the file-name, you can supply different options to the date command.
See man date for full details about that.
But be sure to carefully set the format of the timestamp, to avoid outputting any special characters that should not be used in file-names. Otherwise 7z will not be able to create the file.
 
The main problem that I can see is - you haven't specified a filename for your .7z file.

Also, for the -mhe variable (used for enccrypting the header) - you haven't specified whether it is to be turned on or off. According to the man page for 7z, the default is off.

So your command should look more like this:
Bash:
7z a -pMyPassword -mhe=on /home/me/Desktop/archived/archive.7z /home/me/Desktop/to_archive
Above will create a password protected .7z file called archive.7z in your ~/Desktop/archived/ directory, containing whatever is in the ~/Desktop/to_archive/ directory.

Or perhaps, if you're going to regularly back up the contents of the to_archive directory, you might want to include a time-stamp on your backup 7z's.
e.g.
Bash:
7z a -pMyPassword -mhe=on /home/me/Desktop/archived/archive-"$(date +%F)".7z /home/me/Desktop/to_archive
The above will create a password protected file called archive-YYYY-MM-DD.7z (where YYYY-MM-DD is today's date) in the ~/Desktop/archived/ directory.

If you want to use a different time-stamp format for the file-name, you can supply different options to the date command.
See man date for full details about that.
But be sure to carefully set the format of the timestamp, to avoid outputting any special characters that should not be used in file-names. Otherwise 7z will not be able to create the file.
I appreciate your input. By not specifying a file name, The compressed file takes the name of the file it is compressing. Also, Even without specifying on for -mhe It works perfectly fine.
My issue is not the structure of the 7Z format, The issue is how to compress these files into their own archive.
 
My issue is not the structure of the 7Z format, The issue is how to compress these files into their own archive.
OK, sorry - I thought you were having problems getting 7z to produce an archive.

In that case - assuming that the files are all in ~/Desktop/to_backup/ - you'd need to run 7z on each of the files you want to compress.

To do that in a bash one-liner, you'd use a for loop:
Bash:
for file in ~/Desktop/to_backup/*; do 7z a -pMyPassword -mhe ~/Desktop/backup/"$(basename $file)".7z "$file"; done

So if you had 10 files inside ~/Desktop/to_backup/ called file1 .. file10, then running the above command would put 10 compressed files into ~/Desktop/backup/ called file1.7z .. file10.7z.

Is that the result you're looking for?!
 
Last edited:
JasKinasis, I will give that a try for sure. I'll let you know. Thank you
 
OK, sorry - I thought you were having problems getting 7z to produce an archive.

In that case - assuming that the files are all in ~/Desktop/to_backup/ - you'd need to run 7z on each of the files you want to compress.

To do that in a bash one-liner, you'd use a for loop:
Bash:
for file in ~/Desktop/to_backup/*; do 7z a -pMyPassword -mhe ~/Desktop/backup/"$(basename $file)".7z "$file"; done

So if you had 10 files inside ~/Desktop/to_backup/ called file1 .. file10, then running the above command would put 10 compressed files into ~/Desktop/backup/
called file1.7z .. file10.7z.

Is that the result you're looking for?!
That works! Now, last question. Is there a not too difficult way to duplicate the directory structure as well?
For example if the "to_backup" directory contains 10 directories with files under each directory, can I have the loop duplicate those directories so that the archives can be easily located in the original directory structure? I hope that makes sense..
 

Members online


Top