nixCraft Poll

Topics

Bash Shell Tip: Copy Set of Files to All Users Home Directory

Posted by Vivek Gite [Last updated: January 3, 2008]

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

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:

Discussion on This Article:

  1. Casper Pedersen Says:

    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

  2. vivek Says:

    Casper,

    Yeah, you got a valid point there.

    Appreciate your post.

  3. Ilias Marinos Says:

    marinosi@lucifer:~$ id -u nobody
    65534

    I’m pretty sure you don’t want to copy something to the nonexistent nobody’s homedir. :-P

    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.

  4. vivek Says:

    Ilias,

    The post has been updated. Thanks for sharp observation.

  5. Jeff Schroeder Says:

    Instead of using cp… chown… use the install command. It does the same thing.

  6. Vincent Says:

    If you don’t have your users in /etc/passwd, but for instance in ldap, you can use `getent passwd` instead.

  7. Alex Gretlein Says:

    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.

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!

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Tags: , , , , , , , , , , , ,

Copyright © 2004-2008 nixCraft. All rights reserved - TOS/Disclaimer - Privacy policy - Sitemap - Powered by Open source software.