#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2012
    Location
    Baltimore, MD
    Posts
    29
    Rep Power
    0

    Script to change Permissions on files and directories


    Hey, It's me again.

    Have a problem, that's not really a problem. I have the below script, that goes to the directory I want it to go to. lists out the directories available, lets you choose the directory you want, then it changes the permissions on said directory. using chmod -R and chown -R.

    I however am setting chmod to 0770 recursively, but I want to be able to chose the directory, go into the directory, and determine if it's a file then chmod 0660, if directory 0770 and so on and so forth throughout the entire directory structure

    I know i can test -f a file or test -d a directory. But cant work the logic out in my head to get what I want accomplished any help would be appreciated.

    Code:
    #!/bin/bash
    
    source /generic/utils/etc/environments/perm.conf
    
    cd $ENVR
    DIRS=`ls -l $ENVR | egrep '^d' | awk '{print $9}'`
    
    for DIR in "${DIRS[@]}";
    do
        echo "$DIR"
            echo "Which environment do you want?: "
            echo -n "> "
            read i
    echo "Changing permissions now..."
    
    echo sudo chown -R $OWN:$GRP "$i" && echo sudo chmod -R $MOD1 "$i"
    #cd $ENVR/$i
    #sudo chmod -R $MOD2 *
    
    echo "Permissions are changed!"
    
    done
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,450
    Rep Power
    1751
    The recursive chown and chmod are not going to care if it's a file or directory when they 'do their stuff', so that'd not be the way to go.

    Have a look/think about using find:

    Code:
    find $ENVR/$i -type f -exec sudo chmod 770 {} \;
    find $ENVR/$i -type d -exec sudo chmod 660 {} \;
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2012
    Location
    Baltimore, MD
    Posts
    29
    Rep Power
    0
    Originally Posted by SimonJM
    The recursive chown and chmod are not going to care if it's a file or directory when they 'do their stuff', so that'd not be the way to go.

    Have a look/think about using find:

    Code:
    find $ENVR/$i -type f -exec sudo chmod 770 {} \;
    find $ENVR/$i -type d -exec sudo chmod 660 {} \;
    Never had a chance to respond. But i took your adivce and got it to work.

    THANKS
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,450
    Rep Power
    1751
    Glad you got it working
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc

IMN logo majestic logo threadwatch logo seochat tools logo