Skip to main content
updated output after change in chmod
Source Link
Jeff Schaller
  • 68.8k
  • 35
  • 122
  • 267
$ chmod o+r "$f"
$ (cd "$(dirname "$f")" && while [ "$PWD" != "/" ]; do chmod o+x .; cd ..; done)
chmod: changing permissions of `.': Operation not permitted
$ find /tmp/lh -ls
16    4 drwx---r--x   3 user  group        4096 Oct 23 12:01 /tmp/lh
20    4 drwx---r--x   3 user  group        4096 Oct 23 12:01 /tmp/lh/subdir1
21    4 drwx---r--x   3 user  group        4096 Oct 23 12:01 /tmp/lh/subdir1/subdir2
22    4 drwx---r--x   2 user  group        4096 Oct 23 12:01 /tmp/lh/subdir1/subdir2/subdir3
23    0 -rwx---r--   1 user  group           0 Oct 23 12:01 /tmp/lh/subdir1/subdir2/subdir3/filehere
$ chmod o+r "$f"
$ (cd "$(dirname "$f")" && while [ "$PWD" != "/" ]; do chmod o+x .; cd ..; done)
chmod: changing permissions of `.': Operation not permitted
$ find /tmp/lh -ls
16    4 drwx---r--   3 user  group        4096 Oct 23 12:01 /tmp/lh
20    4 drwx---r--   3 user  group        4096 Oct 23 12:01 /tmp/lh/subdir1
21    4 drwx---r--   3 user  group        4096 Oct 23 12:01 /tmp/lh/subdir1/subdir2
22    4 drwx---r--   2 user  group        4096 Oct 23 12:01 /tmp/lh/subdir1/subdir2/subdir3
23    0 -rwx---r--   1 user  group           0 Oct 23 12:01 /tmp/lh/subdir1/subdir2/subdir3/filehere
$ chmod o+r "$f"
$ (cd "$(dirname "$f")" && while [ "$PWD" != "/" ]; do chmod o+x .; cd ..; done)
chmod: changing permissions of `.': Operation not permitted
$ find /tmp/lh -ls
16    4 drwx-----x   3 user  group        4096 Oct 23 12:01 /tmp/lh
20    4 drwx-----x   3 user  group        4096 Oct 23 12:01 /tmp/lh/subdir1
21    4 drwx-----x   3 user  group        4096 Oct 23 12:01 /tmp/lh/subdir1/subdir2
22    4 drwx-----x   2 user  group        4096 Oct 23 12:01 /tmp/lh/subdir1/subdir2/subdir3
23    0 -rwx---r--   1 user  group           0 Oct 23 12:01 /tmp/lh/subdir1/subdir2/subdir3/filehere
repaired "o+r" to "or+x"
Source Link
Jeff Schaller
  • 68.8k
  • 35
  • 122
  • 267

Integrating chepner's astute comment regarding the directories only really needing readexecute-permission:

$ chmod o+r "$f"
$ (cd "$(dirname "$f")" && while [ "$PWD" != "/" ]; do chmod o+ro+x .; cd ..; done)
chmod: changing permissions of `.': Operation not permitted
$ find /tmp/lh -ls
16    4 drwx---r--   3 user  group        4096 Oct 23 12:01 /tmp/lh
20    4 drwx---r--   3 user  group        4096 Oct 23 12:01 /tmp/lh/subdir1
21    4 drwx---r--   3 user  group        4096 Oct 23 12:01 /tmp/lh/subdir1/subdir2
22    4 drwx---r--   2 user  group        4096 Oct 23 12:01 /tmp/lh/subdir1/subdir2/subdir3
23    0 -rwx---r--   1 user  group           0 Oct 23 12:01 /tmp/lh/subdir1/subdir2/subdir3/filehere
function makeitreadable() (
  chmod o+r "$1"
  cd "$(dirname "$1")" &&
    while [ "$PWD" != "/" ]
    do
      chmod o+ro+x .
      cd ..
    done
)

Integrating chepner's astute comment regarding the directories only really needing read-permission:

