Change permissions for multiple files Linux

You can change permissions for all files in a directory.

What if you want to change permissions for a lot of files? For example, suppose you want to extend read/write access to your group, for all files in the directory.

Of course, one thing you can do is to run chmod on all of them individually. You can also use the wildcard character (*) to change all files in the directory:

    % chmod g+rw *

The problem with this is that you won’t change any files in subdirectories. For example, suppose you’re in your home directory, and you have a Mail subdirectory in your home directory. Running chmod would change the permissions to all the files in your home directory:

    % ls -l
    total 26
    drwxr-x--x 2 lmui 512 Dec 14 17:54 Mail
    -rw-r----1 lmui 20181 Dec 14 17:54 mbox
    -rw-r----1 lmui 867 Dec 14 17:54 people
    -rw------1 lmui 249 Dec 14 17:54 updates
    % chmod g+rw *
    % ls -l
    total 26
    drwxrwx--x  2 lmui 512 Dec 14 17:54 Mail
    -rw-rw--- 1 lmui 20181 Dec 14 17:54 mbox
    -rw-rw--- 1 lmui 867 Dec 14 17:54 people
    -rw-rw--- 1 lmui 249 Dec 14 17:54 updates

However, files in the Mail subdirectory remain unchanged.

    % ls -l Mail
    total 184
    -rw------ 1 lmui 105808 Dec 14 17:54 lamb
    -rw------ 1 lmui 37010 Dec 14 17:54 tim
    -rw------ 1 lmui 35184 Dec 14 17:54 val

Luckily, there’s a command-line option for changing permissions in subdirectories (recursively). You can use the -R option to chmod to change the permissions for all subdirectories. For example:

    % chmod -R g+rw *

I have a directory with numerous files. Part of the files have the 755 permissions and the other part have 644 permissions. I'd like to convert the files with 755 permissions to 644. I have tried the following line by running it from the directory itself:

find . -perm 755 -exec chmod 644 {}\;

However as a result, the permission changed only for the directory itself and after changing it back I found out that the files permissions remained unchanged. Do I miss something?

Change permissions for multiple files Linux

sr_

14.9k47 silver badges55 bronze badges

asked May 22, 2012 at 7:49

1

Ok, it seems that I've found the problem. It seems that there must be a mandatory space between the {} and \;, so the command will look like this:

find . -perm 755 -exec chmod 644 {} \;

Rather than:

find . -perm 755 -exec chmod 644 {}\;

Also the issue with changing the directory permissions can be solved by adding a -type f flag, so it'll look as follows:

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

answered May 22, 2012 at 8:07

Eugene SEugene S

3,3448 gold badges28 silver badges35 bronze badges

2

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 {} \;

Can you chmod multiple files at once?

If you need to change a file permission, use the chmod command. It also allows to change the file permission recursively to configure multiple files and sub-directories using a single command.

What is the difference between chmod 755 and 777?

A 777 permission on the directory means that everyone has access to read/write/execute (execute on a directory means that you can do an ls of the directory). 755 means read and execute access for everyone and also write access for the owner of the file.

What does the 777 in chmod mean?

Some file permission examples: 777 - all can read/write/execute (full access). 755 - owner can read/write/execute, group/others can read/execute. 644 - owner can read/write, group/others can read only.