When initiated, the script below automatically changes the permissions of all files of a given type (extension) in a directory (one time). After that, the script checks the directory every 5 seconds for newly added files, and changes the permissions if the file is of the given type (in this case a .py
file)
It has a few options: in this case, it makes the newly added files executable, but other actions are possible too, as defined in the line: command = "chmod +x"
. Additionally, you can define (change) on what kind of files (language extensions) the action should be performed.
How to use
Copy the script below into an empty file. Save it as change_permission.py
and run it in the background by the command:
python3 <script> <folder_to_watch>
The script
#!/usr/bin/env python3
import subprocess
import time
import sys
directory = sys.argv[1]
command = "chmod +x"; check_interval = 5; extensions = (".py")
def current_files():
read = subprocess.check_output(["ls", directory]).decode("utf-8").strip()
return [item for item in read.split("\n") if item[item.rfind("."):] in extensions]
initial_files = current_files()
for file in initial_files:
subprocess.call(["/bin/bash", "-c", command+" "+directory+"/"+file])
while True:
update = current_files()
for file in update:
if not file in initial_files:
subprocess.call(["/bin/bash", "-c", command+" "+directory+"/"+file])
initial_files = update
time.sleep(check_interval)
*Note: if you need sudo privileges, simply run the script with sudo
vim
oremacs
to do that for you.