You are on page 1of 2

CHMOD command examples

By surya aix13:57No comments
Chmod (change mode) is one of the most frequently used commands in unix or linux
operating system. The chmod command is used to change the file or directory acc
ess permissions. To know about the access permissions of a file or directory, us
e the ls -l command as shown below:
$ ls -l sample.sh
-rwx-rw-r-- 1 matt deploy 94 Oct 4 03:12 sample.sh
Here in the above example: Use matt has the read, write and execute permissions
on the file. Group deploy has read and write permissions. Others have only the r
ead permission.
File and Directory permissions:
There are three different permissions. They are:
Read (4): Permitted to read the contents of the file. In case of directory, you
can view all the files and sub-directories in that directory.
Write (2): Permitted to write to the file. In case of directory, you can create
files and sub-directories.
Execute (1): Execute the file as a program/shell script. In case of directory, Y
ou can enter into that directory.
Here in the above, the numbers in the brackets represents the numeric values for
the corresponding permissions. If you want to have a combination of permissions
add the required numbers. For example, for read and execute, it is 4+1=5.
The syntax of chmod command is
chmod [options] mode filename
THe important options are:
-R : recursively change the permissions of a directory.
-v : Verbose
Chmod Examples in Linux / Unix:
1. Give read, write and execute permissions to everyone.
Read, write and execute: 4+2+1=7
$ chmod 777 sample.sh
In the above example, you can see that the permissions are specified with a thre
e digit number. The first digit is for user permissions, second is for group and
third is for others permission. This type of representation is called octal rep
resentation. Alternatively, you can use the symbolic representation to give the
permissions.
chmod ugo+rwx sample.sh
We will see more details about the symbolic representation later.
2. Give read permission to user, write permission to group and execute permissio
n to others.
$ chmod 421 sample.sh
3. Recursive permissions to directory
To give read and write permissions to all the users to a directory (including fi
les and subdirectories) use the recursive option -R.
chmod -R 666 /dir
Symbolic Representation of Permissions:
The
u :
g :
o :
The
r :
w :
x :
The

following symbols are used to represent the users, groups and others:
User
Group
Others a : All (user, group and others)
following symbols represent the permissions:
read
write
execute
following symbols represent the permissions grant or revoke:

Only selected permissions are assigned. Give only read permissions to the user $ chmod u=w sample. 2.+ : Additional permissions. this will add execute permission to oth ers. Remove write permission from group $ chmod g-w sample.sh In addition to the existing permissions.: Revoke the permissions.sh This will remove the existing permissions to the user and gives only write permi ssion to the user. Examples: 1. = : Specific permissions. Selected permissions are added. . . Add new permission execute to others $ chmod o+x sample. Selected permissions are revoked. 3.sh This will only removes the write permission for the group.