Linux commands
Browse topics

Changing permissions with chmod

Version
unversioned
Review
Draft

Summary#

chmod changes the read, write and execute permissions of files. Modes can be octal numbers such as 640 or symbolic changes such as u+x.

Syntax#

sh
chmod 640 file
chmod u+x script
chmod -R u=rwX,go=rX directory

In symbolic modes, u, g, o and a select the owner, group, others and everyone. Changing a mode normally requires owning the file or having elevated privileges.

Example#

sh
(
  set -eu
  dir=$(mktemp -d)
  trap 'rm -rf -- "$dir"' EXIT
  cd "$dir"
  mkdir -p site/css
  : > site/css/main.css
  printf '#!/bin/sh\necho deployed\n' > deploy.sh

  chmod 644 deploy.sh
  stat -c '%A %a %n' deploy.sh
  chmod u+x deploy.sh
  stat -c '%A %a %n' deploy.sh
  ./deploy.sh

  chmod -R u=rwX,go=rX site
  stat -c '%A %a %n' site site/css site/css/main.css
)

Expected output#

Text
-rw-r--r-- 644 deploy.sh
-rwxr--r-- 744 deploy.sh
deployed
drwxr-xr-x 755 site
drwxr-xr-x 755 site/css
-rw-r--r-- 644 site/css/main.css

Common mistakes#

chmod -R 755 directory also makes every regular file executable. Use capital X, which adds execute permission only to directories and to files that already have an execute bit. Do not use 777 to fix an access error; correct the owner or group instead.

Version applicability#

This entry describes GNU coreutils chmod on Linux. The stat -c format in the example is GNU-style; BSD and macOS stat use -f.

Sources#

Attribution
DevLookup contributors
License
CC-BY-4.0