- Clone ownership and permissions from another file?
- 8 Answers 8
- How to copy files and give them permission of destination directory
- 2 Answers 2
- Linux:Copy, change permissions and ownership of a file in a command
- How to Preserve File Permissions While Copying Files in Linux
- Preserve File Permissions Using cp
- Retain Permissions in Linux Using rsync
Clone ownership and permissions from another file?
Is there a command or flag to clone the user/group ownership and permissions on a file from another file? To make the perms and ownership exactly those of another file?
8 Answers 8
On GNU/Linux chown and chmod have a —reference option
chown --reference=otherfile thisfile chmod --reference=otherfile thisfile
Could you reference to this answer (and likely cite it) as answer to my question : unix.stackexchange.com/questions/44253/… ? , I think I will be great addition and I’d love to find up-votes there for it.
@GrzegorzWierzowiecki: probably that question should be closed, but is a little bit different than this and already has answers, so I better do nothing.
As you wish and suggest. Thanks for help, I have never put attention to —reference parameter of chmod and chown before :).
On any unix with GNU utilities, such as (non-embedded) Linux or Cygwin, you can use chmod —reference and chown —reference .
If your system has ACLs, try the ACL commands getfacl and setfacl . These commands differ a little from system to system, but on many you can use getfacl other_file | setfacl -bnM — file_to_change to copy the permissions. This doesn’t copy the ownership; you can do that with careful parsing of ls -l other_file , assuming that you don’t have user or group names containing whitespace.
LC_ALL=C ls -l other_file | < read -r permissions links user group stuff; chown -- "$user:$group" file_to_change >getfacl other_file | setfacl -bnM - file_to_change
@enzotib At least on Linux, ACL tools will work to copy permissions (but not ownership) even if the source and target filesystem don’t support ACLs.
Pour info, on RHEL 7 (and possibly onwards) you can use getfacl other_file | setfacl —set-file=- file_to_change .
Did a bash command based on the response of Matteo 🙂
Egad! Where did you learn to say $ ? Don’t ever do that again! That will fail if any of the filenames contain space (or tabs). Use «$» . Also, use «$1» instead of just $1 .*:2>
chmod «$(stat -c ‘%a’ «$fromfile»)» tofile in GNU Coreutils, but you might as well use —reference in that case since the stat CLI utility is not POSIX, it even says pubs.opengroup.org/onlinepubs/9699919799/utilities/ls.htmlthat ls -l won’t cut it: «The output of ls (with the -l and related options) contains information that logically could be used by utilities such as chmod and touch to restore files to a known state. However, this information is presented in a format that cannot be used directly by those utilities or be easily translated into a format that can be used.»
If you are not using a system with GNU’s chmod/chown (which support the —reference option) you could try to parse the output of ls -l
Here a small script for chmod (if you have a see which supports extended regexes they could be written in a much more readable way . )
#!/bin/sh reference=$1 shift files=$* # strip the permissions (whith extended regexes could be more readable) OWNER=$(ls -l $ | sed -e "s/.\(. \).*/\1/" | sed -e "s/[-]//g" ) GROUP=$(ls -l $ | sed -e "s/. \(. \).*/\1/" | sed -e "s/[-]//g" ) OTHER=$(ls -l $ | sed -e "s/. \(. \).*/\1/" | sed -e "s/[-]//g" ) chmod u=$,g=$,o=$ $
This is even easier using stat :
@jfgagne: thanks makes sense I do not know why I didn’t think about it in the first place. I updated the answer
You’re using *BSD stat syntax here. Your chmod $(stat . ) command won’t work because %p alone outputs too much information for *BSD’s chmod , use %Lp to output just the u/g/o bits. Something slightly more elaborate would be required for sticky/setuid/setgid bits.
That copies all attributes though (change -p to —preserve=ownership,mode to copy only permissions and ownership (and ACLs if any)). That will also not work for files of type directory. Also note that if is a symlink, that will copy the attributes to the target of the symlink (likely what one would want anyway as permissions of symlinks themselves are rarely relevant (though ownership can be)).
On MacOS, cp —attributes-only or chmod —reference won’t work.
A solution for MacOS is to first install coreutils using
then use the coreutils ‘s version of cp command, that is gcp :
gcp --attributes-only --archive sourcefile destfile
This will copy the ownership and attributes while preserving destfile ‘s content and filename.
I wanted to add an adjustment to Matteo’s script. A for loop should be used to validate that the files exist before actually running the chmod command on them. This will let the script error out more gracefully.
I think this is the best option because it can be used for all *nix OSes, like Solaris, Linux, etc.
#!/bin/sh reference=$1 shift files=$* for file in $reference $files; do [ -f $file ] || < echo "$file does not exist"; exit 1; >done # strip the permissions (whith extended regexes could be more readable) OWNER=$(ls -l $ | sed -e "s/.\(. \).*/\1/" | sed -e "s/[-]//g" ) GROUP=$(ls -l $ | sed -e "s/. \(. \).*/\1/" | sed -e "s/[-]//g" ) OTHER=$(ls -l $ | sed -e "s/. \(. \).*/\1/" | sed -e "s/[-]//g" ) chmod u=$,g=$,o=$ $
I found that on one of my Solaris 10 machines, stat was not found. That might be an issue with my configuration though.
How to copy files and give them permission of destination directory
I am copying files from source to location. The source is not owned by me and the permission for files at source is —-rwx—. The permission of files coped to destination directory which is owned by me is —-r-x—. The permission of destination directory is drwxrwsrwx. How do I have the files with same permission of destination directory. I tried «cp —no-preserve=all» but it did not work (still the same permission).
2 Answers 2
cp --no-preserve=mode,ownership $backupfile $destination
This does not set the destination directory’s permissions on newly copied files. Instead, it sets permissions of the user under which copy operation is done. For example, if you copy under root, copied files permissions will be root:root .
Let me rephrase that to «How to preserve permissions of destination directory on copy?»
I can’t take credit for the answer since I just combined a couple of answers I found on the wild. So here it comes.
Permissions are generally not propagated by the directory that files are being copied into, rather new permissions are controlled by the user’s umask. However when you copy a file from one location to another it’s a bit of a special case where the user’s umask is essentially ignored and the existing permissions on the file are preserved.
Which explains why you can’t directly propagate the permissions of the src to the dst directory.
However, there is two-step workaround to this.
- cp-metadata: Copy the attributes and only the attributes you want to preserve back to the source directory. Here is a quick script that can do this:
#!/bin/bash # Filename: cp-metadata myecho=echo src_path="$1" dst_path="$2" find "$src_path" | while read src_file; do dst_file="$dst_path$" $myecho chmod --reference="$src_file" "$dst_file" $myecho chown --reference="$src_file" "$dst_file" $myecho touch --reference="$src_file" "$dst_file" done
You can leave out the touch command if you don’t want keep the timestamp. Replace myecho=echo with myecho= to actually perform the commands.
Mind that this script should be run in sudo mode in order to be able to run chown and chmod effectively
- cp —preserve : After you have successfully run the first command now it’s time to copy the contents along with the attributes to the dst directory.
—preserve[=ATTR_LIST]
preserve the specified attributes (default: mode,ownership,timestamps), if possible additional attributes: context, links, xattr, all
Linux:Copy, change permissions and ownership of a file in a command
Today I came across a requirement to copy couple of files and change permissions to execute those files when writing some ansible playbooks. We can do this by using cp, chmod, chowncommand as shown below. Ownership is changed from root user to normal user surendra.
-rw-r–r– 1 root root 0 Jun 10 21:22 abc.sh
cp abc.sh /tmp/
chmod 755 /tmp/abc.sh
chown surendra.surendra /tmp/abc.sh
ls -l /tmp/abc.sh
Output:
-rwxr-xr-x 1 surendra surendra 0 Jun 10 21:23 /tmp/abc.sh
I can execute these commands with out any problem, but if you want to do same activity on a regular basis then it is time consuming task and more over keeping these commands in ansible play-book is not a great option. How about if we have a command in Linux which do all these stuff in one shot? Yes, we have a command called “install” which do this all in one command. The equivalent install command of above three commands is below.
install -v -g surendra -o surendra -m a+x abc.sh /tmp/abc.sh
To understand above command we should know couple of options for install command. Below examples will help you to understand install command with ease.
Example: Just copy a file from one location to other and do not bother about permissions and ownership.
Example: Copy a file with different permissions, for example every user in my machine should have execute permission on the file
install -m a+x abc.sh /tmp/
Note: -m option arguments will be similar to chmod arguments.
Example: Copy a file with different permissions, for example full permissions for a file for all the users.
install -m 777 abc.sh /tmp/
Example: Copy a file with different owner name
install -m 755 -o surendra abc.sh /tmp/
Example: Copy a file with different group owner
How to Preserve File Permissions While Copying Files in Linux
Want to retain file permissions while copying files on Linux? Here’s how to do it using cp and rsync.
Readers like you help support MUO. When you make a purchase using links on our site, we may earn an affiliate commission. Read More.
File permissions are an integral part of the Unix specification. However, there are certain things starting users are often unaware of, such as how to retain file permissions in Linux while copying them.
Since copied files are essentially new files, their permission depends on the umask of the current user. This can lead to situations where copied files or folders have entirely different permissions than the source.
Luckily for you, it’s easy to retain file permissions in Linux using standard command-line tools like cp and rsync. Check out the below examples to see how to copy and preserve permissions in Linux.
Preserve File Permissions Using cp
The standard cp command has all you need to retain file permissions while copying. 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.
You may also use the -a option of cp to retain file permissions. This enables the Archive mode, preserving everything from file permissions to SELinux contexts.
Retain Permissions in Linux Using rsync
You can also use the rsync utility for preserving copy permissions in Linux. Many admins prefer rsync over cp due to its faster copying speed. Since rsync only copies the updated part of the file, they are more suitable for tasks like cloning your Linux hard drive.