Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy files from one user home directory to another user home directory in Linux

I have the logins and passwords for two linux users (not root), for example user1 and 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).

I think I must use a sudo command but didn't found how exactly.

like image 811
kirpi4 Avatar asked Sep 18 '13 09:09

kirpi4


2 Answers

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

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

like image 153
fedorqui 'SO stop harming' Avatar answered Oct 12 '22 14:10

fedorqui 'SO stop harming'


sudo cp -a /home/user1/folder1 /home/user2/folder2
sudo chown -R user2:user2 /home/user2/folder2

cp -a archive
chown -R act recursively

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.

like image 41
Rolf of Saxony Avatar answered Oct 12 '22 15:10

Rolf of Saxony