UNIX / Linux chmod Command Examples

by SathiyaMoorthy

What is chmod?
3 chmod examples
Syntax and Options
Related Commands

What is chmod?

chmod stands for change mode, which changes the file or directory mode bits. To put it simply, use chmod command to change the file or directory permissions.

Following is a sample of ls -l command output. In this, the 9 characters from 2nd to 10th position represents the permissions for the 3 types of users.

-rw-r--r--  1 john john  272 Mar 17 08:22 test.txt

In the above example:

  • User (john) has read and write permission
  • Group has read permission
  • Others have read permission

Three file permissions:

  • read: permitted to read the contents of file.
  • write: permitted to write to the file.
  • execute: permitted to execute the file as a program/script.

Three directory permissions:

  • read: permitted to read the contents of directory ( view files and sub-directories in that directory ).
  • write: permitted to write in to the directory. ( create files and sub-directories in that directory )
  • execute: permitted to enter into that directory.

Numeric values for the read, write and execute permissions:

  • read 4
  • write 2
  • execute 1

To have combination of permissions, add required numbers. For example, for read and write permission, it is 4+2 = 6.

3 chmod 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

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


Related Commands

chown
umask

{ 6 comments… read them below or add one }

1 Alexandre Palomo March 13, 2012 at 11:40 am

Hi guys!

I’m looking for a solution in my CHMOD problem.
The unix version is: SunOS 5.10 sun4u sparc

I’m trying to run this command (I’m not root):
chmod -v 770 *

But verbose argument does not work. It’s returning these messages:
chmod: WARNING: can’t access 770
chmod: ERROR: invalid mode

How can I manage it?
I need to log all the files that had been your privileges changed.

Thanks!

2 seyi December 1, 2012 at 5:03 am

U are doing a good job – helping people with your knowledge being spread abroad.
Thank you very much.
Please, I need your assistance. I am to use a chmod command
“chmod -R ugo-w $SPEC” but returning “chmod: missing operand after “ugo-w” and i have searched several materials but no direct solution. Please help

3 Biork February 21, 2013 at 1:58 pm

There is no mode like 770

4 kashyap darania March 22, 2013 at 1:16 am

Why I can’t access 755 in unix?

5 Ixium May 3, 2013 at 7:56 am

Biork: How do you mean there’s no mode like 770?

chmod 770 gives the mode rwxrwx—

Which means rights are set as follows:
—-
read+write+execute to the owner, read+write+execute to the group and no rights to others (world)
—-

6 Prateek January 8, 2014 at 7:53 pm

Hi All,

I’ve one question in my mind, which one is correctly denied read, write and execute the file “unix”
a) chmod a — unix
b) chmod -d rwx unix
c) chmod a – rwx unix
d) chmod d — unix

Please try to answer the following question. Your help is much appreciated.

Leave a Comment

Previous post:

Next post: