How To Keep Ownership And File Permissions Intact When Copying Files Or Directories
The other day I planned to backup some data from my Ubuntu desktop system to an external USB drive. After I transferred all data to the external drive, I noticed that the owner and permissions of the files and directories on source and destination are different. But, I wanted to keep existing file attributes (such as owner, group and timestamp etc.) intact on both location. After a quick Google search and going through man pages of “cp” command, I found that we can keep ownership and file permissions intact when copying files and directories on Linux. If you ever been in a situation like this, here is a quick workaround to do it.
Keep Ownership And File Permissions Intact When Copying Files Or Directories On Linux
The cp command has an option to copy files and directories but preserves modification times, access times, and modes from the original file.
$ cp -rp ~/data /media/sk/sk_seagate/data/
Here, I am copying the contents of a folder named “data” to my external drive.
From the cp command’s man pages:
-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, if you use -p flag, it will preserve all existing file attributes such as mode, ownership and timestamps from original file.
And the “-r” flag is used here to copy directories recursively. Meaning – it will copy directories and its sub-directories and files.
Alternatively, you can use -a flag. It includes the -r flag and preserves everything, such as links, xattr, SELinux attributes etc.
$ cp -a ~/data /media/sk/sk_seagate/data/
From cp command’s man pages:
-a, --archive same as -dR --preserve=all
To verify if the file permissions and ownership are intact, use getfacl command on both locations i.e. source and destination.
$ getfacl ~/data
$ getfacl /media/sk/sk_seagate/data/
If you want to copy files between from the local system to a remote system in the same network, you can use “scp” command to transfer files from one system to another like below.
$ scp -rp ~/data [email protected]:/home/senthil/
The above command will copy the contents folder named “data” from my local system to a remote system. Here, 192168.225.22 is the IP address of my remote system and “senthil” is the user name of remote system.
Check the file permissions and ownership of the “data” directory on both systems using “getfacl” command.
First let check the file attributes of “data” directory on the local system:
$ getfacl data/
# file: data/ # owner: sk # group: sk user::rwx group::rwx other::r-x
Next, check the file attributes of “data” directory on the remote system. You can directly log in to the remote system and check the fie attributes of a remote directory via ssh command like below.
$ ssh [email protected] getfacl data
[email protected]'s password: # file: data # owner: senthil # group: senthil user::rwx group::rwx other::r-x
As you may noticed in the above outputs, the owner and group information (i.e. senthil) are different in my remote system. Because, the -p flag of scp command doesn’t preserve all permissions.
From the man pages of scp command:
-p Preserves modification times, access times, and modes from the original file.
It is clear that the -p flag will only preserve modification times, access times, and modes from the original file, but not the ownership. In such cases, you can create a common user name on both systems and try the above command to carry the same ownership and file permissions on different systems. Or simply use the “chown” command on the destination system to change the ownership.
For more details, refer man pages of cp and scp commands.
$ man cp
$ man scp
Hope this helps.