Copy files in linux with permissions

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.

Copy File Permissions to Another File

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.

Copy File Ownership to Another File

You can also copy file permissions and ownership from one file to multiple files as shown.

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.

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

We are thankful for your never ending support.

Источник

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

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.

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

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.

Example: Copy a file with different owner name

Example: Copy a file with different group owner

Источник

How To Copy Files And Change The Ownership, Permissions At The Same Time

This brief tutorial describes how to copy files and change the ownership, permissions at the same time from command line in Unix-like operating systems.

Читайте также:  Лазерный принтер марает бумагу при печати что делать

Usually, we use «cp» command to copy files from one location to another. Then, we use «chmod», and «chown» commands to change the permissions and ownership of a file, respectively.

However, we can combine all these tasks into a one-liner command, and still get the same result instead of running the three consecutive commands. This can be helpful when you want to do this on a regular basis, or within a script.

Copy Files And Change The Ownership, Permissions At The Same Time

Have a look at the following example.

To copy files and change permissions and ownership, we usually do:

However, you don’t have to use three commands to this simple task. We can do this with a single command. This is where the «install» command comes in handy.

I thought «install» command is meant to install things. But, I was wrong! The «install» command is used to copy files into destination of your choice and set attributes. It is part of GNU coreutils which comes pre-installed with all Linux distributions.

Now, let me show how to change the permissions and ownership of a file during copy.

The above command will copy the file /dir1/file1 to /dir2, change the permissions of the file to 775, the owner to sk, and the group to ostechnix.

Let us break down the above command and see what each part of command does.

  • -C, (—compare) : Compare each pair of source and destination files, and in some cases, do not modify the destination at all.
  • -m, (—mode) : Set permission mode (as in chmod).
  • -o, (—owner) : Set user ownership.
  • -g, (—group) : Set group ownership.

Источник

How To Keep Ownership And File Permissions Intact When Copying Files Or Directories

The other day I planned to backup some data from my Ubuntu desktop system to an external USB drive. After I transferred all data to the external drive, I noticed that the owner and permissions of the files and directories on source and destination are different. But, I wanted to keep existing file attributes (such as owner, group and timestamp etc.) intact on both location. After a quick Google search and going through man pages of «cp» command, I found that we can keep ownership and file permissions intact when copying files and directories on Linux. If you ever been in a situation like this, here is a quick workaround to do it.

Keep Ownership And File Permissions Intact When Copying Files Or Directories On Linux

The cp command has an option to copy files and directories but preserves modification times, access times, and modes from the original file.

Here, I am copying the contents of a folder named «data» to my external drive.

From the cp command’s man pages:

So, if you use -p flag, it will preserve all existing file attributes such as mode, ownership and timestamps from original file.

And the «-r» flag is used here to copy directories recursively. Meaning — it will copy directories and its sub-directories and files.

Alternatively, you can use -a flag. It includes the -r flag and preserves everything, such as links, xattr, SELinux attributes etc.

From cp command’s man pages:

To verify if the file permissions and ownership are intact, use getfacl command on both locations i.e. source and destination.

If you want to copy files between from the local system to a remote system in the same network, you can use «scp» command to transfer files from one system to another like below.

The above command will copy the contents folder named «data» from my local system to a remote system. Here, 192168.225.22 is the IP address of my remote system and «senthil» is the user name of remote system.

Check the file permissions and ownership of the «data» directory on both systems using «getfacl» command.

First let check the file attributes of «data» directory on the local system:

Sample output:

Next, check the file attributes of «data» directory on the remote system. You can directly log in to the remote system and check the fie attributes of a remote directory via ssh command like below.

As you may noticed in the above outputs, the owner and group information (i.e. senthil) are different in my remote system. Because, the -p flag of scp command doesn’t preserve all permissions.

Читайте также:  Как стереть с бумаги напечатанное принтером лазерным

From the man pages of scp command:

It is clear that the -p flag will only preserve modification times, access times, and modes from the original file, but not the ownership. In such cases, you can create a common user name on both systems and try the above command to carry the same ownership and file permissions on different systems. Or simply use the «chown» command on the destination system to change the ownership.

For more details, refer man pages of cp and scp commands.

Источник

Copying files in Linux

Copying documents used to require a dedicated staff member in offices, and then a dedicated machine. Today, copying is a task computer users do without a second thought. Copying data on a computer is so trivial that copies are made without you realizing it, such as when dragging a file to an external drive.

The concept that digital entities are trivial to reproduce is pervasive, so most modern computerists don’t think about the options available for duplicating their work. And yet, there are several different ways to copy a file on Linux. Each method has nuanced features that might benefit you, depending on what you need to get done.

Here are a number of ways to copy files on Linux, BSD, and Mac.

Copying in the GUI

As with most operating systems, you can do all of your file management in the GUI, if that’s the way you prefer to work.

The most obvious way to copy a file is the way you’re probably used to copying files on computers: drag and drop. On most Linux desktops, dragging and dropping from one local folder to another local folder moves a file by default. You can change this behavior to a copy operation by holding down the Ctrl key after you start dragging the file.

