user761100
user761100

Reputation: 2317

Why copied file has different permissions in Linux?

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.

Upvotes: 11

Views: 14815

Answers (2)

P.P
P.P

Reputation: 121357

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.

Upvotes: 18

Dan
Dan

Reputation: 11084

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'

Upvotes: 3

Related Questions