How to give automatic permissions in shell script

3

I have a project that whenever I go up for production or change of development machine I copy and paste the whole folder, but every time I do this I have to give permissions in this folder and in some sub-folders. I would like to create a script to do this automatically, someone knows how to help me.

Scenario: I copy the projeto folder into my /var/www give permissions for this project.

sudo chmod 755 /var/www/projeto -R
# dai me pede a senha de root, eu digito e ok
# dai eu tenho que alterar outras permissões de outras pastas para escrita
sudo chmod 777 /var/www/projeto/uploads -R
sudo chmod 777 /var/www/projeto/logs -R

I think it's simple, but I do not have much idea, because I'm thinking of calling this shell to pass the project path where it will give those permissions and I do not know if it's okay to pass in the root password.

I think it would be something like this:

#!/bin/sh
#dar permissoes necessarias
PASTAPROJETO=$1
sudo chmod 755 '$PASTAPROJETO' -R
sudo chmod 777 '$PASTAPROJETO'/uploads -R
sudo chmod 777 '$PASTAPROJETO'/logs -R
    
asked by anonymous 14.03.2014 / 19:31

2 answers

4

use "-p", this way you will be copying not only the files and folders, but also their due permissions; to better understand: $ man cp

    
26.03.2014 / 14:28
2

Alternatively the -p option in @asfelix's response is to use the rsync command to copy.

rsync copies and keeps the owner, group, permissions, and dates of the files equal to the original (with the -a option). The advantage in using it is that if the files already exist it checks and corrects this data if necessary.

It would be something like:

sudo rsync -a --delete  /diretorio/projeto /var/www/projeto

Just be careful not to put the "/" slash at the end of the project path, as this changes the rsync behavior, causes it to read / write content, item by item rather than the entire folder.

Useful parameters in rsync:

-n / --dry-run  : não efetiva copia, apenas simula
-i  : exibe arquivo a arquivo e qual atualização está sendo feita
-h  : modo "human-readable"
-v  : modo verbose....
    
27.03.2014 / 04:53