LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 05-08-2009, 03:25 AM   #1
packets
Member
 
Registered: Oct 2005
Posts: 280

Rep: Reputation: 30
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

#!/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/new
mkdir /emails/$letter/$user/cur
mkdir /emails/$letter/$user/tmp
echo "CHECKING T"
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
echo "CHECKING N"
fi

done


done
 
Old 05-08-2009, 03:39 AM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,458

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
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:
Code:
#!/bin/bash
while read user
do
  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
packets
Member
 
Registered: Oct 2005
Posts: 280

Original Poster
Rep: Reputation: 30
@colucix

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
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 240Reputation: 240Reputation: 240
if you have Python, here's an alternative solution
Code:
#!/usr/bin/env python
import os
directory="/emails"
for line in open("/etc/passwd"):
    line=line.strip().split(":")
    if "home" in line[5]:
        letter=line[0][0]
        os.makedirs(os.path.join(directory,letter,line[0]))
 
  


Reply


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


All times are GMT -5. The time now is 08:01 AM.

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