Change permissions for Directory & Sub-directories in Linux

In this article we will discuss different ways to change the permissions of a folder and all files or subdirectories inside it in Linux.

Table Of Contents

Introduction

We often come across the “Permission denied” error when we are trying to edit the files in the Linux operating system. This is because Linux operating system protects files so that only authorized users will be able to access files and directories. It’s very difficult to change permissions of each file manually and luckily Linux operating system provides a set of commands to easily change the file permissions. Let’s looks at various ways in which we can change the file permissions

When we get “Permission Denied” error, first step is to check the permissions of the file. We can do that just by using the below command.

Syntax:

ls -lrt <File/Directory Name>

Example:

ls -lrt www

Output :

drwxr-xr-x. 5 opc opc 43 Aug 21 18:25 web

Before understanding the output of the above command, we will look at permission types.

Permission Types

  • Read – This will give the ability for the user to read contents of the file.
  • Write – This will give the ability for the user to write or modify the contents of a file/directory
  • Execute – This will give the ability for the user to execute a file or view content of a directory

Now lets understand the output of “ls -lrt” command i.e.

drwxr-xr-x. 5 opc opc 43 Aug 21 18:25 web
  • First character will be used to identify if it’s a directory or file. “d” means directory, “l” means symbolic link and “-” means it a file
  • Next three characters (2-4) will be used to identify the permissions of the file/directory owner. In this case the owner has read (r), write (w) and execute (x) permissions
  • Next three characters (5-7) will be used to identify the permissions of the group in which owner is part of. In this case the group has read (r) and execute (x) permissions
  • Next three characters (8-10) will be used to identify the permissions of all other users. In this case the other users have read (r) and execute (x) permissions

Once we understand the permissions that are assigned to a directory/file, we can easily change their permissions using below commands.

Change permissions of a Directory & all files/sub-directories in it using chmod command:

The Chmod (change mode) is one of the most widely used command in linux operating system to change the file permissions. The general syntax of the command is shown below

Syntax:

chmod -R <permissions> <file/directory name>

Permissions Parameters are,

-R : recursively apply the permissions
-v : output a diagnostic for every file processed
-f : suppress error messages

While setting the permissions we have an option to use two notations. One is to explicitly define permission using (rwx) notation or we can also use binary references (octal notation) to set the permissions of a file/directory. Following is the binary representation of “rwx” string

  • r = 4
  • w = 2
  • x = 1

Let’s see some examples, where we will change the permissions of all files and sub-folders inside a folder.

Example:1

If we want to give full permissions to a folder and all it’s sub folders to only a specific user and read only access to others then we use the below command,

chmod -R 744 www
ls -lrt

Output :

-rw-rw-r--. 1 opc opc  0 Aug  7 20:14 tutorial.sh
drwxr--r--. 3 opc opc 17 Aug 21 18:24 www

Using the above command we have specified who can list the files (r), create or update files in a directory (W) and traverse (x) into the directory.

Example:2

If we want to add write permission to a group using octal notation then we can use the below command,

chmod -R g+w www
ls -lrt

Output :

-rw-r--r--. 1 opc opc  0 Aug  7 20:14 tutorial.sh
drwxrwxr-x. 3 opc opc 17 Aug 21 18:24 www

Using the above command we gave write permissions to group on www and all sub directories within www folder.

Find and Change Permissions of selected files/sub-directories inside a diretory

The find command can also be used to search directories/files that matches a specific pattern and then change the permissions

Syntax:

find . -type <d/f> -exec <command>

Examples:

If we want to give 755 permission to all directories, but we want to give only 644 to files then we can use the below command

find . -type d -exec chmod 755 {} \;
find . -type f -exec chmod 644 {} \;

Output :

-rw-r--r--. 1 opc opc  0 Aug 27 04:13 tutorial2.txt
-rw-r--r--. 1 opc opc  0 Aug  7 20:14 tutorial.sh
drwxr-xr-x. 3 opc opc 17 Aug 21 18:24 www

Summary:

In this guide, we have learnt how to change permissions of a directory and it’s subdirectories recursively using chmod as well as find command. When we want to update permissions to a specific set of files the find command can be used along with chmod to change permissions of a file/directory. Thanks.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top