how to change users home directories
I want to change a bunch of users home directories from /home and move them to /home/year7 but I want the username, password and directory content to remain the same. I can edit /etc/passwd and change to the new directory but how do I make the changes take effect? Unfortunately I can't use yast to do this. Thanks, Steve.
On Thu, Jan 29, 2004 at 06:18:40PM +0000, steve-ss wrote:
I want to change a bunch of users home directories from /home and move them to /home/year7 but I want the username, password and directory content to remain the same. I can edit /etc/passwd and change to the new directory but how do I make the changes take effect? Unfortunately I can't use yast to do this. Thanks, Steve.
something like: cd /homr for file in *; do if [ ! -e year7 ] ; then mkdir year7 fi mv $file year7/. done then edit /etc/passwd
-- Check the headers for your unsubscription address For additional commands send e-mail to suse-linux-e-help@suse.com Also check the archives at http://lists.suse.com Please read the FAQs: suse-linux-e-faq@suse.com
-- Jeff Kinz, Open-PC, Emergent Research, Hudson, MA. "jkinz@kinz.org" is copyright 2003. Use is restricted. Any use is an acceptance of the offer at http://www.kinz.org/policy.html.
From: Jeff Kinz [mailto:jkinz@kinz.org] Sent: Thursday, January 29, 2004 9:33 AM To: suse-linux-e@suse.com Subject: Re: [SLE] how to change users home directories
I want to change a bunch of users home directories from /home and move them to /home/year7 but I want the username, password and directory content to remain the same. I can edit /etc/passwd and change to the new
On Thu, Jan 29, 2004 at 06:18:40PM +0000, steve-ss wrote: directory but
how do I make the changes take effect? Unfortunately I can't use yast to do this. Thanks, Steve.
something like:
cd /homr for file in *; do if [ ! -e year7 ] ; then mkdir year7 fi mv $file year7/. done
then edit /etc/passwd
Could you help explain the script ? the "for file in *" seems like it would move every username in /home to /home/year7 ? (why not just do: cd /home; make year7; mv * year7/ ) ? I am probably reading your script wrong so if you could help out that'd be awesome. Anyway, if the OP only wants to move specified accounts and not everyone in /home, here is another possible way edit the /etc/passwd file first. Change the /home dirs. assuming "year7" only shows up in your /etc/passwd file for those users whose accounts are in /home/year7/[username] (e.g. there is no username "year7" or somehting like that). (grep year7 passwd | awk -F: '{print "mv /home/"$1" /home/year7/"$1}') | cat This will print out what the script is going to do. If it looks good, you can then redirect to a file and run as a shell script OR change "cat" to "/bin/sh" or "/bin/bash" and it will then do the actual "mv "commands. Ben
On Thursday 29 January 2004 19:14, Ben Yau wrote:
From: Jeff Kinz [mailto:jkinz@kinz.org] Sent: Thursday, January 29, 2004 9:33 AM To: suse-linux-e@suse.com Subject: Re: [SLE] how to change users home directories
On Thu, Jan 29, 2004 at 06:18:40PM +0000, steve-ss wrote:
I want to change a bunch of users home directories from /home
and move them
to /home/year7 but I want the username, password and directory
content to
remain the same. I can edit /etc/passwd and change to the new
directory but
how do I make the changes take effect? Unfortunately I can't
use yast to do
this. Thanks, Steve.
something like:
cd /homr for file in *; do if [ ! -e year7 ] ; then mkdir year7 fi mv $file year7/. done
then edit /etc/passwd
Could you help explain the script ? the "for file in *" seems like it would move every username in /home to /home/year7 ?
(why not just do: cd /home; make year7; mv * year7/ ) ? I am probably reading your script wrong so if you could help out that'd be awesome.
Anyway, if the OP only wants to move specified accounts and not everyone in /home, here is another possible way
edit the /etc/passwd file first. Change the /home dirs. assuming "year7" only shows up in your /etc/passwd file for those users whose accounts are in /home/year7/[username] (e.g. there is no username "year7" or somehting like that).
(grep year7 passwd | awk -F: '{print "mv /home/"$1" /home/year7/"$1}') | cat
This will print out what the script is going to do. If it looks good, you can then redirect to a file and run as a shell script OR change "cat" to "/bin/sh" or "/bin/bash" and it will then do the actual "mv "commands.
Ben
Hi and thanks for the tips. I changed it to: (grep year7 /etc/passwd | awk -F: '{print "mv /home/"$1" /home/year7/"$1}') | cat I don't understand it but it runs OK. Is there a quick one liner to explain how it works? Thanks again, Steve.
Hi and thanks for the tips. I changed it to:
(grep year7 /etc/passwd | awk -F: '{print "mv /home/"$1" /home/year7/"$1}') | cat
I don't understand it but it runs OK. Is there a quick one liner to explain how it works?
Steve, The English-language version reads something like .... Find every instance of the string "year7" in the file /etc/passwd and for each one you find make a command line which moves will move the home directory to /home/year7, then show me all these command lines on the screen. As Ben said running this will only show you what would be done. Make the modifications he talked about to actually action this. Damian
On Thursday 29 January 2004 21:56, steve-ss wrote:
(grep year7 /etc/passwd | awk -F: '{print "mv /home/"$1" /home/year7/"$1}') | cat I don't understand it but it runs OK. Is there a quick one liner to explain how it works? Thanks again, Steve.
Okay, here we go [afaik :)] grep is a powerful pattern matching utility: #grep year7 /etc/passwd goes through the file /etc/passwd and looks for the string "year7" and prints it to "stdout" [i.e. standard output so other programs can process the info, or display on screen] The " | " is known as a pipe through - basically, run the program to the left and feed the output to the program on the right. awk is like grep iirc, but more powerful. the -F: runs a program, and the stuff after that in the apostrophes is the program itself. It prints: mv /home/ The $1 indicates take the first field from the output line from the previous program and insert it here, so if your paswd file had "bob" as the first field, then it would print "mv /home/bob" :) If you follow in the same vein for the rest of it, then you get: mv /home/firstfield /home/year7/firstfield then the lot is "piped through" cat which just displays it to the screen [like "type" on dos/windows] Hopefully I haven't confused you more :g: Jon
On Friday 30 January 2004 09:21, The Purple Tiger wrote:
On Thursday 29 January 2004 21:56, steve-ss wrote:
(grep year7 /etc/passwd | awk -F: '{print "mv /home/"$1" /home/year7/"$1}') | cat I don't understand it but it runs OK. Is there a quick one liner to explain how it works? Thanks again, Steve.
Okay, here we go [afaik :)] grep is a powerful pattern matching utility:
#grep year7 /etc/passwd goes through the file /etc/passwd and looks for the string "year7" and prints it to "stdout" [i.e. standard output so other programs can process the info, or display on screen] The " | " is known as a pipe through - basically, run the program to the left and feed the output to the program on the right.
awk is like grep iirc, but more powerful. the -F: runs a program, and the stuff after that in the apostrophes is the program itself. It prints: mv /home/ The $1 indicates take the first field from the output line from the previous program and insert it here, so if your paswd file had "bob" as the first field, then it would print "mv /home/bob" :)
If you follow in the same vein for the rest of it, then you get: mv /home/firstfield /home/year7/firstfield
then the lot is "piped through" cat which just displays it to the screen [like "type" on dos/windows]
Hopefully I haven't confused you more :g:
Jon
Now THIS is what i like!!! A short description what the fix/scrip/sollution does. It helps us illiterates to understand whats going on. I usaly dont run unexplained/undocumented scripts unless i get a halfdecent understanding of what it does. The way Jon explained the script is exemplary! VERY GOOD WORK!! -- /Rikard ------------------------------------------------------------------------------------ Rikard Johnels email : rikjoh@norweb.se Web : http://www.rikjoh.com Mob : +46 (0)735 05 51 01 ------------------------ Public PGP fingerprint ---------------------------- < 15 28 DF 78 67 98 B2 16 1F D3 FD C5 59 D4 B6 78 46 1C EE 56 >
grep is a powerful pattern matching utility:
#grep year7 /etc/passwd goes through the file /etc/passwd and looks for the string "year7" and prints it to "stdout" [i.e. standard output so other programs can process the info, or display on screen] The " | " is known as a pipe through - basically, run the program to the left and feed the output to the program on the right.
awk is like grep iirc, but more powerful. the -F: runs a program, and the stuff after that in the apostrophes is the program itself. It prints: mv /home/ The $1 indicates take the first field from the output line from the previous program and insert it here, so if your paswd file had "bob" as the first field, then it would print "mv /home/bob" :)
If you follow in the same vein for the rest of it, then you get: mv /home/firstfield /home/year7/firstfield
then the lot is "piped through" cat which just displays it to the screen [like "type" on dos/windows]
Hopefully I haven't confused you more :g:
Jon
Now THIS is what i like!!! A short description what the fix/scrip/sollution does. It helps us illiterates to understand whats going on. I usaly dont run unexplained/undocumented scripts unless i get a halfdecent understanding of what it does. The way Jon explained the script is exemplary!
VERY GOOD WORK!!
Rikard, I also replied to this with something similar but heaven knows where that mail is sitting now ... Only one correction... the -F: actully tells awk to use the : character as a field seperator (as opposed to a space character that we normally use). Awk is way more powerful than grep. Iif you can find out what the initials AWK stand for then that'll give you a clue why. Damian
On Fri, 2004-01-30 at 07:07, Damian O'Hara wrote:
Rikard,
I also replied to this with something similar but heaven knows where that mail is sitting now ...
Only one correction... the -F: actully tells awk to use the : character as a field seperator (as opposed to a space character that we normally use). Awk is way more powerful than grep. Iif you can find out what the initials AWK stand for then that'll give you a clue why.
They stand for the initials of the developers of AWK. -- Ken Schneider unix user since 1989 linux user since 1994 SuSE user since 1998 (5.2)
On Friday 30 January 2004 12:07, Damian O'Hara wrote:
Only one correction... the -F: actully tells awk to use the : character as a field seperator (as opposed to a space character that we normally use). Ahhh :) Thank you! I will add that to my reference list :g:
I am just trying to think what program I got that switch confused with.. hrmm... *thinks*.
Awk is way more powerful than grep. Iif you can find out what the initials AWK stand for then that'll give you a clue why. I thought they were the initials of the founders of that proggie? I shall do some research methinks :)
Thank you for the correction to my explanation Damian - I appreciate it :) Jon
(grep year7 passwd | awk -F: '{print "mv /home/"$1" /home/year7/"$1}') | cat
This will print out what the script is going to do. If it
looks good, you
can then redirect to a file and run as a shell script OR change "cat" to "/bin/sh" or "/bin/bash" and it will then do the actual "mv "commands.
Ben
Hi and thanks for the tips. I changed it to:
(grep year7 /etc/passwd | awk -F: '{print "mv /home/"$1" /home/year7/"$1}') | cat
Good idea on the "/etc/passwd". Not a quick one liner explanation, but here is a brief explanation. If you do a man on grep and awk, you will get the complete info . The short version is: grep searchstring file will look in "file" and print out every line that has searchstring in it. There are a lot of args you can use (case sensitive, etc.). The | is a pipe . It will use the standard output of the previous command as standard input of the next command awk '{print $1}' on it's own will print out the first column of the output using whitespace as a delimiter. to print out the second column, use $2, third column $3, and last column $NF. I usually use awk as part of a pipe. For example: # echo "col1 col2 col3" | awk '{print $1}' col1 # echo "col1 col2 col3" | awk '{print $2}' col2 # echo "col1 col2 col3" | awk '{print $NF}' col3 # To change the delimiter from white space to something else, use -F option followed by the character you want as a delimiter. In the case of /etc/passwd, we want to use the colon as the delimiter. Try the below to see what you get. cat /etc/passwd | awk -F: '{print $1}' cat /etc/passwd | awk -F: '{print $2}' cat /etc/passwd | awk -F: '{print $NF}' (Note: similar to how you can specify a filename in the "grep" command, you can do the same thing with awk. cat /etc/passwd | awk -F: '{print $1}' and awk -F: '{print $1}' /etc/passwd will give the same results. Now the print utility is very strong in awk. You can specify other strings to print out literally with the double quote. Anything in double quotes is printed out. So you can print out echo "file1 file2" | awk '{print "mv "$1" to "$file2}' The trickiest part of awk and printing is keeping track of your double quotes. That's the basics. Hope that helps. Ben Yau
On Friday 30 January 2004 16:17, Ben Yau wrote:
(grep year7 passwd | awk -F: '{print "mv /home/"$1" /home/year7/"$1}') | cat
This will print out what the script is going to do. If it
looks good, you
can then redirect to a file and run as a shell script OR change "cat" to "/bin/sh" or "/bin/bash" and it will then do the actual "mv "commands.
Ben
Hi and thanks for the tips. I changed it to:
(grep year7 /etc/passwd | awk -F: '{print "mv /home/"$1" /home/year7/"$1}') | cat
Good idea on the "/etc/passwd". Not a quick one liner explanation, but here is a brief explanation. If you do a man on grep and awk, you will get the complete info . The short version is:
grep searchstring file
will look in "file" and print out every line that has searchstring in it. There are a lot of args you can use (case sensitive, etc.).
The | is a pipe . It will use the standard output of the previous command as standard input of the next command
awk '{print $1}'
on it's own will print out the first column of the output using whitespace as a delimiter. to print out the second column, use $2, third column $3, and last column $NF. I usually use awk as part of a pipe. For example:
# echo "col1 col2 col3" | awk '{print $1}' col1 # echo "col1 col2 col3" | awk '{print $2}' col2 # echo "col1 col2 col3" | awk '{print $NF}' col3 #
To change the delimiter from white space to something else, use -F option followed by the character you want as a delimiter. In the case of /etc/passwd, we want to use the colon as the delimiter. Try the below to see what you get.
cat /etc/passwd | awk -F: '{print $1}' cat /etc/passwd | awk -F: '{print $2}' cat /etc/passwd | awk -F: '{print $NF}'
(Note: similar to how you can specify a filename in the "grep" command, you can do the same thing with awk. cat /etc/passwd | awk -F: '{print $1}'
and
awk -F: '{print $1}' /etc/passwd
will give the same results.
Now the print utility is very strong in awk. You can specify other strings to print out literally with the double quote. Anything in double quotes is printed out. So you can print out
echo "file1 file2" | awk '{print "mv "$1" to "$file2}'
The trickiest part of awk and printing is keeping track of your double quotes. That's the basics.
Hope that helps.
Utterly and 100%'ly. You guys have saved me so much time. Next round on me. Cheers, Steve.
steve-ss wrote:
I want to change a bunch of users home directories from /home and move them to /home/year7 but I want the username, password and directory content to remain the same. I can edit /etc/passwd and change to the new directory but how do I make the changes take effect? Unfortunately I can't use yast to do this. Thanks, Steve.
You have to both change the password file and move the directories.
participants (8)
-
Ben Yau
-
Damian O'Hara
-
James Knott
-
Jeff Kinz
-
Kenneth Schneider
-
Rikard Johnels
-
steve-ss
-
The Purple Tiger