Your cursor may show an indicator, such as a plus sign, to show that you are in copy mode:

Note that if the file exists on a remote system, whether it’s a web server or another computer on your own network that you access through a file-sharing protocol, the default action is often to copy, not move, the file.

Right-click

If you find dragging and dropping files around your desktop imprecise or clumsy, or doing so takes your hands away from your keyboard too much, you can usually copy a file using the right-click menu. This possibility depends on the file manager you use, but generally, a right-click produces a contextual menu containing common actions.

The contextual menu copy action stores the file path (where the file exists on your system) in your clipboard so you can then paste the file somewhere else:

In this case, you’re not actually copying the file’s contents to your clipboard. Instead, you’re copying the file path. When you paste, your file manager looks at the path in your clipboard and then runs a copy command, copying the file located at that path to the path you are pasting into.

Copying on the command line

While the GUI is a generally familiar way to copy files, copying in a terminal can be more efficient.

The obvious terminal-based equivalent to copying and pasting a file on the desktop is the cp command. This command copies files and directories and is relatively straightforward. It uses the familiar source and target (strictly in that order) syntax, so to copy a file called example.txt into your Documents directory:

Just like when you drag and drop a file onto a folder, this action doesn’t replace Documents with example.txt. Instead, cp detects that Documents is a folder, and places a copy of example.txt into it.

You can also, conveniently (and efficiently), rename the file as you copy it:

That fact is important because it allows you to make a copy of a file in the same directory as the original:

To copy a directory, you must use the -r option, which stands for —recursive. This option runs cp on the directory inode, and then on all files within the directory. Without the -r option, cp doesn’t even recognize a directory as an object that can be copied:

The cat command is one of the most misunderstood commands, but only because it exemplifies the extreme flexibility of a POSIX system. Among everything else cat does (including its intended purpose of concatenating files), it can also copy. For instance, with cat you can create two copies from one file with just a single command. You can’t do that with cp.

Читайте также:  Postgresql сложение с null

The significance of using cat to copy a file is the way the system interprets the action. When you use cp to copy a file, the file’s attributes are copied along with the file itself. That means that the file permissions of the duplicate are the same as the original:

Using cat to read the contents of a file into another file, however, invokes a system call to create a new file. These new files are subject to your default umask settings. To learn more about umask , read Alex Juarez’s article covering umask and permissions in general.

Run umask to get the current settings:

This setting means that new files created in this location are granted 664 (rw-rw-r—) permission because nothing is masked by the first digits of the umask setting (and the executable bit is not a default bit for file creation), and the write permission is blocked by the final digit.

When you copy with cat, you don’t actually copy the file. You use cat to read the contents of the file, and then redirect the output into a new file:

As you can see, cat created a brand new file with the system’s default umask applied.

In the end, when all you want to do is copy a file, the technicalities often don’t matter. But sometimes you want to copy a file and end up with a default set of permissions, and with cat you can do it all in one command .

rsync

The rsync command is a versatile tool for copying files, with the notable ability to synchronize your source and destination. At its most simple, rsync can be used similarly to cp command:

The command’s true power lies in its ability to not copy when it’s not necessary. If you use rsync to copy a file into a directory, but that file already exists in that directory, then rsync doesn’t bother performing the copy operation. Locally, that fact doesn’t necessarily mean much, but if you’re copying gigabytes of data to a remote server, this feature makes a world of difference.

What does make a difference even locally, though, is the command’s ability to differentiate files that share the same name but which contain different data. If you’ve ever found yourself faced with two copies of what is meant to be the same directory, then rsync can synchronize them into one directory containing the latest changes from each. This setup is a pretty common occurrence in industries that haven’t yet discovered the magic of version control, and for backup solutions in which there is one source of truth to propagate.

You can emulate this situation intentionally by creating two folders, one called example and the other example_dupe:

Create a file in the first folder:

Use rsync to synchronize the two directories. The most common options for this operation are -a (for archive, which ensures symlinks and other special files are preserved) and -v (for verbose, providing feedback to you on the command’s progress):

The directories now contain the same information:

If the file you are treating as the source diverges, then the target is updated to match:

Keep in mind that the rsync command is meant to copy data, not to act as a version control system. For instance, if a file in the destination somehow gets ahead of a file in the source, that file is still overwritten because rsync compares files for divergence and assumes that the destination is always meant to mirror the source:

If there is no change, then no copy occurs.

The rsync command has many options not available in cp, such as the ability to set target permissions, exclude files, delete outdated files that don’t appear in both directories, and much more. Use rsync as a powerful replacement for cp, or just as a useful supplement.

Many ways to copy

There are many ways to achieve essentially the same outcome on a POSIX system, so it seems that open source’s reputation for flexibility is well earned. Have I missed a useful way to copy data? Share your copy hacks in the comments.

Источник

Поделиться с друзьями
КомпСовет
Adblock
detector