How To Copy Files And Retain Permissions

 Thu, 10 Nov 2022 09:38 UTC

How To Copy Files And Retain Permissions
Image: CC BY 4.0 by cybrkyd

In Linux, the cp command will preserve file permissions, ownership and timestamps when copying a file or directory.

To expressly instruct cp to preserve file permissions and ownership, use the (lowercase) -p option.

$ cp -p file.txt copy.txt

Preserve permissions when copying files from EXT4 to NTFS

Retaining file permissions and ownership is something easily overlooked when copying from a Linux EXT4 file system to a NTFS partition. For example, I frequently transfer documents to and from my desktop (EXT4) and laptop (EXT4) via a USB key (NTFS).

To preserve file permissions, begin by packing them into a tar archive first.

$ tar -cpvf archive.tar folder/

Move the tar over to the destination machine and extract:

$ tar -xvf archive.tar

The file permissions will be intact when the tar is extracted on the destination EXT4 partition.