Easily Set Home Directory Ownership for Multiple Users In Linux Using This Shell Script
| |
Today I was in a situation where I had over 300 home directories that had been copied from an older Linux machine. The user accounts had already been created on the new machine using Webmin’s batch account creation feature, but were not loaded using the same UID as the original server. Fortunately the home names did reflect the user name, as I was able to use this little bit of shell command line to set the ownership of each home directory to the newly created accounts.
You could also set group ownership, as well as permissions at the same time. This little bit of code will obtain the name of each directory within the /home directory and assign it as the variable $user. It then recursively chowns each directory to the user with the same name as of the directory. If your directory contained three home folders for mark, joe and sam, the script would do this. You could use this same loop to set permissions, chgrp (group ownership), move or rename directories and so on. Its simple, yet very handy at times. Once again the command line proves more powerful than the GUI in Linux! Since this was my first Linux post I thought I would post something quick and easy, but very handy just the same (for me anyhow). I hope that someone else in a similar situation may find this useful also. |


T. J. Hooker:
If you used Webmin to create the accounts you could have also selcted the option to not create home directories, but change ownership and permissions. You would have to make sure the home directory path was correct on your delimited file and the home folders were in place prior to import. I have done this before and it worked smooth.
Ciao
12 September 2008, 2:46 pmMark:
Thanks T.J. yes Webmin is slick for administering a large number of accounts. At first I was going to export the accounts (as they had already been created when I arrived) using Webmin and use the batch modify option. I figured I would try to do it like this first and then use Webmin if I couldn’t figure it out in the shell.
Mark
12 September 2008, 4:40 pmShane England:
Cool, I just used this script to populate public_html directories with dummy templates. I could not figure out how to copy it in, wildcards did not work.
cp index.html /home/*/public_html/This worked like a charm.
#!/bin/bash
cd /home
for user in `ls -1`;
do cp index.html /home/$user/public_html
done
Thanks, its almost too simple!
1 October 2008, 6:29 pm