chmod - Unix, Linux Command


Advertisements

Previous Page
Next Page  
 

NAME

chmod - change file mode bits

SYNOPSIS

chmod [OPTION]... MODE[,MODE]... FILE...
chmod [OPTION]... OCTAL-MODE FILE...
chmod [OPTION]... --reference=RFILE FILE...

DESCRIPTION

chmod changes the file mode bits of each given file according to mode, which can be either a symbolic representation of changes to make, or an octal number representing the bit pattern for the new mode bits. chmod never changes the permissions of symbolic links; the chmod system call cannot change their permissions. This is not a problem since the permissions of symbolic links are never used. However, for each symbolic link listed on the command line, chmod changes the permissions of the pointed-to file. In contrast, chmod ignores symbolic links encountered during recursive directory traversals.

OPTIONS

TAG 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
--version output version information and exit

EXAMPLES

EXAMPLE-1:

Add single permission to a file/directory

Use comma to separate the multiple permission sets as shown below.
Before performing the command do
$ ls -lrt
-rw-rw-r--  1 nubo nubo   15 Jan  8 14:26 file1.txt
$ chmod u+r,g+x filename

Now do ls -lrt agin
-rw-rwxr--  1 nubo nubo   15 Jan  8 14:26 file1.txt

EXAMPLE-2:

Remove permission from a file/directory

Following example removes read and write permission for the user.

$ chmod u-rx filename
output:
ls -lrt
--w-rwxr--  1 nubo nubo   15 Jan  8 14:26 file1.txt

EXAMPLE-3:

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

output:
ls -lrt
--wxrwxr-x  1 nubo nubo   15 Jan  8 14:26 file1.txt

EXAMPLE-4:

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.
$ ls -lrt

-rw-rw-r-- 1 nubo nubo 46 Jan 7 20:37 xyz
--wxrwxr-x 1 nubo nubo 15 Jan 8 14:26 file1.txt

$ chmod --reference=file1 file2

$ ls -lrt
-rw-rw-r-- 1 nubo nubo 46 Jan 7 20:37 xyz
-rw-rw-r-- 1 nubo nubo 15 Jan 8 14:26 file1.txt

EXAMPLE-5:

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/

output:

ls -lrt

drwxr-xr-x  2 nubo nubo 4096 Jan  8 11:08 web

EXAMPLE-6:

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).

$ ls -lrt
total 4
-rw-rw-r-- 1 nubo nubo 0 Dec 29 19:03 <F11>
-rw-rw-r-- 1 nubo nubo 248 Dec 29 19:07 Test-AR.a
-rw-rw-r-- 1 nubo nubo 0 Dec 29 19:18 example-4
-rw-rw-r-- 1 nubo nubo 0 Dec 29 19:18 example-3
-rw-rw-r-- 1 nubo nubo 0 Dec 29 19:18 example-2
-rw-rw-r-- 1 nubo nubo 0 Dec 29 19:18 example-1

$ chmod u+x *

ls -lrt
total 4
-rwxrw-r-- 1 nubo nubo 0 Dec 29 19:03 <F11>
-rwxrw-r-- 1 nubo nubo 248 Dec 29 19:07 Test-AR.a
-rwxrw-r-- 1 nubo nubo 0 Dec 29 19:18 example-4
-rwxrw-r-- 1 nubo nubo 0 Dec 29 19:18 example-3
-rwxrw-r-- 1 nubo nubo 0 Dec 29 19:18 example-2
-rwxrw-r-- 1 nubo nubo 0 Dec 29 19:18 example-1


Previous Page
Next Page