I am uploading files to my shell account using scp. As I need different permissions on the server than on my computer, I'd like to have a way to easily change the permissions upon upload without needing to ssh to the account and change them manually.

link|improve this question
feedback

migrated from stackoverflow.com Jun 2 '11 at 0:48

This question came from our site for professional and enthusiast programmers.

4 Answers

If you're copying from a windows machine, you can use WinSCP to copy, and it has an option to set the permissions on the copied files after the upload.

If not, I think your only choice is to execute a chmod on the server after the upload, which you could do remotely with an ssh command:

scp /path/to/file server:/server/path/to/file
ssh server chmod 644 /server/path/to/file
link|improve this answer
I thought about that too, what about uploading directories then? – Florian Mayer Oct 11 '08 at 17:16
Hm, I could do chmod -R then. Not a bad idea I guess. – Florian Mayer Oct 11 '08 at 17:22
Right. scp -r, then ssh chmod -R – zigdon Oct 11 '08 at 17:47
feedback

You could do it using tar, ssh, & umask like this:

on host 1:

[saml@host1 testdir]# pwd
/tmp/testdir

[saml@host1 testdir]# ls -l
total 12
-rw-r--r--  1 saml saml 21 May 19 00:21 file1
-rw-r--r--  1 saml saml 48 May 19 00:21 file2
-rw-r--r--  1 saml saml 28 May 19 00:21 file3

[saml@host1 testdir]# tar cvf - . | (ssh host2 "umask 0277; cd /tmp/testdir;tar xvf -")
./
./file1
./file2
./file3
./
./file1
./file2
./file3

on host2:

[samr@host2 testdir]# pwd
/tmp/testdir

[samr@host2 testdir]# ls -l
total 12
-r-------- 1 samr web 21 May 19 00:21 file1
-r-------- 1 samr web 48 May 19 00:21 file2
-r-------- 1 samr web 28 May 19 00:21 file3

You can drop the -v switches to tar which I've included here merely so that you can see the files being tarred up on host1 and sent through STDOUT (aka. -) and then getting un-tarred on host2.

NOTE: Why this works? Tar's default behavior is to unpack files using a remote user's umask. In the above example I've included the command umask to explicitly set it to something different which demonstrates that the remote tar is changing the permissions on the remote side.

link|improve this answer
Can you please delete the comment above now? I've cleaned up the answer. – slm Jul 30 '11 at 1:48
feedback

I wrote a small script for the task in Python. You can do python script.py -p o+r some files some/dir/on/the/server/

import subprocess
import sys
from optparse import OptionParser


DEFAULT_SERVER = 'your.server.com'

parser = OptionParser()

parser.add_option("-p", "--permissions", action="store", 
                     type="str", dest="perm", metavar="PERM",
                     help="chmod files to PERM", default=None)
parser.add_option("-s", "--server", action="store", 
                     type="str", dest="serv", metavar="SERVER",
                     help="scp to SERVER", default=DEFAULT_SERVER)

options, args = parser.parse_args()
files = args[:-1]
direct = args[-1]

proc = subprocess.Popen(['scp'] + files + ['%s:%s' % (options.serv, direct)],
                        stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if proc.wait() != 0:
    print >>sys.stderr, "Uploading failed!"
    sys.exit(1)

if options.perm is not None:
    arg_dict = dict(dir=direct, perm=options.perm, files=' '.join(files))
    proc = subprocess.Popen(['ssh', options.serv, 'cd %(dir)s;'
                             'chmod -R %(perm)s %(files)s' % arg_dict],
                            stdout=subprocess.PIPE, stderr=subprocess.PIPE)
link|improve this answer
Do you mind posting your code at Code Review. There could be nice suggestions there for how it can be improved. – Tshepang Jul 21 '11 at 13:38
feedback

Assuming you are uploading to a UNIX variant, I think that the permissions ought to follow your UMASK settings. I don't recall off the top of my head which dot-files get processed for SCP, but if you set your UMASK in one of those the files you create will have it's permissions set based on it. It probably depends on what shell you use on the remote system.

Whatever you do, don't use the -p option as it does the exact opposite of what you want.

link|improve this answer
Nope. -p is not what I wanted because I need different permissions on the server(yes it's an UNIX) than on my local machine. I could of course chmod them, use -p and then chmod them back, though I'd need to store the permissions then. – Florian Mayer Oct 11 '08 at 17:05
I wasn't suggesting that you use -p, just confirming that you were. I'll clarify my post. – tvanfosson Oct 11 '08 at 17:18
feedback

Your Answer

 
or
required, but never shown

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