Find the answer to your Linux question:
Results 1 to 2 of 2
I was task to change the home dir of the thousand of users in my server. The new directory will be according to the first letter of their username. If ...
  1. #1
    Linux Newbie
    Join Date
    Mar 2006
    Posts
    101

    bash script to change home directory

    I was task to change the home dir of the thousand of users in my server. The new directory will be according to the first letter of their username. If my username is nelson, the home dir must be /emails/n/nelson. If the login name is fred, the home dir must be /emails/f/fred.

    As of the moment, current dir was only located at /home dir such as /home/fred.

    Can anyone give a hint on how would I do this? My problem is getting the first character of their username and putting it in their designate folder which was also according to the first characted of their domain.

    I tried this script but I encounter a problem

    #!/bin/bash

    USER=`cat /etc/passwd | grep home | awk -F: '{print $1}'`
    LETTER=`cat /etc/passwd | grep home | sed -r 's/(.)[^.]*\.?/\L\1/g'`

    for letter in $LETTER;do
    for user in $USER;do
    if [ "$letter" == t ]; then
    mkdir /emails/$letter/$user
    mkdir /emails/$letter/$user/Maildir
    mkdir /emails/$letter/$user/Maildir/new
    mkdir /emails/$letter/$user/Maildir/cur
    mkdir /emails/$letter/$user/Maildir/tmp
    echo "CHECKING T"
    usermod -d /emails/$letter/$user $user
    elif [ "$letter" == n ]; then
    mkdir /emails/$letter/$user
    mkdir /emails/$letter/$user/Maildir/new
    mkdir /emails/$letter/$user/Maildir/cur
    mkdir /emails/$letter/$user/Maildir/tmp
    usermod -d /emails/$letter/$user $user
    echo "CHECKING N"
    fi

    done


    done

  2. #2
    Just Joined! cfajohnson's Avatar
    Join Date
    May 2007
    Location
    Toronto, Canada
    Posts
    52
    Code:
    while IFS=: read user junk
    do
      udir=/emails/${user:0:1}/$user
      mkdir -p "$udir/"{new,cur,tmp}
      usermod -d "$udir" "$user"
    done < /etc/passwd

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...