Linux move files with permissions

How can I move a file to another user’s directory?

I’m using CentOS 6 and I’m trying to move a file from USER A to USER B but I keep getting permission denied. What do I need to edit to allow to me to put files from one user’s directory into another’s?

3 Answers 3

You need the rights to read the file from user A, and to write the file to user B’s location.

There are several ways to archive this:

1)
As user A (assumed she can read her own files) get WRITE permission to user B’s location. This means being in a group which is allowed to w to the destination directory and which is allowed to access (x) the directory path to the destination.

cd /home/user/A/ ls -l testfile -rw------- 1 userA users 18 Dec 31 16:31 testfile 

Notice the r. User A is allowed to read her own file

cp testfile /home/userB/testdir 

User A will need to traverse to the folders /home, /home/userB and /home/userB/testdir. All of these need to be ‘x’ for her. She wants to write to the folder ‘testdir’ and thus she also needs ‘w’ on that folder.

That ‘w’ can be in the user, group or other part. But if you set it in ‘other’ then every user can write to it. Thus you usually make shared files or shared folders part of a group. (e.g. in /etc/group create a group ‘sales’ and add both users to that).

2)
As user B: (which is assumed to be able to write in his own folder).
Get read permission on the file in userA’s folder and the right to traverse there. (same as in 1).

You now can copy the file to your own location. You can not delete it from user A’s folder unless you also have the rights to do that. So you can copy but not move.

3)
As a user with uid 0 (usually root, toor, admin):

mv /home/userA/testfile /home/userB/

Notice that the file is still owned by userA, and that user B might not have the rights to work on the file. You can correct this with chown .

Читайте также:  Ftp windows and linux

4)
Without the heavy guns, as often done by two users:

As UserA: cp testfile /tmp/ As userB: cp /tmp/testfile ~ As userA: rm /tmp/testfile 

This is not the most secure way (other can see the file while it is in /tmp), but I think that this is the most common solution.

If you want to do this often then I strongly suggest creating a group and giving all users in that group rights to a shared location.

Not asked, but a solution like this is often practical (shown with music, but it might as well be sales reports).

vim /etc/group and add a line like this: shared_music:*:3141:userA,UserB,UserC . This will create a group called shared_music, give it a number, and select which users will belong to that group.

Then make folder owned by that group.

cd /usr/local/ md our_ultimate_shared_music_collection chgrp shared_music our_ultimate_shared_music_collection chmod g+rwx our_ultimate_shared_music_collection chmod g+S our_ultimate_shared_music_collection 

Make a folder in a sane location. Set the group and then allow all people in that group to read, write and cd into that folder. The last command (make group setuid) means that newly created subfiles inherit the same group as the directory, and newly created subdirectories inherit the set-group-ID bit of the parent directory. This prevent userA from writing files to that directory which USerB (or anyone but A) could not deleted or rename.

Источник

How to move a file in Linux

Whether you’re new to moving files in Linux or experienced, you’ll learn something in this in-depth writeup.

Files in a folder

Moving files in Linux can seem relatively straightforward, but there are more options available than most realize. This article teaches beginners how to move files in the GUI and on the command line, but also explains what’s actually happening under the hood, and addresses command line options that many experience users have rarely explored.

Moving what?

Before delving into moving files, it’s worth taking a closer look at what actually happens when moving file system objects. When a file is created, it is assigned to an inode, which is a fixed point in a file system that’s used for data storage. You can find what inode maps to a file with the ls command:

$ ls --inode example.txt 7344977 example.txt 

When you move a file, you don’t actually move the data from one inode to another, you only assign the file object a new name or file path. In fact, a file retains its permissions when it’s moved, because moving a file doesn’t change or re-create it.

Читайте также:  Linux real time clock

File and directory inodes never imply inheritance and are dictated by the filesystem itself. Inode assignment is sequential based on when the file was created and is entirely independent of how you organize your computer. A file «inside» a directory may have a lower inode number than its parent directory, or a higher one. For example:

$ mkdir foo $ mv example.txt foo $ ls --inode 7476865 foo $ ls --inode foo 7344977 example.txt 

When moving a file from one hard drive to another, however, the inode is very likely to change. This happens because the new data has to be written onto a new filesystem. For this reason, in Linux the act of moving and renaming files is literally the same action. Whether you move a file to another directory or to the same directory with a new name, both actions are performed by the same underlying program.

This article focuses on moving files from one directory to another.

Moving with a mouse

The GUI is a friendly and, to most people, familiar layer of abstraction on top of a complex collection of binary data. It’s also the first and most intuitive way to move files on Linux. If you’re used to the desktop experience, in a generic sense, then you probably already know how to move files around your hard drive. In the GNOME desktop, for instance, the default action when dragging and dropping a file from one window to another is to move the file rather than to copy it, so it’s probably one of the most intuitive actions on the desktop:

Moving a file in GNOME.

The Dolphin file manager in the KDE Plasma desktop defaults to prompting the user for an action. Holding the Shift key while dragging a file forces a move action:

Источник

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).

Читайте также:  Anaconda python linux mint

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.

  1. 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

Источник

Оцените статью
Adblock
detector