We help IT Professionals succeed at work.

linux file permissions

XK8ER
XK8ER asked
on
hello there,
I am running centos on a web server and I run a php software that has over 100 files and dirs and a lof of them have different file permissions.
every month there is a new software update and I receive the zip file by email.. so what I do is upload the file and unzip it.. then set permissions all over again..
is it possible to unzip and keep the original file permissions? or unzip in a new location and then copy/overwrite but keep old permissions?
Comment
Watch Question

CERTIFIED EXPERT

Commented:
According the the unzip manpage there is no switch for keeping permissions.  But you can set permissions recursively with chmod.

Like so:
chmod -R 644 /parent/directory/

This would change the permissions of everything in /parent/directory/ and below to 644.  
Kerem ERSOYPresident

Commented:
Hello,

there are some options:
- You can create a shell script and put all your permission setting commands in it will set permissions after you unzip it.
- Don't forget that only root user could change permissions of a file for any user so do unzip under root.
- When you unzip the file and copy over the existing files the file contents will be renewed but they will keep the user permissions.

Cheers,
K.
CERTIFIED EXPERT

Commented:
The zip utility retains file permissions and ownership by default.  If you unzip it on a different server, all the read/write/execute permissions should be 100% intact.

What I'm guessing you're seeing though is that the owners uid/gids don't match on the source server, so when you unzip on the destination server, all the owner/groups show up as numerical?  (or as the wrong users/groups)

Author

Commented:
KeremE, that sounds good.. how can I do that exactly?
Keith BrownAWS System Administrator
CERTIFIED EXPERT

Commented:
xterm, it depends entirely on the utility used to compress the files. Not all archive utilities retain permissions and other pertinent information. However, the only time he'd likely run into issues on that is if it created on a Windows machine, or something like that.

I'm with you though, in thinking that it is a user mismatch. It should retain the proper user ID from who compressed it, but if the owner of the files is for a different account than the one the OP is using, that'd have issues. Hard to say without seeing an example of how the mismatch is.
CERTIFIED EXPERT

Commented:
@Hellmark:
The author said the zip was made on a CentOS Linux machine, thus there are a few things we can know with reasonable certainty:
1)  The utility "zip" was used, which retains both user and file permissions
2)  The zip file itself will have ownership permissions of whomever created it.  Not really relevant, as he's talking about the files within the archive
3)  The files within will have the numerical uids and gids from the passwd/shadow files on the SOURCE machine, but if the DESTINATION machine has different uid/gid values for the same usernames, the permissions will look different (but won't BE different)

So again, let's get confirmation of that, and then I can tell you how to fix it.

Author

Commented:
>>The author said the zip was made on a CentOS Linux machine

i said decompressing on centos.. im not the one making the software updates and I dont know what they use to make the zip file..
CERTIFIED EXPERT

Commented:
Well, the program used to compress the files almost certainly retains the correct permissions on the system that the files came from, but they are going to be different on your CentOS machine, naturally.

You're going to have to set the permissions every time to what you want, so if you can tell me what you normally do to fix them each time, I can probably help you to write a little script which will do the copying, unzipping, and permissions changing and save you some time.

Commented:
As suggested by KeremE, you can have the commands you normally use to change the permissions in a shell script, then run this script after unzipping

Author

Commented:
I understood exactly what he said I just dont know exactly how to accomplish that..
CERTIFIED EXPERT
Commented:
@XK8ER
Can you tell us exactly what sequence of commands you normally do when you unzip the archive on the CentOS machine?

If they are the same every time, you can make a script to do this for you.  This is the basic idea for getting started.

  touch myscript.sh

This just creates an empty file that will be your script

  chmod 755 myscript.sh

This sets executable permissions on your script

  mkdir ~/bin

This makes a "bin" directory in your home directory - if it fails because it already exists, don't worry

  mv myscript.sh ~/bin
  cd ~/bin

This moves the script to your bin directory - cd there so you can edit it.

  vi myscript.sh

You can use any editor you like to edit you script, vi, pico or any of the graphical programs if you're running Xorg

Now simply put all the commands you normally do into the script.  Keep in mind you will need full paths, so if your zip file is always in /tmp, you need to put this in your script.  So it may look something like the attached.
#!/bin/sh

MYZIPFILE="/tmp/foo.zip"
MY_DESIRED_DESTINATION="/home/freddy/new_folder"

mv $MYZIPFILE $MY_DESIRED_DESTINATION
cd $MY_DESIRED_DESTINATION
unzip $MYZIPFILE
# RECURSIVELY CHANGE OWNERSHIP/GROUP TO YOUR USER
chown -R someuser:somegroup $MY_DESIRED_DESTINATION
# MAKE ALL DIRECTORIES MODE 0755
chmod 755 `find $MY_DESIRED_DESTINATION -type d`
# MAKE ALL FILES MODE 0644
chmod 644 `find $MY_DESIRED_DESTINATION -type f`

Open in new window

Keith BrownAWS System Administrator
CERTIFIED EXPERT

Commented:
A shell script can be something as simple as a text file, set to be able to be executed in the file permissions (in KDE, Gnome, etc right click on it and goto Properties, or from the commandline use chmod), and on each line is a command that you would ordinarily run. Really not much to it. Bash scripts can be all fancy with different logic operations and such, but it really doesn't have to be.

(I realize you probably know about to use chmod since you have to change permissions on these files on a regular basis, I just put the GUI directions in for ehlping with others that may come across this question)

Explore More ContentExplore courses, solutions, and other research materials related to this topic.