Change mode command – chmod

Change mode command - chmod

Syntax:chmod [-Options] <permissions> <file/directory>

Explanation:chmod is an abbreviation for change mode.
Chmod is used to decide which users has the right to read, write or execute a certain file or folder. In other words: set the permissions of that file or folder.

Permissions of files and folders is set in one of the following ways: symbolic mode or numeric mode.

See also – react native app developers

Setting permissions in symbolic mode

When using the symbolic mode to set permissions of a file or folder you use letters which represents different rights. The most common letters are:

  • R which stands for Read
  • W which stands for Write
  • X which stands for Execute

Furthermore, you must also specify which users the new permissions should be applied on.

  • A Everyone (all)
  • U The owner (a user) of the file (User)
  • G The group which the file/folder is a member of (Group)
  • O Everyone else (Other)

All this becomes clearer with an example:

chmod ug+rw “myfile.txt”

In this example, ug+rw is the part which reveals what permissions the file is going to have afterwards.

  • ug   Decides which users the new permissions are going to be applied on: Owner (u) and the Group (g).
  • +   The plus means that we want to ADD the nextcoming permissions to the users on the line above.
  • rw   Tells us what permissions we want to apply to these users: Read (R) and Write (W).

This command would therefor give both the owner of the file, as well as the group it belongs to, permission to read and write to the file called “myfile.txt”.

If we instead wanted to remove these rights from the given users we would simply change the plus (“+”) into a minus (“-“).
Like this:

chmod ug-rw “myfile.txt”

This command would remove the permissions for the owner, and the group it belongs to, permission to read AND write to this file.

Setting permissions in numeric mode

The numeric permissons is based on four numeric values in a row, for example: 0775.

OwnerGroupOther
0775

In this case we don’t need to use any letter at all (Like the R/W/X in the previous examples) since every number symbolizes the permission given to different users/usergroups.
You can find a more detailed explanation of how to set permissions in numeric mode. We advise you to read that section if you’re not familiar enough with how this works.

Examples

chmod 0775 myfile.txt Read, Write and Execute-permissions for the owner and the group. Everyone else is allowed to read and execute the file, but not write to the file.
chmod 0444 myfile.txtAll users (owner, group and others) only have read permissions to the file myfile.txt.
Change mode command - chmod

Last things

There are also a couple of options you can use to affect which files the permissions are going to be applied on. For example: If you’re using chmod on a directory with several files and subfolders inside – Which files/folders will get the new permissions? Only the folder or even the files located inside it?

The following options can be used with chmod

-RRecursive – All files and subfolders inside a directory will also be inheriting the new permissions.
-HPermissions of files and folders located in the target destination of symbolic links will also be affected (But not subtrees inside directories in a symbolic link).
-LAll files and folders in symbolic links are changed, including subtrees.
-PNo filer or folders in the target destination of symbolic links will be affected. (This is the default action if you’re not using any of the options regarding symbolic links).
-fDo not display an error message if a file or a folder can’t be changed.

Exempel

chmod -R 0777 /usr/Documents All users will get full access (read, write, execute) to the folder /usr/Documents including all of its files and eventual subtrees.

Quick tip:

Sometimes you might not be able to perform a certain command or task from the Terminal. This might be because you don’t have the right permission to do so. When that happens you can try to add the word “sudo” in the beginning of the line.

Sudo is an abbreviation of superuser do and means that you want to execute the following command as a user with extended permissions, most often as an Administrator. When using “sudo” you sometimes need to enter the password of the root account.

More to read: Using cURL in the Terminal for Mac OS X