Unix: Is there a way to "copy" file or directory permissions?
Solution 1
The GNU version of the chmod utility can copy the mode from one file (RFile
) to another (file
).
chmod --reference=RFile file
GNU coreutils is found in most Linux distributions and Cygwin, among other places. Not all chmod implementations provide this option.
Solution 2
I came up with this:
find $SOURCE -mindepth 1 -printf 'chmod --reference=%p\t%p\n'|sed "s/\t$SOURCE/ $DEST/g"|sh
It is not fully bullet proof, but does what I need.
Solution 3
try this:
find /PATH/TO/TARGET -exec chmod --reference /PATH/TO/SOURCE/{} {} \;
this would go up recursivly and chmod every file, if two directory don't match on files you will see lots of "No such file or directory" error.
Related videos on Youtube
Svish
Software Developer, Geek, HSP, SDA, ..., open, honest, careful, perfectionist, ... Currently into indoor rowing and rock climbing, just to mention something non-computer-related... Not the best at bragging about myself... so... not sure what more to write... 🤔
Updated on September 17, 2022Comments
-
Svish over 1 year
I have two files in a directory. One has correct permissions and the other has not. Is there a way I can "copy" the set of permissions from one file to another?
-
quack quixote about 14 yearschmod is not a bash builtin command. it is a separate utility available on many unixes. the
--reference
option is included in the GNU version; OSX probably uses a chmod that originates with BSD instead. OSX man chmod : developer.apple.com/Mac/library/documentation/Darwin/Reference/… -
Jeremy L about 14 yearsSvish, you might consider installing the GNU versions through MacPorts.
-
LawrenceC about 13 yearsJust figured that it would be useful to mention here that
cp -dpR <source-file> <dest-file>
will, when copying a file, copy permissions as well as the file.r -
Rick Sanchez almost 6 years
find /home/myubuntuuser/Desktop/test1 -exec chmod --reference /home/myubuntuuser/Desktop/test2/{} {} \;
-
Rick Sanchez almost 6 yearschmod: failed to get attributes of '/home/myubuntuuser/Desktop/test2//home/myubuntuuser/Desktop/test1': No such file or directory chmod: failed to get attributes of '/home/myubuntuuser/Desktop/test2//home/myubuntuuser/Desktop/test1/111.txt': No such file or directory chmod: failed to get attributes of '/home/myubuntuuser/Desktop/test2//home/myubuntuuser/Desktop/test1/222.txt': No such file or directory chmod: failed to get attributes of '/home/myubuntuuser/Desktop/test2//home/myubuntuuser/Desktop/test1/333.txt': No such file or directory
-
Rick Sanchez almost 6 yearstested it on 2 folders: test1 and test2. each have the same files 111/222/333.txt with different permissions. test1 has the default ones. test2 has 777 permissions. this is the error i get.