Unfortunately sudo wants a program (+arguments) as a parameter, not a piece of shell script, which makes it difficut (not impossible) to do on one line
You could:
a) issue a 'su' command to change to superuser first, don't know if that will be successful on OS X Server as, by default, Administrators are on allowed to go to superuser.
b) change this into a simple shell script and then run that with sudo
Just breaking things down so you understand what is happening, if we take a simple version of the command (which also is not going to change anything!):
for i in `ls /Users`; do echo "$i"; done
which returns (on my server which only has one user)
Administrator
Shared
each ";" acts as a command separator. Also you can use the output of any UNIX / Linux command as list of values to the for loop by enclosing the command in back-ticks ` `.
Going further with building the command (again without changing anything):
for i in `ls /Users`; do ls -la "/Users/$i"; done
the $i is substituted by the output of the first command which, as you see, is the name of the folders after /Users/
Here is the same (harmless) command converted to a shell script
#!/bin/bash
for i in `ls /Users`
do
ls -la "/Users/$i"
done
Here is the command to change user directory & file ownership in a shell script - note this will execute the changes.
#!/bin/bash
for i in `ls /Users`
do
chown -R $i "/Users/$i"
done
This is what I suggest you do:
- Look at the interactive output of terminal command: sudo ls /Users do you only see the users you want to be affected being listed? If yes then you can proceed with the automated update.
- Are the Unix user names the same as the folder names? This is critical. If the Unix user name is "fred.bloggs" and the users' directory name is "fbloggs" then obviously the command will not work because the 'chown -R' command is going to recursively change ownership of directories & files to user "fred.bloggs". Do you understand this point?
- Make an executable script file - I suggest first a test one with my (harmless) script then the action one.
Assuming the harmless one, in terminal do
- touch test (this will create file)
- nano test (to edit it)
- copy / paste 5 lines (including #!/bin/bash) into file
- save it
- chmod 774 test (to make it executable)
- sudo ./test (to run it, the './' means current directory)
Look at the output, if you are happy step two is to make automated script and TO BE SAFE only change one test user. In step 1 above change to 'touch UpdateUser' to create a file called UpdateUser (for rest of commands change 'test' to 'UpdateUser') and copy paste this below where 'SomeTestUserName' is the name of one users' directory:
#!/bin/bash
for i in SomeTestUserName
do
chown -R $i "/Users/$i"
done
If you are happy with results of script change second line to
for i in `ls /Users`
save script, execute it.
Of course, as usual, this is at your risk.