Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why copied file has different permissions in Linux?

Tags:

linux

cp

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.

like image 979
user761100 Avatar asked Apr 26 '16 18:04

user761100


People also ask

Does copying a file change the permissions Linux?

Permissions are generally not propagated by the directory that files are being copied into, rather new permissions are controlled by the user's umask.

Does copying a file change permissions?

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.

What permissions are needed to copy a file Linux?

For creating a copy alone, only read permissions on the source file and execute permissions on all its (parent) directories are needed.

How do I copy files without changing permissions?

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.


2 Answers

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.

like image 191
P.P Avatar answered Sep 22 '22 00:09

P.P


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'
like image 41
Dan Avatar answered Sep 22 '22 00:09

Dan