0

Background

I'm maintaining a repo in which I create many small bash files containing tiny code snippets.
To create these files, I use the Right Click -> New -> File interaction in PyCharm/IntelliJ quite often: Create New File

These files are automatically created with the rw-rw-r-- (664) permissions.
So every time, I have to start a terminal, and execute chmod 700 somefile.sh to make them executable.

The Question

It would be wonderful to automatically set the permissions of files with a certain extension in IntelliJ/PyCharm.
I don't want all my files to be executable, just the files with a .sh extension.
Is there a way I configure this?

melvio
  • 762
  • 6
  • 24
  • 2
    There is File Permissions plug-in: https://plugins.jetbrains.com/plugin/16238-file-permissions. It's open source so you can probably modify it to perform it automatically on certain actions (like VCS update). – CrazyCoder Aug 10 '21 at 17:23
  • 2
    If you are using Git, this can be automated with Git hooks: https://stackoverflow.com/a/22710807/104891. – CrazyCoder Aug 10 '21 at 17:28
  • 2
    https://plugins.jetbrains.com/plugin/11217-git-extended-update-index plug-in claims to support --chmod=+x on git update-index. – CrazyCoder Aug 10 '21 at 17:30
  • Thanks, @CrazyCoder! Not quite what I was looking for because I'd like them to be executable before adding them to git. However, this is not a crazy start ;-). Upvoted because they are good suggestions. – melvio Aug 10 '21 at 17:38

1 Answers1

1

A non-elegant solution and potentially problematic would be to have a bash script in the background checking contents of a specific directory for specific filenames and applying chmod 700 on those.

I strongly suggest a special directory - to minimize the risk of this script affecting other/unexpected files.

You'd need to set it to sleep for a few seconds so that it doesn't take up too much of your CPU time.

It could look something like this:

background_chmod.sh:

#!/bin/bash
FILESPEC="*.sh"
DIRPATH=/some/special/path/

while [ 0 ] :
do
    find $DIRPATH -name $FILESPEC -type f -exec chmod 700 {} \;
    sleep 10;  # tweak this to your requirements
done

You could then put it in your .bashrc file (if you are using bash) to execute when you log on. Additional line in your .bashrc would be: /path_to_script/background_chmod.sh &

Rememeber to:

 chmod +x /path_to_script/background_chmod.sh

IMPORTANT: I have no means of testing the above. Please test this on some test files before implementing! Use at your own risk!

melvio
  • 762
  • 6
  • 24
ewoj
  • 88
  • 7
  • Upvoted because this worked and you properly addressed the risks and limitations. Didn't accept it because I'm hoping for a more elegant solution. Regardless, you made a quality contribution. Thanks!. – melvio Sep 11 '21 at 10:01