Change permissions upon uploading with scp

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

My preferred working solution would be to use rsync instead:

Replace:

scp /path/to/file server:/server/path/to/file

With:

rsync --chmod=u+rwx,g+rwx,o+rwx /path/to/file server:/path/to/file

This prevents you from authenticating twice. There are also a lot of other options with rsync which would probably add value such as being able to preserve owner, group, etc.


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.