Link to home
Start Free TrialLog in
Avatar of Molko
Molko

asked on

unzip then chmod in one command

HI

I have a zip file that I need to unzip, it contains lots of files and takes around 3 hours to unzip. Then i need to issue a recursive chmod 777 against these files, which again takes some time to do.

Is there a smart way to apply the chmod as the files are being unzipped ?

Maybe piping the unzip thru chmod ?...dunno

I was thinking umask ?...But i dont really understand it
Avatar of pilson66
pilson66
Flag of Ukraine image

I suggest you to use tar+gzip (tar -cfz/-xzf) instead of zip. This will store/restore file/directory permissions.
Avatar of Molko
Molko

ASKER

The zip file is not provided by me....I have no control over it

Thanks
I see only way:
unzip bla-bla-bla; chod 777 bla-bla-bla
Avatar of Molko

ASKER

How does that work ? How are you piping the files to chmod ?
ASKER CERTIFIED SOLUTION
Avatar of theodorejsalvo
theodorejsalvo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
hi,
Try this.
unzip file.zip; chmod -R 777

Or in a shell script... myunzip.sh

# ---------------------------------------------------
#Script to unzip and change permissions
#Variables
MYFILE=$1

#go to file location
cd /some/path/where/zip/file/is

#unzip
unzip MYFILE

#changing permissions
chmod -R 777 *

#End Script
#----------------------------------------


To run type this
myunzip.sh myfile


-- hope helps --
Avatar of simon3270
unzip prints out the names of the files it is extracting, as below:
$ unzip bigfile.zip 
Archive:  bigfile.zip
   creating: dir1/
   creating: dir2/
 extracting: dir1/f1                 
 extracting: dir1/f2                 
 extracting: dir2/f4                 
 extracting: dir2/f3                 
$

Open in new window


You can read those names and, with a bit of text processing, pick out the names from the above output text and chmod them as they are extracted.  The command below captures that output as unzip runs, and for any lines where the first word ends "ing:", it runs chmod on the file.
unzip bigfile.zip | awk '/ing:/{print $2}' | xargs chmod 777

Open in new window

As with many commands like this, filenames with special characters (including spaces) will break processing.

If your files might contain spaces, you need a more complex:
unzip bigfile.zip | awk '/ing:/{gsub("^.*ing: ","");gsub(" *$","");printf "%s%c",$0,0}' | xargs -0 chmod 777

Open in new window