How to give automatic permissions in shell script

Asked

Viewed 1,414 times

3

I have a project that whenever I go up to production or change the development machine I copy and paste the whole folder, only whenever 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 can help me.

Scenario: I copy the folder projeto 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 imagine it to be simple, but I don’t have much idea, because I’m thinking of calling this shell pass the path of the project where will give these permissions and I don’t know if it is correct to pass in the root password.

I think it would be something so correct:

#!/bin/sh
#dar permissoes necessarias
PASTAPROJETO=$1
sudo chmod 755 '$PASTAPROJETO' -R
sudo chmod 777 '$PASTAPROJETO'/uploads -R
sudo chmod 777 '$PASTAPROJETO'/logs -R
  • What’s the matter, it’s not working?

  • @alacerda not yet... nor knew all these parameters... I will take a look

  • @winged unfortunately not... apparently when I gave the command he even rode without mistakes but did not work.

  • @Ricardo the script was giving an error that could not find the folder. Then I removed the quotes and it worked. But when I call the script I have to pass the root password. That’s why I put sudo in the script. Is there anything simpler than having to do it like this? Valew

  • @Marcelodiniz dude, you should give permissions to the user you’re using.

  • 2

    The problem you should investigate is how to give a user permission to run certain commands with sudo without having to enter password. This is perfectly possible, but it is another question. :)

  • Daniel is correct... give permission, you already know because you have already put the answer in the question itself. The question, as I understand then is the need to run this with root, no password through sudo. Write the script with the chmod/chown commands and then run the script with sudo : sudo sh meuscript.sh and it will do everything at once. If you need to avoid typing password, search for the configuration sudoers or ask another more specific question of how to use it.

Show 2 more comments

2 answers

4

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

  • 1

    OK, I just don’t understand why the copying part, which I’m seeing about giving permissions.

  • 4

    @Marcelodiniz The idea of the answer is that you can create an empty project template, made of folders even, set the right permissions in this template. When necessary, just copy the whole structure using the command cp with the -p parameter, because the copy will have the same permissions as the original model, and you will not have to set any permission again.

2

Alternative to option -p in the @asfelix response is to use the command rsync for copying.

rsync copies and keeps Owner, group, permissions and file dates equal to the original (with the option -a) . The advantage in its use is that if the files already exist it checks and fixes 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 "/" bar at the end of the project path, as this changes the rsync behavior, causes it to read/record the content, item by item instead of making 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....

Browser other questions tagged

You are not signed in. Login or sign up in order to post.