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:
--referencechange the mode of each FILE to that of RFILE.-c, --changeslike verbose but report only when a change is made-f, --silent, --quietsuppress most error messages-v, --verboseoutput a diagnostic for every file processed--no-preserve-rootdo not treat '/' specially (the default)--preserve-rootfail to operate recursively on '/'--reference=RFILEuse RFILE's mode instead of MODE values-R, --recursivechange files and directories recursively--helpdisplay this help and exit--versionoutput version information and exit#flagsuThe file owner.gThe users who are members of the group.oAll other users.aAll users, identical to ugo.