- Linux:Copy, change permissions and ownership of a file in a command
- How to Copy File Permissions and Ownership to Another File in Linux
- Copy File Permissions to Another File
- Copy File Ownership to Another File
- How to copy files and give them permission of destination directory
- 2 Answers 2
- How to Preserve File Permissions While Copying Files in Linux
- Preserve File Permissions Using cp
- Retain Permissions in Linux Using rsync
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 Copy File Permissions and Ownership to Another File in Linux
Assuming you have two files or you have just created a new file and want it to have the same permissions and ownership of an older file.
In this article, we will show you how to copy permissions and ownership from one file to another file in Linux using chmod and chown commands respectively.
Copy File Permissions to Another File
To copy file permissions from one file to another file, use chmod command with the —reference switch in the following syntax, where reference_file is the file from which permissions will be copied rather than specifying mode (i.e octal or numerical mode permissions) for file.
$ chmod --reference=reference_file file
$ ls -l users.list $ ls -l keys.list $ sudo chmod --reference=users.list keys.list $ ls -l keys.list
Copy File Ownership to Another File
Likewise, to copy ownership from another file, use chown command with the —reference switch as well using the following syntax, where reference_file is file from which owner and group will be copied rather than specifying owner:group values for file.
$ chown --reference=reference_file file
$ ls -l keys.list $ touch api.list $ ls -l keys.list $ sudo chown --reference=keys.list api.list $ ls -l api.list
You can also copy file permissions and ownership from one file to multiple files as shown.
$ sudo chmod --reference=users.list users1.list users2.list users3.list $ sudo chown --reference=users.list users1.list users2.list users3.list
For more information, refer to the chown and chmod man pages.
You will also find these guides concerning file permissions to be useful:
That’s all! If you know any other way to copy or clone file permissions in Linux, do share with us via the feedback form below.
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
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.