11

I have files created into my home directory with only user read permission (r-- --- ---). I want to copy this file to another directory /etc/test/ which has the folder permission of 744 (rwx r-- r--). I need to allow for the file I am copying to inherit the permission of the folder it is copied in because so far when I copy it, the files permissions are still the same (r-- --- ---). I have tried setfacl command, but it did not work? Please help.

PS. I can't just chmod -r /etc/test/ because there are many files which will be copied into this folder over time and I don't want to run chmod command every time a file is copied over.

4
  • What setfacl command did you try? What was its output?
    – Mikel
    Feb 14, 2014 at 19:06
  • setfacl -Rm d:u::rwx,d:g::r,d:o::r /etc/test. There was no output it just accepted the command. Feb 14, 2014 at 19:12
  • How are you copying the file? What happens if you use /bin/cp with no options?
    – Mikel
    Feb 14, 2014 at 19:18
  • The command I use is sudo cp /home/file /etc/test/file. I tried using /bin/cp now too with no different result Feb 14, 2014 at 19:23

1 Answer 1

14

Permissions are generally not propagated by the directory that files are being copied into, rather new permissions are controlled by the user's umask. However when you copy a file from one location to another it's a bit of a special case where the user's umask is essentially ignored and the existing permissions on the file are preserved. Understanding this concept is the key to getting what you want.

So to copy a file but "drop" its current permissions you can tell cp to "not preserve" using the --no-preserve=all switch.

Example

Say I have the following file like you.

$ mkdir -m 744 somedir

$ touch afile
$ chmod 400 afile 

$ ll
total 0
-r--------. 1 saml saml 0 Feb 14 15:20 afile

And as you've confirmed if we just blindly copy it using cp we get this:

$ cp afile somedir/
$ ls -l somedir/
total 0
-r--------. 1 saml saml 0 Feb 14 15:20 afile

Now let's repeat this but this time tell cp to "drop permissions":

$ rm -f somedir/afile 

$ cp --no-preserve=all afile somedir/

$ ls -l somedir/
total 0
-rw-rw-r--. 1 saml saml 0 Feb 14 15:21 afile

So the copied file now has its permissions set to 664, where did it get those?

$ umask
0002

If I changed my umask to something else we can repeat this test a 3rd time and see the effects that umask has on the un-preserved cp:

$ umask 037
$ rm somedir/afile 

$ cp --no-preserve=all afile somedir/
$ ls -l somedir/
total 0
-rw-r-----. 1 saml saml 0 Feb 14 15:29 afile

Notice the permissions are no longer 664, but are 640? That was dictated by the umask. It was telling any commands that create a file to disable the lower 5 bits in the permissions ... these guys: (----wxrwx).

3
  • The --no-preserve flag isn't standard, presumably a GNUism.
    – vonbrand
    Feb 14, 2014 at 20:38
  • Correct, thanks I meant to say that in the A.
    – slm
    Feb 14, 2014 at 20:38
  • in the spirit of Valentine's Day I give you a virtual kiss , it works! THANKS! Feb 14, 2014 at 20:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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