Review your favorite Linux distribution.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 05-08-2009, 03:25 AM   #1
Registered: Oct 2005
Posts: 321

Rep: Reputation: 32
how to change directory using bash or other simple language

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


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/new
mkdir /emails/$letter/$user/cur
mkdir /emails/$letter/$user/tmp
usermod -d /emails/$letter/$user $user
elif [ "$letter" == n ]; then
mkdir /emails/$letter/$user
mkdir /emails/$letter/$user/new
mkdir /emails/$letter/$user/cur
mkdir /emails/$letter/$user/tmp
usermod -d /emails/$letter/$user $user


Old 05-08-2009, 03:39 AM   #2
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Don't use two for loops, otherwise for each letter a new home directory is created for all the users under each letter. Just loop over the users and extract the first letter from the username inside the loop. Actually you don't need neither the if/elif statement, until the commands are the same for all the letters:
while read user
  letter=$(echo $user | cut -c1)
  mkdir -p /emails/$letter/$user/new /emails/$letter/$user/cur /emails/$letter/$user/tmp
  echo "CHECKING T"
  usermod -d /emails/$letter/$user $user
done < <(awk -F: '/home/{print $1}' /etc/passwd)
Old 05-08-2009, 03:46 AM   #3
Registered: Oct 2005
Posts: 321

Original Poster
Rep: Reputation: 32

Thanks for the reply. Will post here if I encounter some problem.

I'm still checking the scripts for some additional commands. It was such a headache migrating to new mail servers
Old 05-08-2009, 04:28 AM   #4
Senior Member
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
if you have Python, here's an alternative solution
#!/usr/bin/env python
import os
for line in open("/etc/passwd"):
    if "home" in line[5]:


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
change default bash directory in gnome-terminal ming0 Linux - Software 5 10-01-2009 06:54 PM
Bash change the items in comparisons with simple front end geeyathink Programming 3 03-04-2008 09:53 PM
Unable to change directory on terminal using bash script gregorian Linux - Newbie 11 02-05-2007 03:17 AM
under kde3 i change change language, where in gnome? sirpelidor Mandriva 1 12-10-2003 04:39 PM
Simple graphical linux programming language? Nerd2 Linux - Software 4 05-03-2003 07:12 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:46 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration