Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy file permissions, but not files [closed]

I have two copies of the same directory tree. They almost have the same files in both (one version may have a couple extra or missing files). However, most of the files are in common to both directories (have the same relative paths and everything).

Assume these are in directories:

version1/ version2/ 

The problem is that the permissions in version1/ got messed up, and I would like to copy over the permissions from version2/, but do it without replacing the files in version1/ which are newer.

Is there an automated way to do this via bash? (It doesn't have to be bash, it could be some other method/programming language as well).

like image 862
user788171 Avatar asked Mar 06 '13 10:03

user788171


People also ask

How do I copy files without changing permissions?

You can use the -p option of cp to preserve the mode, ownership, and timestamps of the file. However, you will need to add the -r option to this command when dealing with directories. It will copy all sub-directories and individual files, keeping their original permissions intact.

What happens to the permissions when you copy and move a file?

When you copy or move an object to another volume, the object inherits the permissions of its new folder.

Can you copy a file with read permissions?

There is no “Copy” access mask because copying is not a fundamental file operation. Copying a file is just reading it into memory and then writing it out. Once the bytes come off the disk, the file system has no control any more over what the user does with them.

Does copy and paste keep permissions?

When copying a file (copy/paste) or moving it (cut/paste) from one volume to another (e.g., from Collab to Home), the file will lose the original permissions it had before the copy/move.


1 Answers

You should have a look at the --reference option for chmod:

chmod --reference version2/somefile version1/somefile 

Apply find and xargs in a fitting manner and you should be fine, i.e. something like

 ~/version2$ find . -type f | xargs -I {} chmod --reference {} ../version1/{} 

This even works recursively, and is robust against missing files in the target directory (bar the No such file ... errors, which can be ignored). Of course it won't do anything to files that only exist in the target directory.

Cheers,

like image 80
Anders R. Bystrup Avatar answered Oct 05 '22 17:10

Anders R. Bystrup