Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

copy file, keep permissions and owner

Tags:

The docs of shutil tells me:

Even the higher-level file copying functions (shutil.copy(), shutil.copy2()) can’t copy all file metadata. On POSIX platforms, this means that file owner and group are lost as well as ACLs

How can I keep the file owner and group if I need to copy a file in python?

The process runs on linux as root.

Update: We don't use ACLs. We only need to keep the stuff which is preserved with tools like tar and rsync.

like image 623
guettli Avatar asked Nov 05 '13 10:11

guettli


People also ask

How do I copy a folder keeping owners and permissions intact?

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.

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.

Does cp preserve file permissions?

The -a or --archive option can be used with the cp command in order to preserve file permissions and ownership. Check the example below where we copy a file one directory to another, while preserving these extra attributes.

How do I copy a shared folder and keep permissions?

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.


1 Answers

You perhaps could use os.stat to get the guid and uid like in this answer and then reset the uid and guid after coping using os.chown.

like image 153
Thom Wiggers Avatar answered Oct 31 '22 05:10

Thom Wiggers