Bash Shell Tip: Copy Set of Files to All Users Home Directory
If you would like to copy a set of files for all existing users, use the following scripting trick. It will save lots of manual work.
First you need to grab all user names from /etc/passwd file using cut comand:
cut -d: -f1 /etc/passwd
Next use shell for loop command to apply copy and set the correct permissions on the file.
Finally, id command is used to obtain the correct user ID and group ID for each user.
Copy Single File To All Users Home Directory
#!/bin/bash
UHOME="/home"
FILE="/etc/skel/.newconfig-file"
USERS=$(cut -d':' -f1 /etc/passwd) # get list of all users
for u in $USERS
do
/bin/cp $FILE ${UHOME}/${u}
chown $(id -un $u):$(id -gn $u) /${UHOME}/${u}/${FILE}
done
Copy Multiles File To All Users Home Directory
You can also copy multiple files using inner and outer loop concept:
UHOME="/home"
FILES="/etc/skel/.newconfig-file /etc/skek/.update-config /chroot/jail/.force.conf"
USERS=$(cut -d':' -f1 /etc/passwd) # get list of all users
for u in $USERS
do
for f in $FILES
do
/bin/cp ${f} ${UHOME}/${u}
chown $(id -un $u):$(id -gn $u) /${UHOME}/${u}/${f}
done
done
As pointed out by our readers (see comments below), you need to add additional security check such as
- User should be a normal user
- User must have a directory
- User must have a valid password / account.
This is left as exercise for the reader.
Updated for accuracy.
Want to stay up to date with the latest Linux tips, news and announcements? Subscribe to our free e-mail newsletter or RSS feed to get all updates.
You can Email this page to a friend.
You may also be interested in other helpful articles:
- Howto add pause prompt in a shell script ( bash pause command )
- Linux: How to create multiple users accounts in batch
- How to: Install Ubuntu Linux on a flash drive and run from Windows
- nixCraft FAQ Roundup
- Linux: Recovering deleted /etc/shadow password file
Discussion on This Article:
Leave a Reply
We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!
Tags: batch copy, chown, copy multipe files, correct permissions, existing users, group id, home directory, loop command, multiple files, outer loop, shell scripts, single file, user names



This will also copy files to home directory of system users (not all of them have home directories).
This will prevent that this happens.
ID=$(id -u $u)
if [ $ID -ge 1000 ] ; then
if [ -d ${UHOME}/${u} ] ; then
… do copy here….
fi
fi
Casper,
Yeah, you got a valid point there.
Appreciate your post.
marinosi@lucifer:~$ id -u nobody
65534
I’m pretty sure you don’t want to copy something to the nonexistent nobody’s homedir.
Someone , could have in the system other accounts that cannot login (or don’t have a home dir etc)..One way to find the active users of a system is :
$ cat /etc/shadow | grep -v ‘[*!]‘
Users who are inactive(have disabled accounts ) have a * or $ (these users cannot login) at the field where they should have the password hash.
Ilias,
The post has been updated. Thanks for sharp observation.
Instead of using cp… chown… use the install command. It does the same thing.
If you don’t have your users in /etc/passwd, but for instance in ldap, you can use `getent passwd` instead.
You also need to safely handle pre-existing files of the same name. You can use the –backup option with install or cp, but you probably want to log it and/or inform users in some way.