Copy files from path to path same structure for each user

Copy files from path to path same structure for each user

How to copy single file for example index.html

from source

/backup/cpbackup/daily/a1/homedir/public_html/index.html
/backup/cpbackup/daily/b2/homedir/public_html/index.html
/backup/cpbackup/daily/c3/homedir/public_html/index.html
/backup/cpbackup/daily/d4/homedir/public_html/index.html
/backup/cpbackup/daily/e5/homedir/public_html/index.html
/backup/cpbackup/daily/f6/homedir/public_html/index.html

to destination

/home/a1/public_html/index.html
/home/b2/public_html/index.html
/home/c3/public_html/index.html
/home/c4/public_html/index.html
/home/e5/public_html/index.html
/home/f6/public_html/index.html

cp command can't do this to copy all files to the same directory for each user , so how can i do it ?

答案1

You could use a shell loop (copy/paste this into a terminal window):

for user in a1 b2 c3 c4 e5 f6; do
 cp -p "/backup/cpbackup/daily/$user/homedir/public_html/index.html" \
    "/home/$user/public_html/";
done

The -p flag makes cp preserve the attributes (owner,group, timestamp) of the copied files.

If you want to do this for all users (i.e. all sub directories of /backup/cpbackup/daily/) you can do something like this which avoids manually entering the usernames:

for dir in /backup/cpbackup/daily/*; do user=$(basename $dir);
    cp -p "/backup/cpbackup/daily/$user/homedir/public_html/index.html" \
    "/home/$user/public_html/";
done

相关内容