UNIX / Linux Modify File Permissions with chmod

What is chmod?

chmod is a Unix/Linux command that lets you tell the system, how much access it should permit to a file or directory. chmod stands for change mode, It is used to change a directory or file mode bits. The chmod command is the easiest and best way to modify the file permissions.

SYNOPSIS

chmod [Options]... Mode [,Mode]... file...
chmod [Options]... Numeric_Mode file...
chmod [Options]... --reference=RFile file...

OPTIONS

-f, –silent, –quiet :- most error messages are suppressed
-v, –verbose :- diagnostic is outputted for every file processed
-c, –changes :- is similar to verbose but it only report when a change is made
-c, –reference=RFile :- use RFile’s mode instead of MODE values
-R, –recursive :- change directories and files recursively
–help :- display help and exit
–version :- it displays version information and then exit

Example

In this example, the 9 characters (rwxrw-r–) from 2nd position to 10th represents the permissions setup for the three types of users in Linux.

[[email protected] ~]$ ls -al | grep newfile

-rwxrw-r--. 1 user1 user1 0 Apr 9 15:59 newfile1

Where,

r — file can be read

w — file can be written to

x — file can be executed (if it is a program)

– (dash) — specific permission has not been assigned
In the above example:

User (user1) has read, write and execute permission.
Group has read and write permission.
Others also have read-only permission.
0 — indicates no permissions.
1 –x indicates execute permissions.
2 -w- indicates write permissions.
3 -wx indicates write and execute permissions.
4 r– indicates read permissions.
5 r-x indicates read and execute permissions.
6 rw- indicates read and write permissions.
7 rwx indicates read, write, and execute permissions.

Examples

1.) Give read, write and execute to everybody (user, group, and others) on file named file1

read, write and execute = 4 + 2 + 1 = 7.

$ chmod 777 file1.txt
(or)
$ chmod ugo+rwx file1.txt

u — the user who owns the file (that is, the owner)
g — the group to which the user belongs
o — others (not the owner or the owner’s group)
a — everyone or all (u, g, and o)

2.) Give write privilege to user. Leave other privileges untouched

$ chmod u+w file1.txt

3.) Remove read, write and execute privileges to others (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 /directory

4.) How to Make a shell script executable through command line

$ chmod u+x script.sh

 

Related Commands

chown
umask

One Response

  1. John watson May 6, 2017

Add Comment