$ chmod o+r "$f"
$ (cd "$(dirname "$f")" && while [ "$PWD" != "/" ]; do chmod o+r .; cd ..; done)
chmod: changing permissions of `.': Operation not permitted
$ find /tmp/lh -ls
16    4 drwx---r--   3 user  group        4096 Oct 23 12:01 /tmp/lh
20    4 drwx---r--   3 user  group        4096 Oct 23 12:01 /tmp/lh/subdir1
21    4 drwx---r--   3 user  group        4096 Oct 23 12:01 /tmp/lh/subdir1/subdir2
22    4 drwx---r--   2 user  group        4096 Oct 23 12:01 /tmp/lh/subdir1/subdir2/subdir3
23    0 -rwx---r--   1 user  group           0 Oct 23 12:01 /tmp/lh/subdir1/subdir2/subdir3/filehere
function makeitreadable() (
  chmod o+r "$1"
  cd "$(dirname "$1")" &&
    while [ "$PWD" != "/" ]
    do
      chmod o+r .
      cd ..
    done
)

Integrating chepner's astute comment regarding the directories only really needing execute-permission:

$ chmod o+r "$f"
$ (cd "$(dirname "$f")" && while [ "$PWD" != "/" ]; do chmod o+x .; cd ..; done)
chmod: changing permissions of `.': Operation not permitted
$ find /tmp/lh -ls
16    4 drwx---r--   3 user  group        4096 Oct 23 12:01 /tmp/lh
20    4 drwx---r--   3 user  group        4096 Oct 23 12:01 /tmp/lh/subdir1
21    4 drwx---r--   3 user  group        4096 Oct 23 12:01 /tmp/lh/subdir1/subdir2
22    4 drwx---r--   2 user  group        4096 Oct 23 12:01 /tmp/lh/subdir1/subdir2/subdir3
23    0 -rwx---r--   1 user  group           0 Oct 23 12:01 /tmp/lh/subdir1/subdir2/subdir3/filehere
function makeitreadable() (
  chmod o+r "$1"
  cd "$(dirname "$1")" &&
    while [ "$PWD" != "/" ]
    do
      chmod o+x .
      cd ..
    done
)
Source Link
Jeff Schaller
  • 68.8k
  • 35
  • 122
  • 267

Integrating chepner's astute comment regarding the directories only really needing read-permission:

Setup:

$ mkdir -p /tmp/lh/subdir1/subdir2/subdir3
$ touch /tmp/lh/subdir1/subdir2/subdir3/filehere
$ chmod -R 700 /tmp/lh
$ find /tmp/lh -ls
16    4 drwx------   3 user  group        4096 Oct 23 12:01 /tmp/lh
20    4 drwx------   3 user  group        4096 Oct 23 12:01 /tmp/lh/subdir1
21    4 drwx------   3 user  group        4096 Oct 23 12:01 /tmp/lh/subdir1/subdir2
22    4 drwx------   2 user  group        4096 Oct 23 12:01 /tmp/lh/subdir1/subdir2/subdir3
23    0 -rwx------   1 user  group           0 Oct 23 12:01 /tmp/lh/subdir1/subdir2/subdir3/filehere

Prep:

$ f=/tmp/lh/subdir1/subdir2/subdir3/filehere

Do it:

$ chmod o+r "$f"
$ (cd "$(dirname "$f")" && while [ "$PWD" != "/" ]; do chmod o+r .; cd ..; done)
chmod: changing permissions of `.': Operation not permitted
$ find /tmp/lh -ls
16    4 drwx---r--   3 user  group        4096 Oct 23 12:01 /tmp/lh
20    4 drwx---r--   3 user  group        4096 Oct 23 12:01 /tmp/lh/subdir1
21    4 drwx---r--   3 user  group        4096 Oct 23 12:01 /tmp/lh/subdir1/subdir2
22    4 drwx---r--   2 user  group        4096 Oct 23 12:01 /tmp/lh/subdir1/subdir2/subdir3
23    0 -rwx---r--   1 user  group           0 Oct 23 12:01 /tmp/lh/subdir1/subdir2/subdir3/filehere

If you really prefer the intermediate directories to also have other-execute permissions, just change the chmod command to chmod o+rx.

The error message I got from the above results from my non-root userid attempting to change the permissions of the /tmp directory, which I don't own.

The loop runs in a subshell to isolate the changing of directories from your current shell's $PWD. It starts the loop by entering the directory containing the file then loops upwards, chmod'ing along the way, until it lands in the root / directory. The loop exits when it reaches the root directory -- it does not attempt to chmod the root directory.

You could make a script-file or function out of it like so:

function makeitreadable() (
  chmod o+r "$1"
  cd "$(dirname "$1")" &&
    while [ "$PWD" != "/" ]
    do
      chmod o+r .
      cd ..
    done
)