How to Change Bulk File Permissions Recursively from “abc” to “xyz” in Linux

As Linux administrator, we always use chmod command to change file permissions in Linux.

It may happens many times in a day, it depends on your environment size and team size.

It could be a single file or multiple files. If it’s in the same directory, you may need to use chmod command with file name and new file permission to be applied.

If it’s applicable for sub-directories as well then you may need to use recursive option in that.

What would be an option if you want to change bulk file permissions recursively from “abc” to “xyz”?

Say for example, i would like to change the set of files which has 0666 permission to 0644 permission in my current directory.

Yep, it’s very easy and not a big deal but we need to combine multiple commands and options with find command to achieve this.

Let’s see the examples to understand this in better way.

To experiment this, we are going to use the following two folders and their files. To do so, we need to use ls command in Linux.

$ ls -lh $(pwd)/*
-rw-r--r-- 1 daygeek daygeek  177 Aug 16 16:59 /home/daygeek/shell-script/backup/service1a.sh
-rw-r--r-- 1 daygeek daygeek  157 Aug 16 16:59 /home/daygeek/shell-script/backup/service1.sh
-rw-r--r-- 1 daygeek daygeek  324 Aug 16 16:59 /home/daygeek/shell-script/backup/service2a.sh
-rw-r--r-- 1 daygeek daygeek  312 Aug 16 16:58 /home/daygeek/shell-script/backup/service2.sh
-rw-r--r-- 1 daygeek daygeek  361 Aug 16 16:58 /home/daygeek/shell-script/backup/service3a.sh
-rw-r--r-- 1 daygeek daygeek  341 Aug 16 16:58 /home/daygeek/shell-script/backup/service3.sh
-rw-r--r-- 1 daygeek daygeek  296 Aug 16 16:58 /home/daygeek/shell-script/backup/servicem.sh
-rw-r--r-- 1 daygeek daygeek  288 Aug 16 16:57 /home/daygeek/shell-script/backup/service.sh

/home/daygeek/shell-script/backup/old:
total 20K
-rwxr-xr-x 1 daygeek daygeek 237 Aug 19 00:48 mysql_backup_1.sh
-rwxr-xr-x 1 daygeek daygeek 241 Aug 19 00:48 mysql_backup_2.sh
-rwxr-xr-x 1 daygeek daygeek 761 Aug 19 00:48 mysql_backup.sh
-rwxr-xr-x 1 daygeek daygeek  98 Aug 19 00:48 passwd-up1.sh
-rwxr-xr-x 1 daygeek daygeek 159 Aug 19 00:48 passwd-up.sh

How to Change File Permissions in Linux?

In this example, we are going to setting up 0666 permission to service.sh file.

Changing file permission in Linux is a piece of cake as you can see the same below.

$ chmod 666 /home/daygeek/shell-script/backup/service.sh

$ ls -lh /home/daygeek/shell-script/backup/service.sh
-rw-rw-rw- 1 daygeek daygeek 288 Aug 16 16:57 /home/daygeek/shell-script/backup/service.sh

How to Change Multiple File Permissions in Linux?

This example also similar to above but we will be adding more than one file at the same time.

$ chmod 666 /home/daygeek/shell-script/backup/service1.sh /home/daygeek/shell-script/backup/service1a.sh

$ ls -lh /home/daygeek/shell-script/backup/service1.sh /home/daygeek/shell-script/backup/service1a.sh
-rw-rw-rw- 1 daygeek daygeek 177 Aug 16 16:59 /home/daygeek/shell-script/backup/service1a.sh
-rw-rw-rw- 1 daygeek daygeek 157 Aug 16 16:59 /home/daygeek/shell-script/backup/service1.sh

How to Change File Permissions Recursively in Linux?

This example describes how to change file permission recursively.

It’s pathetic, why. Why it’s not working as expected. Because this has changed the directory permission in the first place.

Directory should have “executable” permission, which allow users to navigate to inside a directory or sub-directory.

$ chmod -R 0644 /home/daygeek/shell-script/backup/

chmod: cannot access '/home/daygeek/shell-script/backup/service2.sh': Permission denied
chmod: cannot access '/home/daygeek/shell-script/backup/servicem.sh': Permission denied
chmod: cannot access '/home/daygeek/shell-script/backup/service2a.sh': Permission denied
chmod: cannot access '/home/daygeek/shell-script/backup/service1a.sh': Permission denied
chmod: cannot access '/home/daygeek/shell-script/backup/service3.sh': Permission denied
chmod: cannot access '/home/daygeek/shell-script/backup/service3a.sh': Permission denied
chmod: cannot access '/home/daygeek/shell-script/backup/old': Permission denied
chmod: cannot access '/home/daygeek/shell-script/backup/service1.sh': Permission denied
chmod: cannot access '/home/daygeek/shell-script/backup/service.sh': Permission denied

What is the best options to do this. Yes, there are alternatives and everyone suggest to use find command.

Yes, it’s a good solution and we can use it. What I’m asking is, does any option is available in chmod command to overcome this issue?

Most of us straight away say no option is available for this in chmod but i don’t want to say no since option is available to overcome this.

If yes, how. Can you show us the example for this.

Yes, of course, let’s see the below example.

$ chmod -R u=rwX,go=rX /home/daygeek/shell-script/backup/

$ ls -lh $(pwd)/*
-rwxr-xr-x 1 daygeek daygeek  177 Aug 16 16:59 /home/daygeek/shell-script/backup/service1a.sh
-rwxr-xr-x 1 daygeek daygeek  157 Aug 16 16:59 /home/daygeek/shell-script/backup/service1.sh
-rwxr-xr-x 1 daygeek daygeek  324 Aug 16 16:59 /home/daygeek/shell-script/backup/service2a.sh
-rwxr-xr-x 1 daygeek daygeek  312 Aug 16 16:58 /home/daygeek/shell-script/backup/service2.sh
-rwxr-xr-x 1 daygeek daygeek  361 Aug 16 16:58 /home/daygeek/shell-script/backup/service3a.sh
-rwxr-xr-x 1 daygeek daygeek  341 Aug 16 16:58 /home/daygeek/shell-script/backup/service3.sh
-rwxr-xr-x 1 daygeek daygeek  296 Aug 16 16:58 /home/daygeek/shell-script/backup/servicem.sh
-rwxr-xr-x 1 daygeek daygeek  288 Aug 16 16:57 /home/daygeek/shell-script/backup/service.sh

/home/daygeek/shell-script/backup/old:
total 20K
-rwxr-xr-x 1 daygeek daygeek 237 Aug 19 00:48 mysql_backup_1.sh
-rwxr-xr-x 1 daygeek daygeek 241 Aug 19 00:48 mysql_backup_2.sh
-rwxr-xr-x 1 daygeek daygeek 761 Aug 19 00:48 mysql_backup.sh
-rwxr-xr-x 1 daygeek daygeek  98 Aug 19 00:48 passwd-up1.sh
-rwxr-xr-x 1 daygeek daygeek 159 Aug 19 00:48 passwd-up.sh

Yup, it did the job nicely. Can you explain this. I don’t see any option for this in chmod command, however, they had mentioned this in the description area and the same can be verfied by navigating to the man page of the chmod.

What X does in this example. See the details below.

X : Set an execute bit if the file is a directory or already has execute permission for some user.

How to Change Bulk File Permissions Recursively from “abc” to “xyz” in Linux?

As i told in the beginning of the article, we need to combine few commands together to achieve this.

Let’s see the examples.

If you would like to print files that has 755 permission, use the following format.

$ find /home/daygeek/shell-script/backup/ -type f -perm 755 -print

/home/daygeek/shell-script/backup/service2.sh
/home/daygeek/shell-script/backup/servicem.sh
/home/daygeek/shell-script/backup/service2a.sh
/home/daygeek/shell-script/backup/service1a.sh
/home/daygeek/shell-script/backup/service3.sh
/home/daygeek/shell-script/backup/service3a.sh
/home/daygeek/shell-script/backup/old/mysql_backup_1.sh
/home/daygeek/shell-script/backup/old/passwd-up1.sh
/home/daygeek/shell-script/backup/old/mysql_backup_2.sh
/home/daygeek/shell-script/backup/old/mysql_backup.sh
/home/daygeek/shell-script/backup/old/passwd-up.sh
/home/daygeek/shell-script/backup/service1.sh
/home/daygeek/shell-script/backup/service.sh

Run the following command to change the set of files that has 755 permission to 644 permission in the given directory.

$ find /home/daygeek/shell-script/backup/ -type f -perm 755 -exec chmod 644 {} \;

Details:

  • find: find is a command
  • [Path]: It is the path to folder (Input your path)
  • -type f: It is type of file (Use “f” for file and “d” for directory)
  • -perm 755: Get a list of files that has 755 permission.
  • -exec chmod 644: It changes the list of files that has 755 permission to 644 permission.
  • {} It holds all the files that has 755 permissions.
  • \; This used to exit the command once it’s done the job.

The same can be verified by running the following command.

$ ls -lh $(pwd)/*

-rw-r--r-- 1 daygeek daygeek  177 Aug 16 16:59 /home/daygeek/shell-script/backup/service1a.sh
-rw-r--r-- 1 daygeek daygeek  157 Aug 16 16:59 /home/daygeek/shell-script/backup/service1.sh
-rw-r--r-- 1 daygeek daygeek  324 Aug 16 16:59 /home/daygeek/shell-script/backup/service2a.sh
-rw-r--r-- 1 daygeek daygeek  312 Aug 16 16:58 /home/daygeek/shell-script/backup/service2.sh
-rw-r--r-- 1 daygeek daygeek  361 Aug 16 16:58 /home/daygeek/shell-script/backup/service3a.sh
-rw-r--r-- 1 daygeek daygeek  341 Aug 16 16:58 /home/daygeek/shell-script/backup/service3.sh
-rw-r--r-- 1 daygeek daygeek  296 Aug 16 16:58 /home/daygeek/shell-script/backup/servicem.sh
-rw-r--r-- 1 daygeek daygeek  288 Aug 16 16:57 /home/daygeek/shell-script/backup/service.sh

/home/daygeek/shell-script/backup/old:
total 20K
-rw-r--r-- 1 daygeek daygeek 237 Aug 19 00:48 mysql_backup_1.sh
-rw-r--r-- 1 daygeek daygeek 241 Aug 19 00:48 mysql_backup_2.sh
-rw-r--r-- 1 daygeek daygeek 761 Aug 19 00:48 mysql_backup.sh
-rw-r--r-- 1 daygeek daygeek  98 Aug 19 00:48 passwd-up1.sh
-rw-r--r-- 1 daygeek daygeek 159 Aug 19 00:48 passwd-up.sh

How to Change Bulk File Permissions Recursively from “abc” to “xyz” Excluding Certain Folder in Linux?

This example allows you to exclude the certain folder while performing this action.

$ find /home/daygeek/shell-script/backup/ -not -path "/home/daygeek/shell-script/backup/old/*" -type f -perm 755 -exec chmod 644 {} \;
or
$ find /home/daygeek/shell-script/backup/ -not -path "*/old/*" -type f -perm 755 -exec chmod 644 {} \;

How to Change Bulk File Permissions Recursively from “abc” to “xyz” Excluding Certain Folder and Pattern Matching Files in Linux?

This example allows you to exclude the certain folder and pattern matching files while performing this action.

$ find /home/daygeek/shell-script/backup/ -not -path "*/old/*" \( -iname "*.txt" \) -type f -perm 644 -exec chmod 755 {} \;

About Magesh Maruthamuthu

Love to play with all Linux distribution

View all posts by Magesh Maruthamuthu

Leave a Reply

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