Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question already has an answer here:

I have two machines, In my machineA I have a jar file with these permissions -

-rwxr-xr-x 1 cronus app 16758150 2013-03-19 13:35 exhibitor-1.5.1-jar-with-dependencies.jar

In my another machineB, I have a same jar file but with different permissions -

-rw-r--r-- 1 root messagebus 19340260 Nov 25 14:28 exhibitor-1.5.1-jar-with-dependencies.jar

How to make the permission of machineB jar file same as machineA jar file? In short, how do I get this permission -rwxr-xr-x which I can apply on machineB jar file?

And apart from this, can anybody explain me how does this permission work and what does it mean?


Thanks Jordan for the link, I am able to understand its meaning now..

share|improve this question

marked as duplicate by jordanm, slm, Chris Down, Anthon, Alexios Dec 19 '13 at 7:38

This question was marked as an exact duplicate of an existing question.

Thanks but my other question is how to change the permission of machineB jar same as machineA jar? – SSH Dec 19 '13 at 0:56
Using the chmod command. The question you have in the body of the post is a duplicate, but I guess the question in the title is not. – jordanm Dec 19 '13 at 0:56
I updated it to make that more clear. – SSH Dec 19 '13 at 0:58
so it should be chmod 755 jar name right? if I understand correctly from that link? – SSH Dec 19 '13 at 0:59
Sure, but adding execute perms on a jar isn't going to do much since jar files are not executed. I don't know what your problem is but you probably want to change the owner instead. – jordanm Dec 19 '13 at 1:02

The permission columns in the ls output form three groups: user, group and other. Each group corresponds to a class of users: the owner of the file, users other than the owner who are in the group of the file¹, and everybody else. You can set them with chmod by specifying the three columns in order:

chmod u=rwx,g=r-x,o=r-x somefile

You can write the parts in a different order, e.g. chmod o=r-x,u=rwx,g=r-x somefile. It's usual to use the same order as in the ls -l output just to be consistent.

You can also pass numeric modes. They're easier to type but you have to know how they work. Each digit corresponds to one of the groups; add 1 for x, 2 for w and 4 for r. For example, 7 is everything; 5 is read and execute but not write.

chmod 755 somefile

Wikipedia and many other documents about unix file permissions have all the details.

You can save and restore permissons with the getfacl and setfacl utilities. They're overkill in simple cases like this, but they can process a whole directory at once and they handle access control lists in addition to basic file permissions.

getfacl file1 file2 >permissions.txt
setfacl --restore=permissions.txt

¹ Technically, processes whose effective GID or one of whose supplemental GIDs is the group owning the file. Yeah, don't worry about it for now.

share|improve this answer

If you are willing to use python (on both machines) this little snippet of python shows you how to pull the permissions on a file and apply them to another. It uses the paramiko module in python to handle the communication across machines and apply the permissions from one file on the first box to the other file on the second. You can pull down paramiko with easy-install or pip or whatever.

import os
from stat import *
import paramiko

if __name__ == '__main__':

    # make a first test file and open up the permissions on it to
    # everything and anything:
    os.system('touch /tmp/file_1')
    os.system('chmod 777 /tmp/file_1')

    # make a second test file with more restrictive permissions:
    os.system('touch /tmp/file_2')
    os.system('chmod 644 /tmp/file_2')

    # let's talk to the first box:
    ssh_1 = paramiko.SSHClient()

    # make sure paramiko won't complain about unknown hosts:

    # connect:
    ssh_1.connect('localhost', username='yourUserName', password='yourPassword')

    # now use os.stat in python to get the permissions:
    stdin, stdout, stderr = ssh_1.exec_command('''python -c "from stat import *; import os; print os.stat('/tmp/file_1')[0]" ''')    

    # and parse the result taken from box one:
    stdoutFirstBox = stdout.readlines()

    permissions_1 = int(stdoutFirstBox[0].strip('\n'))

    # now apply the permissions from the first test file to the
    # second:
    ssh_2 = paramiko.SSHClient()

    # make sure paramiko won't complain about unknown hosts:

    # connect:
    ssh_2.connect('localhost', username='yourUserName', password='yourPassword')

    # now use os.chmod to apply the permissions from the first to the second
    stdin, stdout, stderr = ssh_2.exec_command('''python -c "import os; os.chmod('/tmp/file_2', {0})" '''.format(permissions_1))    

I cheated a bit here by sshing twice to my own machine but with small modifications I think it'd work for you. If you had to scale it up to whole directories of files you'd have to get fancier but the basis for this approach would work I think.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.