0

I'm kind of a beginner in Linux and I'm having a hard time. On Ubuntu 19.10, I made a small script for 7zip to unzip multiple files with password:

FILES=/mnt/sdx
for f in *.7z;
do 7za x -oUNZIPPED $f -ppassowrd;
done

It works perfectly for one password, but the point is, if I download an older file from the site that has the compressed files it uses another password so the program doesn't open, then I have to make another script for another password to work.

What I would like to know is if you can't integrate these old passwords into the script, like if he tries with one password and the file doesn't unzip, it tries with another password until the file sucsessfully unzip or until the available passwords are over.

Another thing, some older files are in .rar. How can I expand the script search for these two files?


Update: It finaly Worked!!! Thanks guys for the help and the patience the Final Script Looks Like This:

#!/bin/bash

for file in *.{7z,zip,rar}
do
  for password in Pass1 Pass2 Pass3
  do
    7z x -oOUTPUTDIR -p"${password}" "${file}" && break
  done
done

I took the code that you gave me and add a minor modification, Thanks Again!!

7
  • i saw this comand line: "for Z_FILE in *.zip; do for PASSWD in [ pass123, PASS123, abc123, ABC123 ]; do unzip -P $PASSWD $Z_FILE; if [ $? = 0 ]; then # successful unzip break fi done done" In a post with folows this link: cyberciti.biz/faq/linux-unix-shell-unzipping-many-zip-files But i don't know hot to adapt this to mys script. Nov 5, 2019 at 20:40
  • You want to do the following: an outer for-loop for all files, an inner for-loop for all passwords. break the inner loop, if the extraction succeeds: 7za x -oDIR -p"${passw}" "${file}" && break. a && b = if successful termination a-command, do b-command. Please also try to edit your question to make your code readable. Markdown help
    – FelixJN
    Nov 5, 2019 at 21:56
  • sorry, can you be more clear? I couldn't understand I dont know how to make more readble, i'm new in the forum. I can try to take a screenshot if helps Nov 5, 2019 at 23:13
  • It is a bit hard to understand what you have achieved so far and what your reference source means. Please format your question accordingly, otherwise it cannot be answered. My hints for your goal: Nested do-loops (outer runs though filenames, inner through passwords), the inner loop will break (this is a bash command - look it up!) when a password successfully is used to unzip a file (so that the other passwords will not be tried on that file and the next file will be tried). Check the following bash commands and operators: for ... do ; done (nested), break, &&, ||.
    – FelixJN
    Nov 5, 2019 at 23:19
  • At the current Time i Manage to make this: FILES=/mnt/sdax <br/> for f in * .7z, .zip, .rar; <br/> do 7za x -oUNZIPPED $f -ppassword; <br/> done <br/> The program is able to find the formats, but i'm stil having some dificulties to make the program use the others passwords. Nov 5, 2019 at 23:27

1 Answer 1

1

A possible script - save this in a file called e.g. 7za_script.sh

#!/bin/bash

for file in *.7z
do
  for password in pass1 pass2 pass3
  do
    7za x -oOUTPUTDIR -p"${password}" "${file}" && break
  done
done

What does it do?

1. The outer loop for file in *.7z

We define the variable called file to any entry in your current working directory that ends in .7z. The * hereby is a wildcard that is expanded to zero or any number of characters. *.7z will thus match anythng that has a 7zip suffix. The loop now runs once per match and we can refer to the match of the current loop run via $file, i.e. calling a variable.

2. The inner loop for password in pass1 pass2 pass3

In this loop we define tha variable password by explicitely stating three optional values pass1, pass2, and pass3. The loop will run once for each value.

3. The 7za-command

Try to extract the current file from our list with the given password. For each $file all three $password possibilities are tried. The order of nested loops is such that the inner loop will always be run fully before going to the next step in the outer loop.

4. The && break part

break will stop a loop run of the innermost loop. Thus if we call break in the password loop after trying pass1 the pass2 and pass3 option will not even be tried. The question is when NOT to try other passwords. Simple: upon successfully extracting a file with pass1 the other passwords need not to be tried for that file.

Each command has an exit status that is either 0 for success or any other number for fail (the actual value of the number can give more information about what went wrong, but this depends on the individual program). man 7za shows under DIAGNOSTICS:

0 Normal (no errors or warnings detected)

1 Warning (Non fatal error(s)). For example, some files cannot be read during compressing, so they were not compressed

2 Fatal error

Thus if a file is successfully extracted, the exit code should be 0 and if a password does not unlock it, it should be 2. You can get the exit status of a previous command by echo $?. Just try echo $? after running true or false (commands that either always succeed or always fail).

Now && command means "run command if the previous command was successful (exit status = 0). So what we do is: if $password unlocked $file (and no other error occured), we break the password-loop, as there is no reason for trying the other passwords on that file. Instead we can now go on to the next file.

5. Running the script

Once you saved the script as e.g. 7za_script.sh and wrote the passwords in there as you need them, you can run it via bash 7za_script.sh OR make it executable with chmod +x 7za_script.sh and run it via ./7za_script.sh.

6. Notes

  • The script is very simplistic, but just meant as a basic start for your scripting career.
  • Move it to your desired directory and execute it.
  • It will not differ between a file called 1.7za or a directory 2.7za (that is obviously not a zipped thing at all but just shares the name). Testing this would be the next step.
  • It is very unsafe to store passwords in a clear text file, but it seems to be a low security problem in the first place.
  • Quoting " and calling variables as ${var} instead of $var make scripts more stable in case of e.g. spaces in filenames. I'll not go more into detail here but leave the keywords: hard quotes, soft quotes, shell variables
  • && and || are called OPERATORS
  • Take this as a start, try to understand what it does and enjoy scripting.
7
  • It worked for the .7z files, if i want to unzip the .rar and .zip files in the same code what i have to do? Nov 6, 2019 at 0:30
  • Check out how the wildcard selects 7z-files. How could you add wildcards for other suffixes?
    – FelixJN
    Nov 6, 2019 at 0:34
  • the program recognizes the .rar files but still it not being able to unzip the .rar files, for 7z works like a charm, I have the extensions for .rar files installed but I believe these files don't use 7za to uncompress. When I run the program for .rar files, it gives me back an error "Can not open the file as archive" Nov 6, 2019 at 1:00
  • 7za is a reduced but faster version of 7z which might not handle rar. Try using 7z instead of 7za.
    – FelixJN
    Nov 6, 2019 at 1:07
  • Something strange is happening, the program keeps trying no extract all files in the folder including the files that are not in the extensions previously established. I don't know why. Nov 6, 2019 at 1:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .