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/folder1 to /home/user2/folder2, using one single shell script (one single script launching, without manually switching of users).
如何使用一个shell脚本(一个脚本启动,无需手动切换用户)将文件从/ home / user1 / folder1复制到/ home / user2 / folder2。
I think I must use a sudo command but didn't found how exactly.
我想我必须使用sudo命令,但没有找到确切的结果。
4 个解决方案
#1
3
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(所以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复制以包含隐藏文件和隐藏目录及其内容?供进一步参考。
#2
3
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访问权限的权限。复制所有文件,包括点文件,所有子目录,并且在命令之前不需要存在directory / home / user2 / folder2。
#3
1
(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 -)
#4
-4
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.
我相信默认情况下,家庭许可允许所有人列出并将工作目录更改为另一个人的家:例如。 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),只关心回复,因为这是我的搜索排名第一的结果。
#1
3
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(所以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复制以包含隐藏文件和隐藏目录及其内容?供进一步参考。
#2
3
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访问权限的权限。复制所有文件,包括点文件,所有子目录,并且在命令之前不需要存在directory / home / user2 / folder2。
#3
1
(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 -)
#4
-4
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.
我相信默认情况下,家庭许可允许所有人列出并将工作目录更改为另一个人的家:例如。 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),只关心回复,因为这是我的搜索排名第一的结果。