Skip to content

Instantly share code, notes, and snippets.

@florido
Forked from seangeleno/chmod_examples.md
Created October 31, 2018 09:49
Show Gist options
  • Save florido/d3338cadc15d2f6f7416c909b452ae25 to your computer and use it in GitHub Desktop.
Save florido/d3338cadc15d2f6f7416c909b452ae25 to your computer and use it in GitHub Desktop.

assume we start with a file named foo.txt that has no assigned permissions, like this:

----------

add read permission to the file for all users:

chmod +r foo.txt
-r--r--r--

Next add write permission to the file for all users:

chmod +w foo.txt
--w--w--w-

Simultaneously:

chmod +rw foo.txt
-rw-rw-rw-

All priveleges for all users:

chmod +rwx foo.txt
-rwxrwxrwx

control file permissions for the user (u), the user's group (g), and all others (o). Here's a command where I add read permission for the current user:

chmod u+r foo.txt
-r--------

only the user has read permission on the file now. Similarly I can add write permission like this:

chmod u+w foo.txt
--w-------

execute permission like this:

chmod u+x foo.txt
---x------

You can also add read, write, and execute permissions for the user with one command like this:

chmod u+rwx foo.txt
-rwx------

You can do the same thing for the group:

chmod g+rwx foo.txt
----rwx---

and all others:

chmod o+rwx foo.txt
-------rwx

You can also combine chmod permission commands:

chmod u+rwx,g+rw,o+r foo.txt
-rwx-rw-r--

So far I've just shown "add" permissions with the plus sign (+) operator; this adds permissions to whatever permissions the file already has. You can also use the equal sign operator (=) to set permissions exactly, instead of adding to file permissions as shown so far. Here's a typical chmod equal permission assignment:

chmod u=rw,g=rw,o=r foo.txt
-rw-rw-r--

When using numerical chmod commands you use a sequence of three numbers, and the numbers again correspond to the user, group, and owner of the file.

Here are some numerical chmod command examples.

Add read permission to the owner of the file foo.txt:

chmod 400 foo.txt
-r--------

Add read permission to the owner and the group for the file foo.txt

chmod 440 foo.txt
-r--r-----

and read permission for the user, group, and all others:

chmod 444 foo.txt
-r--r--r--

As you can see, the number '4' implies 'read' permission.

You can do the same thing to add write permission using the number six, like this:

chmod 664 foo.txt
-rw-rw-r--

And you can add execute permission to the file, like this:

chmod 774 foo.txt
-rwxrwxr--

or this:

chmod 700 foo.txt
-rwx------

Finally, if you want to set and read and execute permissions -- typically done on a directory -- you use this command:

chmod 755 MyDir
-rxwr-xr-x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment