3

I'm running an Ubuntu LTSP setup at a school with about 60 unique users. Occasionally, we need to share a document, create a directory or place a config file in every user's account. Obviously, it's not efficient to do this one at a time.

I know I can place a file in every user's home directory with:

ls /home/ | xargs -n 1 sudo cp -i <file>

But what if I need to put it somewhere specific, such as ~/.config/autostart?

Or what if I need to create the directory ~/Desktop/foo/ for every user?

Thanks for your help, and if anyone can suggest resources for me to learn more that would be awesome.

muru
  • 193,181
  • 53
  • 473
  • 722
user244998
  • 33
  • 5
  • Should the action(s) as described (create a file/create directory etc) take place while users are logged in? If not there is a simple option; create a launcher in `/etc/xdg/autostart` to run a single script, centrally managed. The script will then run locally for each and every user that logs in. You can make the script do anything, and there will be no permnission issues, since the script then runs as a process from the local user. – Jacob Vlijm Jun 19 '15 at 16:21

1 Answers1

5

cp has an option to specify the target directory separately: -t. So you can do:

for u in /home/*
do
    sudo cp -t "$u/.config/autostart" -i <file>
    sudo mkdir "$u/Desktop/foo"
done

In general, there's no simple way to manage user's home directories. You can specify what gets created in it when the home directory is first created, but after that, it's each user for itself.

Then, you'd have to use some form of scripting. In this case, I have used shell scripting. Check out the TLDP guides on Bash and scripting in Bash. Even with tools like Puppet, this is not a trivial task.

muru
  • 193,181
  • 53
  • 473
  • 722
  • would there be any issues with permissions and file/directory ownership ? – Sergiy Kolodyazhnyy Jun 19 '15 at 14:38
  • @Serg of course, all these files and directories would be owned by root and mostly uneditable by the users. But then again, I suppose that's what an admin wants. – muru Jun 19 '15 at 14:39
  • 1
    i think for files that are in `~/.config/autostart` that's not a big deal. For files that are only meant for reading - also not a big deal, but in case it's something they want users to edit, I'd probably extract username from the `/home/username` path, and add `chown username:username`. am I right ? – Sergiy Kolodyazhnyy Jun 19 '15 at 14:44
  • 2
    Indeed. Ur use `sudo -u username cp/mkdir ...`. – muru Jun 19 '15 at 14:46
  • 1
    To expand: Debian/Ubuntu `adduser` and `useradd` can have custom files added to `/etc/skel`. You can also look at `/etc/adduser.conf` for some more options for stuff to put into new user directories. – Rylee Fowler Jun 19 '15 at 15:14