Zip with Password

Hi,

In my shell script I am using the zip command to zip the log files. The zip file is password protected. But there is a dicey.

I want the output as:
1) All files should be zipped including sub-directories' files
2) All files (individually) should be Password protected (may be common or different)
3) User should feed the password as input just once.

Condition 1 is met. But in Condition2 the sub-directory files are not password protected.Rather the sub-directory is password protected.

I used

zip -r -9 -m -P password  file_name.zip  *

Here password I handled in variable (condition 3) and without any interference zip command works. I tried using zip command option -e (as zip -r -e ...). But it prompts for password twice which is not recommended. Or is there any way to get the password from user as input and feed the same in run time when it prompts for Password while using -e option?

Please suggest.:confused:

cd /path/to/files
tar cvf /tmp/myarchive.tar .
zip -9 -P password zipfilename.zip /tmp/myarchive.tar

to extract:

unzip -P password zipfilename.zip
cd /path/to/restore/directory
# list the archive to find one file
tar tf /tmp/myarchive.tar | more
# restore one file
tar xvf /tmp/myarchive.tar  [EXACT filename]
# restore the whole archive
tar xvf /tmp/myarchive.tar

Point is: you should be using tar, then compressing.

Thanks for the response, Jim. As you suggested, it worked.
But let know :
1) if we can use -e as option and the password can be fetched from any file and given as input in run time scenario?

Use a here document:

zip -9 -e zipfilename.zip /tmp/myarchive.tar <<EOF
passwordhere
EOF

The last EOF goes in the leftmost column. You can use anything you want instead od EOF - !, PLPL, FOOO

  • anything that is junk as far as the shell you use thinks about things.

Hi Jim,

I tried but still am not meeting with what I want.
Could you let me know where I went wrong ...

Code :
#!/bin/sh
echo "zip file name "
read p0
echo "enter S Path"
read p1
echo " enter D path"
read p2
echo " Enter Password"
read p3
cd $p1
zip -r -9 -e /$p2/$p0.zip <<EOF
$p3
EOF

In Output :
Enter Password
zip
Enter password:
Verify password:

My point is : When I entered the password then why does it prompt again?
Please do help me.:frowning:

Regards,
mkr