skip to Main Content

I’m looking for some help in creating a shell script in Linux to perform a batch ownership change for certain folders in a Plesk environment where the owner:group is apache:apache.

I want to change the owner:group to :psacln.

The FTP user can be ascertained by looking at the owner of the httpdocs folder.
^this is the section I’m having trouble with.

If I was to set all owners to be the same, I could do a one-line:

find /var/www/vhosts/*/httpdocs -user apache -group apache -exec chown user:psacln {} ;

Can anyone help plug the user in to this command?

Thanks

2

Answers


  1. Chosen as BEST ANSWER

    Figured it out... for those who may want to use it in the future:

        for dir in /var/www/vhosts/*
        do
            dir=${dir%*/}
            permissions=`stat -c '%U' ${dir##*/}/httpdocs`
            find ${dir##*/}/httpdocs -user apache -group apache -exec chown $permissions {} ;
        done
    

  2. Since stat doesn’t work on al unices in the same way, I thought I would share my script to set the ownership of all websites to the correct owners in Plesk (tested on Plesk 11, 11.5, 12 and 12.5):

    cd /var/www/vhosts/
    for f in *; do
        if [[ -d "$f" && ! -L "$f" ]]; then
    
            # Get necessary variables
            FOLDERROOT="/var/www/vhosts/"
            FOLDERPATH="/var/www/vhosts/$f/"
            FTPUSER="$(ls -ld /var/www/vhosts/$f/ | awk '{print $3}')"
    
            # Set correct rights for current website, if website has hosting!
            cd $FOLDERPATH
            if [ -d "$FOLDERPATH/httpdocs" ]; then
                chown -R $FTPUSER:psacln httpdocs
                chmod -R g+w httpdocs
                find httpdocs -type d -exec chmod g+s {} ;
    
                # Print success message
                echo "Done... $FTPUSER is now correct owner of $FOLDERPATH."
            fi
    
            # Make sure we are back at the root, so we can continue looping
            cd $FOLDERROOT
        fi
    done
    

    \

    Explanation of code:

    1. Go to vhosts folder
    2. Loop through websites
    3. Store vhosts path, because we are using cdin a loop
    4. If httpdocsfolders exists for the current website, than
    5. set the correct rights of httpdocs and
    6. all underlying folders
    7. Show succes message
    8. cd back to vhosts folder, so we can continue looping

    \

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search