Compressing folders with password via command line

155,225

Solution 1

Go to the relevant folder using the cd command like this:

cd /path/to/folder/

(If your folder F is in your Home folder, you can just do cd ~.)

Then, type in your terminal:

zip -er F.zip F

This will prompt you for a password. Give it, and that will create a password-protected zip file from that folder.

  • -e enables encryption for your zip file. This is what makes it ask for the password.
  • -r makes the command recursive, meaning that all the files inside the folder will be added to the zip file.
  • F.zip is the name of the output file.
  • F is the folder you want to zip.

There is an option called -P that will allow you to pass the password in the command itself, but that is not good because there is always the threat of over-the-shoulder peeking. Also other users can see the password by using ps -ef command if you use -P switch. With that -P switch, the command will look like this:

zip -P password -r F.zip F
  • Visit man zip for more information.

Solution 2

The encryption of zip files is weak and can be broken very easily. Instead use 7zip.

7z a -p Fdirectory.7z /path/to/F
  • a command tells 7zip to add files.
  • -p You can either, leave it blank so it asks you interactively or type your password here.
  • Fdirectory.7z is the name of the to-be-created archive.
  • /path/to/F is the path of your directory. It can be relative or full path.

Is recommendable not typing the password in the shell since it's visible to anyone with access to the /proc directory.

Share:
155,225
UnderDog
Author by

UnderDog

Coding for Food

Updated on September 18, 2022

Comments

  • UnderDog
    UnderDog over 1 year

    I would like to know whether it is possible to do the following via CLI.

    I have a Folder F which contains several sub folders and some files. I want to compress folder F into .zip file with the "password-only-extract".

  • dedunumax
    dedunumax over 10 years
    -e means encrypt
  • Alaa Ali
    Alaa Ali over 10 years
    Hey I just edited your question to include a little bit of context. But can you edit it to include your -e means encrypt comment, as well as the meaning of the -r option. Thanks!
  • UnderDog
    UnderDog over 10 years
    Tx Dedunu for your input.Just one more clarification needed.What if I need to automate this ...What I mean is if instead of prompting for a password can I set a password in the command itself ?
  • dedunumax
    dedunumax over 10 years
    I added that also.
  • dedunumax
    dedunumax over 10 years
    But it is higly unrecommended to use because your history will be recorded. otherwise other users can see your password with "ps aux"
  • dedunumax
    dedunumax over 10 years
    Thanks again Alaa! I'm not good at organizing and formatting. :(
  • ssc
    ssc about 9 years
    It seems if you pass -p only to 7z and omit the password, it asks for it interactively - even a second time for confirmation.
  • Braiam
    Braiam about 9 years
    @ssc that is the most useful option, yet it isn't in the manual pages..
  • BringBackCommodore64
    BringBackCommodore64 over 7 years
    I tried the above command to compress a folder with few files totaling ~19kB in size. Yet It takes about 2 minutes! Why it takes so long?
  • Håken Lid
    Håken Lid over 7 years
    @BringBackCommodore64 Don't use the -r flag to recurse. 7z will recurse the source directory without that flag. -r will instead add all files and subdirectories of the current working directory. The man page for 7z specifically warns against this and says that this flag should be avoided. linux.die.net/man/1/7z
  • BringBackCommodore64
    BringBackCommodore64 over 7 years
    @HåkenLid Nice catch! I did used the -r flag even if I did not mention it above. That was it! Thank you!
  • rubo77
    rubo77 over 6 years
    Attention: Zip passwords can be easily broken. use 7-Zip with a long password instead (see answer below), or better GNUPG encryption!
  • rubo77
    rubo77 over 6 years
    You should use at least a very long password, that is brute-force-safe. But It seems like 7-Zip has also been cracked by now, so if it has to be secure, use GNUPG to encrypt your files
  • Braiam
    Braiam over 6 years
    @rubo77 any format is liable to bruteforcing, but 7zip algo isn't easily breakable as shown by the very answer where >12 characters password might be impossible.
  • Jinhua Wang
    Jinhua Wang over 5 years
    @rubo77 The link you shown doesn't crack 7zip. It is just bruteforce guessing the password.