Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to change file or directory permission in Linux/Unix?
We know that Linux/Unix is a multiuser operating system where files and directories are associated with permissions so that only authorized users can access the files. The chmod command is used to change the access permission of files or directories.
Syntax
The general syntax of the chmod command is as follows −
chmod [OPTION]... [Mode]... [File]...
The chmod command contains three parameters that help set or change file permissions. Let's discuss each parameter in detail.
Command Options
A brief description of options available in the chmod command −
| Option | Description |
|---|---|
| -c, --changes | Gives a diagnosis for all the files that actually changed |
| -f, --silent, --quiet | Suppresses most error messages |
| -v, --verbose | Gives a diagnosis for all processed files |
| -R, --recursive | Changes files and directories recursively |
| --help | Displays help message and exits |
| --version | Shows version information and exits |
Permission Modes
Permissions can be represented in two different ways −
- Numeric notation − Using octal digits (0-7)
- Symbolic notation − Using letters and symbols
Numeric Notation
In numeric notation, a three-digit octal number (0-7) sequence is used. Each digit represents a different class: first digit for user, second digit for group, and the last one for others.
| Value | Permission |
|---|---|
| 7 | Read, write and execute (rwx) |
| 6 | Read and write (rw-) |
| 5 | Read and execute (r-x) |
| 4 | Read only (r--) |
| 3 | Write and execute (-wx) |
| 2 | Write only (-w-) |
| 1 | Execute only (--x) |
| 0 | No permissions (---) |
Symbolic Notation
Symbolic notation uses letters to specify permissions. The format is [who][operator][permissions].
| Who | Operator | Permission |
|---|---|---|
|
u = user (owner) g = group o = others a = all |
+ = add permission - = remove permission = = set exact permission |
r = read w = write x = execute |
Examples
Checking Current Permissions
First, check the current permissions of a file −
$ ls -l file.txt
-rw-r--r-- 1 user group 1024 Jan 15 10:30 file.txt
Using Numeric Notation
Make a file readable only by the owner −
$ chmod 400 file.txt
Give full permissions to owner, read and execute to group and others −
$ chmod 755 file.txt
Using Symbolic Notation
Add read permission for the user −
$ chmod u+r file.txt
Remove write permission for group and others −
$ chmod go-w file.txt
Set exact permissions for all users −
$ chmod a=rx file.txt
Recursive Permission Changes
Change permissions for a directory and all its contents −
$ chmod -R 755 /path/to/directory
Common Permission Patterns
| Numeric | Symbolic | Description |
|---|---|---|
| 755 | rwxr-xr-x | Owner: full access, Others: read and execute |
| 644 | rw-r--r-- | Owner: read/write, Others: read only |
| 600 | rw------- | Owner: read/write, Others: no access |
| 777 | rwxrwxrwx | Everyone: full access (not recommended) |
Conclusion
The chmod command is essential for managing file and directory permissions in Linux/Unix systems. Understanding both numeric and symbolic notation allows you to precisely control who can read, write, or execute your files, ensuring proper security and access control.
