Skip to content

Instantly share code, notes, and snippets.

@moreati
Last active May 19, 2018 06:20
Show Gist options
  • Save moreati/3891068a9df24cc96e1d75525d37acf2 to your computer and use it in GitHub Desktop.
Save moreati/3891068a9df24cc96e1d75525d37acf2 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 we're obtained on Ubuntu Linux 16.04
@lbvf50mobile
Copy link

Hi @moreati. Thank you for this file, I find here a lot of interesting bash expedients. Parsing it line by line, and studding shell scripting craft. And at the first line I got a question:
Is chmod ugo=r a is equal to chmod a=r a?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment