Skip to content

Instantly share code, notes, and snippets.

@simonesestito
Last active October 28, 2022 06:55
Show Gist options
  • Save simonesestito/e1eb0647c305c9996b385128967401f7 to your computer and use it in GitHub Desktop.
Save simonesestito/e1eb0647c305c9996b385128967401f7 to your computer and use it in GitHub Desktop.
Chmod all dirs to 755 and all files to 644, apart from .sh files
#!/bin/env python3
'''
Super simple script to set chmod 644 to every file and 755 to every directory.
Useful, for instance, when moving files (in WSL) from the Windows Host to the Linux machine.
As we know, files in /mnt/c/ have the eXecute permission flagged, which is not desiderable.
'''
import os, sys
def chmod(path, is_dir):
if is_dir:
print('\033[1;34m755', path, '\033[0m')
os.chmod(path, 0o755)
elif path.endswith('.sh'):
print('\033[1;32m755', path, '\033[0m')
os.chmod(path, 0o755)
else:
print('644', path)
os.chmod(path, 0o644)
if __name__ == '__main__':
if len(sys.argv) < 2:
print(f'USAGE: {sys.argv[0]} DIRS...')
sys.exit(1)
for rootdir in sys.argv[1:]:
chmod(rootdir, is_dir=os.path.isdir)
for d, _, files in os.walk(rootdir):
chmod(d, is_dir=True)
for f in files:
chmod(d + '/' + f, is_dir=False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment