Can anyone help with a recursive directory script?

Discussion in 'Programming' started by mm-93, Aug 10, 2008.

  1. #1
    Man can someone hit me with an SSH script that could do the following:

    check in 100's of domain folders and see if the folder "upload" is in there.. If not, create the directory and set it to 777 and move to the next domain folder...

    So I would run the script at:
    /web/sites/vhosts/*
    (which contains tons of domains)
    and it would check just ONE LEVEL down and create the folder ONCE inside each domain. Many thanks for your help!
     
    mm-93, Aug 10, 2008 IP
  2. pr0t0n

    pr0t0n Well-Known Member

    Messages:
    243
    Likes Received:
    10
    Best Answers:
    10
    Trophy Points:
    128
    #2
    pico -w check.sh
    Code (markup):
    and then paste this:

    
    #!/bin/sh
    
    DirName="upload"
    cd /web/sites/vhosts/
    for x in *
    do
    DOMAIN=$x
    cd /web/sites/vhosts/
    USER=`ls -l | grep $DOMAIN | awk '{ print $4}'`
    cd /web/sites/vhosts/$DOMAIN/
    if ! test -d "${DirName}"
    then
    mkdir $DirName
    chmod 777 $DirName
    chown $USER.$USER $DirName
    echo "Created folder and set chmod to 777 in: $DOMAIN"
    else
    echo "Folder already exists in: $DOMAIN"
    fi
    done
    
    Code (markup):
    then chmod this .sh script to 755 and that's it.. just run it :D
    btw: script will also change the ownership of the folder to the owner of that virtual domain... if you wish to have root as owners of all those 'upload' folders then just remove this lines:
    
    USER=`ls -l | grep $DOMAIN | awk '{ print $4}'`
    
    *and
    
    chown $USER.$USER $DirName
    
    Code (markup):
     
    pr0t0n, Aug 10, 2008 IP
  3. pr0t0n

    pr0t0n Well-Known Member

    Messages:
    243
    Likes Received:
    10
    Best Answers:
    10
    Trophy Points:
    128
    #3
    Oh and by the way... the part with checking for folder ownership may be the tricky part... because if virtual domain/user folder does not have a permission of that specific user (and is created with root ownership) then your upload folders will also have root ownership. You should check ownership of those folders in your /web/sites/vhosts/ folder. If you are using cPanel on your server, then it's structure will be fine, and script will do the work. Anyway... you can do ls -la in your /web/sites/vhosts/ and paste a few lines from there and I'll tell you, or you can just try and execute the script :rolleyes:
     
    pr0t0n, Aug 10, 2008 IP
  4. mm-93

    mm-93 Peon

    Messages:
    21
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Many thanks for the response! I didn't even realize you responded actually lol didn't have subscribe to thread turned on apparently. So this is what I went with:

    for f in *; do mkdir $f/foldername ; done

    Seemed to work great... Hey do you work with cURL? Thanks again!
    Mitch
     
    mm-93, Aug 12, 2008 IP