Linux – chmod Command

The chmod (short for “change mode”) command is used to change the permissions of files and directories in Linux. This command allows you to control who can read, write, or execute a file or directory. Permissions are set using either symbolic or numeric modes.



Tutorials dojo strip

Initial Example

To make a script file executable by the owner, you would use:

chmod u+x script.sh




Understanding File Permissions

In Linux, each file and directory has three types of permissions for three different categories of users:

  • Owner (u): The user who owns the file.
  • Group (g): The group that owns the file.
  • Others (o): All other users.

Permissions are represented by three letters:

  • r: Read permission.
  • w: Write permission.
  • x: Execute permission.




CHMOD Parameters

ParameterDescription
-c, --changesReport only when a change is made, similar to verbose but more selective.
-f, --silent, --quietSuppress most error messages.
-v, --verboseOutput a diagnostic for every file processed.
--dereferenceApply changes to the referent of each symbolic link, rather than the symbolic link itself.
-h, --no-dereferenceAffect each symbolic link itself, rather than the referent.
--no-preserve-rootDo not treat ‘/’ specially (this is the default behavior).
--preserve-rootFail to operate recursively on ‘/’.
--reference=RFILEUse RFILE’s mode instead of specifying MODE values. RFILE is always dereferenced if a symbolic link.
-R, --recursiveChange files and directories recursively.
-HTraverse the directory if a command-line argument is a symbolic link to it.
-LTraverse every symbolic link to a directory encountered.
-PDo not traverse any symbolic links.
--helpDisplay help text and exit.
--versionOutput version information and exit.




Examples

1. Using Symbolic Mode

To change permissions using symbolic mode, you specify who the permissions apply to and what you want to change:

  • Adding Permissions: Use + to add a permission.
  chmod u+r filename.txt
  • Removing Permissions: Use - to remove a permission.
  chmod g-w filename.txt
  • Setting Specific Permissions: Use = to set specific permissions.
  chmod o=x filename.txt

2. Using Numeric Mode

Permissions can also be set using a three-digit octal (base-8) value. Each digit represents a different category of users:

  • Read (r): 4
  • Write (w): 2
  • Execute (x): 1

For example, to set read, write, and execute permissions for the owner, and read and execute permissions for the group and others:

chmod 755 filename.txt

Here’s the breakdown:

  • Owner: 7 (4+2+1 = rwx)
  • Group: 5 (4+1 = r-x)
  • Others: 5 (4+1 = r-x)

3. Changing Directory Permissions

To change permissions for a directory and all its contents, use the -R (recursive) option:

chmod -R 755 /path/to/directory




Linux Playground

Scroll to Top