Chmod in Ubuntu

What is chmod

chmod: Change the mode of each FILE to MODE.

Let's know something about the linux file permission

ls -l the first column will show the permissions for files.

1
2
3
4
5
6
7
8
$ ls -l
total 40
drwxr-xr-x 3 liwuchen staff 96 Jul 15 22:05 PaperSharing
-rwxr-xr-x 1 liwuchen staff 225 Jul 15 22:05 README.md
-rw-r--r-- 1 liwuchen staff 895 Jul 26 10:37 TODO.md
-rw-r--r--@ 1 liwuchen staff 1779 Jul 26 15:08 chmod.md
-rw-r--r-- 1 liwuchen staff 474 Jul 26 10:37 entropy.md
-rw-r--r-- 1 liwuchen staff 102 Jul 26 10:37 randomforest.md

On each line, the first character identifies the type of entry that is being listed.If it is a dash(-), it is a file. If is the letter d, it is a directory.

The next 9 characters represent the settings for user permissions, group permissions and other permission (each 3 characters).

User is the one who owns the file. Group is the other users in the group(add the link to another article here). Other can be anyone not in the first two categories.

There are 3 characters in each set of permission. The character means:

  1. -: Permission not granted.
  2. r: Read permission. The file can be opened and its content viewed.
  3. w: Write permission. Th filed can be edited, modified and delted.
  4. x: Execute permissions. If the file is a script or a program (like a shell script), it can be executed.

Let's see an example.

drwxr-xr-x 3 liwuchen staff 96 Jul 15 22:05 PaperSharing

PaperSharing is a directory. User (`Liwuchen) can write, read and execute (for directory, it means cd). Goup (Staff) can read and execute it. Other can read and execute it too. The size of this directory is 96 bytes. The directory is created at 22:05 15 Jul.

How to use chmod

The syntax for chmod is: chmod <who><what><which> <file name> (put it to a table here)

<who>:

  1. u: user, the owner of the file
  2. g: group, members of the group the file belongs to
  3. o: others
  4. a: all above, anyone If you do not specify <who>, chmod will take a as default.

<what>:

  1. -: remove permission
  2. +: add permission
  3. =: set a permission and remove others

<which>:

  1. r: read permission
  2. w: write permission
  3. x: execute permission

eg. chmod u=rwx, og=r file.txt chmod +x scipt.sh chmod o-r *.txt

Numerical Shorthand

  • 0: no permission
  • 1: execute
  • 2: write
  • 3: write & execute
  • 4: read
  • 5: read & execute
  • 6: read & write
  • 7: read & write & execute

CLICK HERE for more information.