When I create file .sh
, I need to give permission for it to be executed, is there any file that can be configured or comando
applied so that I can already create the file with execute permission?
When I create file .sh
, I need to give permission for it to be executed, is there any file that can be configured or comando
applied so that I can already create the file with execute permission?
If you use vim , this can be an automated solution:
Add the following line to your ~ / .vimrc :
au BufWritePost * if getline(1) =~ "^#!" | if getline(1) =~ "/bin/" | silent !chmod +x <afile> | endif | endif
It will detect whenever there is a file where the first line has "/ bin /" or start with "#!" and run chmod + x automatically.
You can not add the execute privilege by default when creating files.
The umask
( file-creation mode mask ) setting of the system controls the default permissions of files and directories. The default value is 0002, which results in the permissions 0664 (666 - 002, fmask
- umask
) for files and 0775 (777 - 002, dmask
- umask
) for directories. And it is not possible to change fmask
and dmask
.
Source: Making new files automatically executable
The solution would be to create a scheduled task using the command crontab -e
and inserting the line below at the end of the file
* * * * * find ~/*.sh -type f -exec chmod +x {} \;
The command will run every minute, it finds the .sh files inside your user folder and adds execute permissions.
It may be interesting to run every 5 minutes (or more), according to demand, so change the beginning of the line to */5 * * * *
If this is something common for you, why do not you create a command for it and save it on your Path
with your bin
? Like this:
echo -e "#!"'which bash'"\ntouch \ && chmod +x \" > mkscript && chmod +x mkscript
Then whenever you want to create an executable script, you just call the command like this:
mkscript <nome_script>