在 Linux 中将文件从一个用户主目录复制到另一个用户主目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18869066/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Copy files from one user home directory to another user home directory in Linux
提问by kirpi4
I have the logins and passwords for two linux users (not root), for example user1 and user2.
我有两个 linux 用户(不是 root)的登录名和密码,例如 user1 和 user2。
How to copy files from /home/user1/folder1to /home/user2/folder2, using one single shell script (one single script launching, without manually switching of users).
如何将文件从/home/user1/folder1复制到/home/user2/folder2,使用单个 shell 脚本(单个脚本启动,无需手动切换用户)。
I think I must use a sudo command but didn't found how exactly.
我想我必须使用 sudo 命令,但没有找到确切的方法。
回答by fedorqui 'SO stop harming'
Just this:
只是这个:
cp -r /home/user1/folder1/ /home/user2/folder2
If you add -p
(so cp -pr
) it will preserve the attributes of the files (mode, ownership, timestamps).
如果添加-p
(so cp -pr
),它将保留文件的属性(模式、所有权、时间戳)。
-r
is required to copy hidden files as well. See How to copy with cp to include hidden files and hidden directories and their contents?for further reference.
-r
也需要复制隐藏文件。请参阅如何使用 cp 复制以包含隐藏文件和隐藏目录及其内容?以供进一步参考。
回答by Alfe
(shopt -s dotglob; cp -a /home/user1/folder1/* /home/user2/folder2/)
Will copy all files (including those starting with a dot) using the standard cp
. The /folder2/
should exist, otherwise the results can be nasty.
将使用标准cp
. 本/folder2/
应该存在,否则其结果可能是讨厌。
Often using a packing tool like tar
can be of help as well:
通常使用像这样的打包工具tar
也有帮助:
cd /home/user1/folder1
tar cf - . | (cd /home/user2/folder2; tar xf -)
回答by user3363247
The question has to to do with permissions across users.
问题与跨用户的权限有关。
I believe by default home permission does allow all people to do listing and changing working directory into another's home: eg. drwxr-xr-x Hence in the previous answers people did not realise what you might have encountered.
我相信默认情况下 home 权限确实允许所有人将工作目录列出并将其更改为另一个人的家:例如。drwxr-xr-x 因此,在之前的答案中,人们没有意识到您可能遇到了什么。
With more restricted settings like what I had on my web host, nonowner users cannot do anything eg. drwx------ Even if you use su/sudo and become the other user, you can still only be ONE USER at one time, so when you copy the file back, the same problem of no enough permission still apply.
有了更多限制设置,比如我在我的网络主机上的设置,非所有者用户不能做任何事情,例如。drwx------ 即使您使用su/sudo并成为其他用户,您仍然一次只能是一个用户,因此当您将文件复制回来时,同样存在权限不足的问题。
So. . . use scp instead, treat the whole thing like a network environment let me put it that way and that's it. By the way this question had already been answered once over here (https://superuser.com/questions/353565/how-do-i-copy-a-file-folder-from-another-users-home-directory-in-linux), only cared to reply because this ranked 1st result from my search.
所以。. . 使用 scp 代替,将整个事情视为网络环境,让我这样说,就是这样。顺便说一下,这个问题已经在这里得到了回答(https://superuser.com/questions/353565/how-do-i-copy-a-file-folder-from-another-users-home-directory-in -linux),只关心回复,因为这在我的搜索中排名第一。
回答by Rolf of Saxony
sudo cp -a /home/user1/folder1 /home/user2/folder2
sudo chown -R user2:user2 /home/user2/folder2
sudo cp -a /home/user1/folder1 /home/user2/folder2
sudo chown -R user2:user2 /home/user2/folder2
cp -a archive
chown -R act recursively
cp -a archive
chown -R 递归执行
Copies the files and then gives permissions to user2 to be able to access them.
Copies all files including dot files, all sub-directories and does not require directory /home/user2/folder2 to exist prior to the command.
复制文件,然后授予 user2 访问权限。
复制包括点文件在内的所有文件、所有子目录,并且不需要在命令之前存在目录 /home/user2/folder2。
回答by CR7
I think you need to use this command
我认为你需要使用这个命令
sudo -u username /path1/file1 /path2/file2
This command allows you to copy the contents as a particular user from any file path.
此命令允许您以特定用户的身份从任何文件路径复制内容。
PS: The parent directory should be list-able at least in order to copy files from it.
PS:父目录至少应该是可列出的,以便从中复制文件。