How to use the chmod command in Linux

Created
Modified

The chmod command sets the file permissions flags on a file or folder. The flags define who can read, write to or execute the file. When you list files with the -l (long format) option you’ll see a string of characters that look like

Add write permission (w) to the Group's (g) access modes of a directory
chmod g+w dirname
ls -l
-rw-r--r-- 12 root root
|[-][-][-]-  [--] [--]
| |  |  | |   |    |
| |  |  | |   |    +--> 7. Group
| |  |  | |   +-------> 6. Owner
| |  |  | +-----------> 5. Alternate Access Method
| |  |  +-------------> 4. Others Permissions
| |  +----------------> 3. Group Permissions
| +-------------------> 2. Owner Permissions
+---------------------> 1. File Type

How to change directory permissions in Linux

add permissions
chmod +rwx filename
remove permissions
chmod -rwx filename.md
cat filename.md
cat: filename.md: Permission denied

To change directory permissions for everyone, use “u” for users, “g” for group, “o” for others, and “ugo” or “a” (for all).

give read, write, and execute to everyone
chmod ugo+rwx foldername
chmod u=rw,og=r filename.md
include files in subdirectories
chmod -R o-r *.page
-rw-r--r--

The syntax for the chmod command is as follows:

Usage: 
  chmod [OPTION]... MODE[,MODE]... FILE...
  chmod [OPTION]... OCTAL-MODE FILE...
  chmod [OPTION]... --reference=RFILE FILE...

OPTIONS:
--reference
change the mode of each FILE to that of RFILE.
-c, --changes
like verbose but report only when a change is made
-f, --silent, --quiet
suppress most error messages
-v, --verbose
output a diagnostic for every file processed
--no-preserve-root
do not treat '/' specially (the default)
--preserve-root
fail to operate recursively on '/'
--reference=RFILE
use RFILE's mode instead of MODE values
-R, --recursive
change files and directories recursively
--help
display this help and exit
--version
output version information and exit
#flags
u
The file owner.
g
The users who are members of the group.
o
All other users.
a
All users, identical to ugo.

Related Tags

#Linux# #chmod#