fix_permissions

This script will fix the directory, file, and script permissions in the given directory ($PWD if not given) and its subdirectories.

The permissions will be fixed in the following way (if -u or -g are not used):

directories

set to 700

files

set to 600

shell scripts (*.sh)

set to 700

The following parameter is required:

d

the directory ($PWD if not given) in which the permissions will be fixed

The following parameters are optional:

g

also fix the group permissions (770/660/770); cannot be used together with -u

u

use the current process' umask to fix the permissions; cannot be used together with -g

The permissions of the given directory (-d) itself are not modified.

node_modules directories will be ignored.

If the given directory (-d) is a Git repository and it has Git hooks configured (git config get core.hooksPath) then the hooks directory will be ignored by this script.

If you use Husky then the Husky base directory (.husky) will be ignored.

Usage

$ scripts/general/fix_permissions.sh
$ tree --noreport -p /tmp/example
[drwxrwxrwx]  /tmp/example
├── [dr-xr-xr-x]  a
│   ├── [dr-xr-xr-x]  b
│   └── [----------]  t.sh
├── [dr-xr-xr-x]  c
├── [----------]  s.sh
└── [----------]  t
$ scripts/general/fix_permissions.sh -d /tmp/example
$ tree --noreport -p /tmp/example
[drwx------]  /tmp/example
├── [drwx------]  a
│   ├── [drwx------]  b
│   └── [-rwx------]  t.sh
├── [drwx------]  c
├── [-rwx------]  s.sh
└── [-rw-------]  t

$ scripts/general/fix_permissions.sh -d /tmp/example -g
$ tree --noreport -p /tmp/example
[drwxrwx---]  /tmp/example
├── [drwxrwx---]  a
│   ├── [drwxrwx---]  b
│   └── [-rwxrwx---]  t.sh
├── [drwxrwx---]  c
├── [-rwxrwx---]  s.sh
└── [-rw-rw----]  t

$ scripts/general/fix_permissions.sh -d /tmp/example -u
$ tree --noreport -p /tmp/example
[drwx------]  /tmp/example
├── [drwx------]  a
│   ├── [drwx------]  b
│   └── [-rwx------]  t.sh
├── [drwx------]  c
├── [-rwx------]  s.sh
└── [-rw-------]  t
$ umask -S
u=rwx,g=,o=

$ mkdir /tmp/test && cd "$_"
$ git init
$ mkdir d && chmod -x d
$ touch f && chmod u+x f
$ touch s.sh
$ mkdir .githooks
$ touch .githooks/pre-commit && chmod u+x .githooks/pre-commit
$ git config core.hooksPath .githooks
$ mkdir -p node_modules/some-module
$ touch node_modules/some-module/some-script && chmod u+x node_modules/some-module/some-script
$ touch node_modules/some-module/some-script-without-execute-permission.sh
$ cd -
$ tree --noreport -p -a -I .git /tmp/test
[drwx------]  /tmp/test
├── [drwx------]  .githooks
│   └── [-rwx------]  pre-commit
├── [drw-------]  d
├── [-rwx------]  f
├── [drwx------]  node_modules
│   └── [drwx------]  some-module
│       ├── [-rwx------]  some-script
│       └── [-rw-------]  some-script-without-execute-permission.sh
└── [-rw-------]  s.sh

$ scripts/general/fix_permissions.sh -d /tmp/test

WARNING: The permissions in the directory '/private/tmp/test' will be fixed.

The following directories will be ignored:

/private/tmp/test/.githooks                                               (1)
/private/tmp/test/node_modules                                            (2)

Do you really want to irreversibly fix the permissions (Y/N)? y
$ tree --noreport -p -a -I .git /tmp/test
[drwx------]  /tmp/test
├── [drwx------]  .githooks                                               (1)
│   └── [-rwx------]  pre-commit
├── [drwx------]  d                                                       (3)
├── [-rw-------]  f                                                       (4)
├── [drwx------]  node_modules                                            (2)
│   └── [drwx------]  some-module
│       ├── [-rwx------]  some-script
│       └── [-rwx------]  some-script-without-execute-permission.sh
└── [-rwx------]  s.sh                                                    (5)
1 the Git hooks directory and it’s files are ignored
2 the node_modules directories and their subdirectories and files are ignored
3 directory permissions have been fixed
4 file permissions have been fixed
5 script permissions have been fixed