Solved

unzip then chmod in one command

Posted on 2011-09-10
7
1,606 Views
Last Modified: 2012-05-12
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
0
Comment
Question by:Molko
7 Comments
 
LVL 8

Expert Comment

by:pilson66
Comment Utility
I suggest you to use tar+gzip (tar -cfz/-xzf) instead of zip. This will store/restore file/directory permissions.
0
 

Author Comment

by:Molko
Comment Utility
The zip file is not provided by me....I have no control over it

Thanks
0
 
LVL 8

Expert Comment

by:pilson66
Comment Utility
I see only way:
unzip bla-bla-bla; chod 777 bla-bla-bla
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:Molko
Comment Utility
How does that work ? How are you piping the files to chmod ?
0
 
LVL 8

Accepted Solution

by:
theodorejsalvo earned 500 total points
Comment Utility
Here's a good one-liner:

unzip -Z -1 file.zip | xargs chmod 0777

Open in new window


Basically the -Z -1 causes unzip to list each file on it's own line, then xargs puts them all on one line and passes them to chmod.
0
 
LVL 5

Expert Comment

by:hvillanu
Comment Utility
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 --
0
 
LVL 19

Expert Comment

by:simon3270
Comment Utility
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

0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
Utilizing an array to gracefully append to a list of EmailAddresses
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now