• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 280
  • Last Modified:

Change home directory - linux

I have users whose home directory is /export/home/$user. I want to change this to /home/$user. I know that the command below works

# usermod -d /home/  USER

I can issue the command and get it changed, But what if I have 200 users. Is this syntax correct

# usermod -d /home `cat /etc/passwd | cut -d":" -f1`

Thanks
0
mnis2008
Asked:
mnis2008
  • 2
1 Solution
 
farzanjCommented:
The command is wrong

It should be
usermod -d /home/$user $user

Run the following script

Users=$(cut -d: -f1 /etc/passwd)

for User in $Users
do
usermod -d /home/$User $User
done
0
 
farzanjCommented:
To check if the command is working as per your expectation, first do this

Users=$(cut -d: -f1 /etc/passwd)

for User in $Users
do
echo usermod -d /home/$User $User
done

This way you will be able to see the entire set of commands then remove echo
0
 
wesly_chenCommented:
If all the home directories are defined in /etc/passwd, then
# sed "s/\/export\/home/\/home/g" /etc/passwd > /etc/passwd.new

Once verify /etc/passwd.new ok, then
# cp /etc/passwd /etc/passwe.backup
# cp /etc/passwd.new /etc/passwd
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now