I am logged in as root in Linux. I have a file with 777 permissions. I copied the file in the same directory with cp
.
cp settings.php settings_copy.php
However, the copied file has different file permissions.
[root@localhost default]# ls -l setting*
-rwxr-xr-x. 1 root root 29105 Apr 26 11:48 settings_copy.php
-rwxrwxrwx. 1 root root 29105 Apr 26 09:48 settings.php
Is this normal? How can I ensure that the copied file gets the same permissions? I believe that it is the default behaviour for the copy command in any OS.
Permissions are generally not propagated by the directory that files are being copied into, rather new permissions are controlled by the user's umask.
When you copy a protected file to a folder on the same, or a different volume, it inherits the permissions of the target directory. However, when you move a protected file to a different location on the same volume, the file retains its access permission setting as though it is an explicit permission.
For creating a copy alone, only read permissions on the source file and execute permissions on all its (parent) directories are needed.
To preserve permissions when files and folders are copied or moved, use the Xcopy.exe utility with the /O or the /X switch. The object's original permissions will be added to inheritable permissions in the new location.
Use the -p
option to preserve the permissions:
cp -p settings.php settings_copy.php
When you copy a file, you are creating a new file. So, its (new file) permissions depends on the current file creation mask, which you change via umask
command. Read man umask
for more information.
have you looked at man cp
This is the relevant section:
-p same as --preserve=mode,ownership,timestamps
--preserve[=ATTR_LIST]
preserve the specified attributes (default: mode,ownership,timestamps), if possible additional attributes: context, links, xattr, all
So to keep the same ownership and mode you would run the command:
cp --preserve=mode,ownership
If you know that's always what you want and don't want to remember it, you can add it as an alias to your .bashrc;
alias cp='cp --preserve=mode,ownership'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With