Syntax and Options:
chmod [OPTION]… MODE[,MODE]… FILE…
chmod [OPTION]… OCTAL-MODE FILE…
chmod [OPTION]… –reference=RFILE FILE…
Short Option | Long Option | Option Description |
-c | –changes | like verbose but report only when a change is made |
–no-preserve-root | do not treat `/’ specially (the default) | |
–preserve-root | fail to operate recursively on `/’ | |
-f | –silent, –quiet | suppress most error messages |
-v | –verbose | output a diagnostic for every file processed |
–reference=RFILE | use RFILE’s mode instead of MODE values | |
-R | –recursive | change files and directories recursively |
–help | display this help and exit | |
–versio | output version information and exit |
Examples:
Give read, write and execute to everybody (user, group, and others)
read, write and execute = 4 + 2 + 1 = 7.
$ chmod 777 file.txt (or) $ chmod ugo+rwx file.txt
Give execute privilege to user. Leave other privileges untouched
execute = 1. If you want to just add execute privilege to users and leave all other privileges as it is, do the following.
$ chmod u+x file.txt
Give read, write and execute privileges to group (including all the files in the sub-directories)
Use -R, as shown below to provide the recursive privileges for the directory and sub-directories (including the files in it).
$ chmod -R g+rwx /u01
1. Add single permission to a file/directory
Changing permission to a single set. + symbol means adding permission. For example, do the following to give execute permission for the user irrespective of anything else:
$ chmod u+x filename
2. Add multiple permission to a file/directory
Use comma to separate the multiple permission sets as shown below.
$ chmod u+r,g+x filename
3. Remove permission from a file/directory
Following example removes read and write permission for the user.
$ chmod u-rx filename
4. Change permission for all roles on a file/directory
Following example assigns execute privilege to user, group and others (basically anybody can execute this file).
$ chmod a+x filename
5. Make permission for a file same as another file (using reference)
If you want to change a file permission same as another file, use the reference option as shown below. In this example, file2′s permission will be set exactly same as file1′s permission.
$ chmod --reference=file1 file2
6. Apply the permission to all the files under a directory recursively
Use option -R to change the permission recursively as shown below.
$ chmod -R 755 directory-name/
7. Change execute permission only on the directories (files are not affected)
On a particular directory if you have multiple sub-directories and files, the following command will assign execute permission only to all the sub-directories in the current directory (not the files in the current directory).
$ chmod u+X *
Note: If the files has execute permission already for either the group or others, the above command will assign the execute permission to the user