Skip to content

Instantly share code, notes, and snippets.

@pipern
Forked from moreati/example.md
Last active November 21, 2016 12:19
Show Gist options
  • Save pipern/f0e9bf05cdbf5d6a66fb2df51d201850 to your computer and use it in GitHub Desktop.
Save pipern/f0e9bf05cdbf5d6a66fb2df51d201850 to your computer and use it in GitHub Desktop.
Read and Execute permissions on Linux directories, by example

Preperation

Create 3 top-level directories. Create a file and a directory in each.

$ mkdir r rx x
$ touch {r,rx,x}/file
$ mkdir {r,rx,x}/dir

Apply read only, read + execute, and execute only permissions to the respective top-level directories

$ chmod ugo=r r
$ chmod ugo=rx rx
$ chmod ugo=x x

The top-level directories now have the following attributes

dr--r--r-- 3 alex alex 4096 Nov 17 18:48 r
dr-xr-xr-x 3 alex alex 4096 Nov 17 18:48 rx
d--x--x--x 3 alex alex 4096 Nov 17 18:48 x

Each directory contains the following

drwxrwxr-x 2 alex alex 4096 Nov 17 18:48 dir
-rw-rw-r-- 1 alex alex    0 Nov 17 18:48 file

Read only

We can ls the directory. However only the name of each item, and whether it is a directory or a file. We cannot stat any of the contained items, so no ownership, size or permissions are revealed.

$ ls -l r/
ls: cannot access 'r/dir': Permission denied
ls: cannot access 'r/file': Permission denied
total 0
d????????? ? ? ? ?            ? dir
-????????? ? ? ? ?            ? file

We cannot list or read any of the contained items

$ ls r/dir
ls: cannot access 'r/dir': Permission denied
$ cat r/file
cat: r/file: Permission denied

We cannot cd into the directory

$ cd r
bash: cd: r: Permission denied

Read + execute

We can ls the directory, and stat all contained items.

$ ls -l rx
total 4
drwxrwxr-x 2 alex alex 4096 Nov 17 18:48 dir
-rw-rw-r-- 1 alex alex    0 Nov 17 18:48 file

We can list or read all of the contained items

$ ls rx/dir
$ cat rx/file

We can cd into the directory

$ cd rx

Execute only

We cannot ls the directory

$ ls -l x
ls: cannot open directory 'x': Permission denied

We can list or read the contained items, but only if we already know (or can guess) their name

$ ls x/dir
$ cat x/file

We can cd into the directory

$ cd x

Notes

  1. The preceding examples were obtained on Ubuntu Linux 16.04
  2. Useful further reading http://unix.stackexchange.com/questions/21251/how-do-directory-permissions-in-linux-work
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment