How do you copy with permissions?

thanks to answers available here – I know how to replicate permission of an identical folder using icacls /save & /restore or robocopy /MIR. But, i can't make them to bring the same user permission from a folder to target folder with different content. With ìcacls /restore, the target folder still get the user permission inherit from it's parent (although i already explicitly disable inheritance).

My use case is, I have a portable app folder and it will run brokenly when i put it in this troubled subfolder, but fully working when i put it in the root of the same D:\ drive. My folder path doesn't have any "forbidden"characters like % ^ etc., only letters and also the path is not super long.

Is there a way to quickly replicates permission from a known good location to a target folder? Please help.

Many thanks.

How do you copy with permissions?

asked Dec 9, 2021 at 7:42

1

EDIT:

Sorry for posting in a rush.

After checking again, here everything what I found:

  • the robocopy command doesn't assign the permission to file(s) or subfolder(s) inside. So, it need this additional step (on Windows 10): Folder properties > Security tab > Advanced > Permission = Check the box at "Replace all child object permission entries with inheritable permission entries from this object"
  • Icacls /save then Icacls /restore actually also work. But it applies the folder inheritance state. If the "source" having inheritance enabled, then the target folder also get the inheritance enabled (from it's parent, not from source's parent). So, the "source" need to get folder inheritance disabled temporarily.

*) I think it is better not to do this to anticipate breaking the source if something get wrong or forget to revert all changes on "source".

  • I'm not testing further with powershell get-acl -path | set-acl -path but previously I found it worked like icacls /restore

For my use case, I choose the robocopy command. Epecially after finding this https://serverfault.com/questions/475612/replace-permission-entries-on-all-child-objects-using-icacls. I'm not testing this, only imagined this can be combined together into one-line cmd command like:

robocopy d:\source_folder_permission destination_folder_permission /e /sec /secfix /xc /xn /xo /xx /xl & icacls "destination_folder_permission\*" /q /c /t /reset

That is all. Cheers :)


Finally! I found the dream answer. Thanks to this guy Coping permissions only using Robocopy

This is the example robocopy command:

robocopy d:\source_folder_permission destination_folder_permission /e /sec /secfix /xc /xn /xo /xx /xl

I run it as Administrator.

Hope this valuable for everyone. Good day!

answered Dec 9, 2021 at 8:41

ariefcfaariefcfa

711 gold badge2 silver badges8 bronze badges

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 :/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/

Sample output:

# 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  getfacl data

Sample output:

'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

Suggested read:

  • How To Copy Files And Change The Ownership, Permissions At The Same Time

Hope this helps.

Thanks for stopping by!

Help us to help you:

  • Subscribe to our Email Newsletter : Sign Up Now
  • Support OSTechNix : Donate Via PayPal
  • Download free E-Books and Videos : OSTechNix on TradePub
  • Connect with us: Reddit | Facebook | Twitter | LinkedIn| RSS feeds

Have a Good day!!

How do you copy with permissions?

sk

Senthilkumar Palani (aka SK) is the Founder and Editor in chief of OSTechNix. He is a Linux/Unix enthusiast and FOSS supporter. He lives in Tamilnadu, India.

How copy file with 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.

How do I copy files without admin rights?

2 Fix Need administrator permission to copy this file/folder error and copy files.
Take the Ownership of a File or Folder. ... .
Step 1: Open the file or folder properties. ... .
Step 2: Open the Advanced settings. ... .
Step 3: Change the Owner. ... .
Step 4: Set your user account as the owner. ... .
Step 5: Check the box and confirm the changes..

Does copy and paste keep permissions?

When moving files, Windows keeps the original file permissions if you are moving files to a location within the same volume. If you copy and paste or move a file to a different volume, it will be assigned the permissions of the destination folder.

Can you copy a file with read permissions?

File copying will never fail because the source is read only. File copying will fail if the existing destination file is read only. The simple remedy is to clear the read only attribute on the destination file before copying.

How do I copy share permissions in Windows?

Right click the Shares key and select “Export” in Regedit. Give it any name you like and save it to an area accessible from your new server. Now go to your new server and right click the registry file you have exported in Step 1 and select “Merge”. All the share information will be imported in your new server.

How do I copy a shared folder without losing permission?

Usually, if you copy or drag and drop a shared folder to another location, it inherits the permissions and properties of the drive or folder to which you copy it. To copy shared folders without losing attributes or share permissions, you must use the XCOPY command in Windows.