strange (to me) behavior of file permissions when copying file
Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
strange (to me) behavior of file permissions when copying file
I wish that when copying a file, it has the attributes defined by umask. This sometimes happens, other times do not:
$ls -l /bin/cat
-rwxr-xr-x 1 root root 44072 11 set 13:18 /bin/cat
$umask 0002
$cp /bin/cat mycat ; ls -l mycat
-rwxr-xr-x 1 rob rob 44072 22 ott 17:51 mycat
The date have been updated, but the permissions don't (the group can't write). Now if I try with:
$umask 0077
$cp /bin/cat mycat ; ls -l mycat
-rwxr-xr-x 1 rob rob 44072 22 ott 17:52 mycat
$rm mycat
$cp /bin/cat mycat ; ls -l mycat
-rwx------ 1 rob rob 44072 22 ott 17:52 mycat
Worked! Note that I had to delete the file in order to have the right permissions set.
It seems that umask is followed only if it is more restrictive than the original file.
Is there any way to force umask for copied files?
I also tried with:
$rm mycat
$umask 0002
$cp --no-preserve=all /bin/cat mycat ; ls -l mycat
-rwxr-xr-x 1 rob rob 44072 22 ott 18:00 mycat
Hi there, i agree your issues seem strange, but copy is not really the right command to install and setup file permissions in one command.
The install command is much better ...
[neil@f11 tmp]$ touch nhi
[neil@f11 tmp]$ install -m 777 nhi nhi2
[neil@f11 tmp]$ ls -l nhi*
-rw-rw-r-- 1 neil neil 0 2009-10-22 19:51 nhi
-rwxrwxrwx 1 neil neil 0 2009-10-22 19:51 nhi2
Using install is a nice workaround. I wish to understand if the behavior of umask is a bug or a programmers' choice. Moreover, I can not use install when using scp or a filemanager such as dolphin or konqueror.
There should be a way to automatically set the default permissions to new files, also when copied.
Using install is a nice workaround. I wish to understand if the behavior of umask is a bug or a programmers' choice. Moreover, I can not use install when using scp or a filemanager such as dolphin or konqueror.
There should be a way to automatically set the default permissions to new files, also when copied.
This command --
Code:
$ cp -p path1 path2
-- preserves the original's permissions and modes. So does this:
Code:
$ scp -p path1 path2
from "man scp":
-p: Preserves modification times, access times, and modes from the original file.
Maybe I wasn't clear enough.
This is what I DON'T want to do: I do not want to preserve permissions BUT I wish to use those from umask.
But "umask" is not part of a transferred file's description. It has effect only when creating a new file. So always create a new file, never copy over an existing one. Simple (but risky).
So, to answer your original inquiry:
Quote:
Originally Posted by warrob
Is there any way to force umask for copied files?
No, it isn't. The "umask" property is not transferred with files, and is unknown to the file copying utilities.
Have you considered just setting permissions on all the copied files rather than trying to trick the system into behaving as though the files are newly created? This is safer than deleting then copying, for a reason that should be obvious.
Perform a data transfer.
Reset permissions on the target directory's contents (or on single files as they are copied).
This would be a better approach than deleting then copying.
It appears that this is not an issue with umask i think it is really cp that was holding you up, using --remove-destination works.
...
...
...
You guys have not red the original post:
if I set umask 0077 , this will affect the copied file (even without --remove-destination) but with umask 0002 the original permissions are retained. That's weird to me!
So why umask is applied if I set umask 0077 and it is not with umask 0002 ??
Code:
$ man umask
umask [-p] [-S] [mode]
The user file-creation mask is set to mode. If mode begins with a digit, it is interpreted as an octal number; otherwise it is interpreted as a symbolic mode mask similar to that accepted by chmod(1). If mode is omitted, the current value of the mask is printed. The -S option causes the mask to be printed in symbolic form; the default output is an octal number. If the -p option is supplied, and mode is omitted, the output is in a form that may be reused as input. The return status is 0 if the mode was successfully changed or if no mode argument was supplied, and false otherwise.
Remember "umask" is a mask, not a value. Think about what that means.
In the example you chose, the copied file has to be created, which means it's a new file. And the source umask has no effect on the destination, which was my point. Your example shows that the destination umask sets default permissions on newly created files, which is what it's for.
Remember "umask" is a mask, not a value. Think about what that means.
In the example you chose, the copied file has to be created, which means it's a new file. And the source umask has no effect on the destination, which was my point. Your example shows that the destination umask sets default permissions on newly created files, which is what it's for.
You are right. I'm still not sure in which way the mask is applied to the source-file permissions. I'm guessing if there exists a mask value that creates new files with permissions rwxrwxr-- , independently of the source-file permissions. My feeling is that probably doesn't exist.
Besides, any idea on why the --no-preserve option in cp does not work?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.