Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 12
Hi there, I am trying to do somehting similar, but on a wider scale. I am trying to write a script that would open the home directory, open the first ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Apr 2005
    Posts
    6

    help writing script to read files names


    Hi there,
    I am trying to do somehting similar, but on a wider scale.
    I am trying to write a script that would open the home directory, open the first (of 650) user's folder
    open the ?mail directory, which every user has

    Then I need the script to read each of the files and folder names with one preceding directory and a carriage return as the delimiter.
    Then the output is to be put into a file called .mailboxlist


    Code:
    cd /home
    cd user1
    cd mail
    grep ls (this is where I am most stuck – adding the ‘mail/’ before each name and a carriage return as the delimiter)  > .mailboxlist  (??)
    cp .mailboxlist /home/user1/.mailboxlist

    The file “.mailboxlist” should look like this:
    mail/Sent
    mail/Trash
    mail/Drafts
    mail/tech support
    mail/test1


    Any help would be greatly appreciated.
    Thanks,
    Nett

  2. #2
    Linux Guru lakerdonald's Avatar
    Join Date
    Jun 2004
    Location
    St. Petersburg, FL
    Posts
    5,035
    so do you want to do this for each user?
    Code:
    #!/bin/bash
    cd /home
    for user in `ls`
    do
    cd $user/mail
    for dir in `ls`
    do
    echo mail/$dir >> /home/$user/.mailboxlist
    done
    cd /home
    done

  3. #3
    Just Joined!
    Join Date
    Apr 2005
    Posts
    6
    Yes, I need this process to be repeated for every (about 650) user folder.
    Thansk for your help!
    nett

  4. #4
    Linux Guru lakerdonald's Avatar
    Join Date
    Jun 2004
    Location
    St. Petersburg, FL
    Posts
    5,035
    that should work, just remember to "chmod +x" it

  5. #5
    Just Joined!
    Join Date
    Apr 2005
    Posts
    6
    Thank yo very much. You have been most helpful.
    Nett

  6. #6
    Just Joined!
    Join Date
    Apr 2005
    Posts
    6
    After running this script, I found that it script doesn't allow for spaces in names.
    For instance, if there was a file named "2005 report", the script sees it as "2005" and "report" as two separate files.
    How can I modify it to allow spaces or change the spaces to underscores?



    Thanks
    Nett

  7. #7
    Linux Newbie
    Join Date
    Jul 2004
    Location
    Calcutta, India
    Posts
    220
    Here is the modified code for your space problem :
    Code:
    #!/bin/bash
    cd /home
    for user in `ls`
    do
    cd "$user"/mail
    for dir in `ls`
    do
    echo mail/"$dir" >> /home/"$user"/.mailboxlist
    done
    cd /home
    done
    This shud work .. I havent tested though ...

  8. #8
    Just Joined!
    Join Date
    Apr 2005
    Posts
    6
    I tried it and it sees two files where there is one.
    Any other thoughts?
    Thanks
    Nett

  9. #9
    Linux Guru lakerdonald's Avatar
    Join Date
    Jun 2004
    Location
    St. Petersburg, FL
    Posts
    5,035
    prehaps you could pipe the output of ls into sed, which would replace a space with an underscore:
    Code:
    #!/bin/bash
    cd /home
    for user in `ls`
    do
    cd $user/mail
    for dir in `ls | sed s/' '/'_'/`
    do
    echo mail/$dir >> /home/$user/.mailboxlist
    done
    cd /home
    done

  10. #10
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    Change the for dir in `ls` line to
    Code:
    for dir in *
    It also avoids the overhead of needlessly running ls, when you can get the shell to find the filenames for you. This works on my FC2.

Page 1 of 2 1 2 LastLast

Posting Permissions

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