How to change bulk files permission recursively

Two years back, when I had been worked in hosting company, we had faced lot of issues on daily basis. One time all the wordpress site’s file permission got changed from 644 to 755. That time we have used below commands to recover the site one by one. Make sure its very very critical command if you run this command wrongly on home you will be face lots of problem and very critical to revert back. So very carefully while running the command.

For our convenient, I am going to change the “mageshm” user account files permission from 644 to 755 & 777 for testing purpose. To perform this action we need to combine find & chmod commands and do it.

Common Syntax :

The common syntax for bulk file permission change.

Syntax :# find /path/to/folder -type f -perm [Current file permission] -print -exec chmod [Permission need to be change] {} \;

Details :

1) The first argument is the path to folder (Mention your path according that)
2) The second argument is type (If you want to change the files permission you need to use “f”, for folders use “d”)
3) To check given permission.
4) Print the given permission files.
5) The fifth argument (-exec) change a file permission (-chmod [Permission need to be change]), If anything found by find command.

1) See the file permission of mageshm account.

I have changed the “mageshm” user account files permission recursively from 644 to 755 & 777 for testing purpose.

  • 5 files having 755 permission under mageshm account
  • 2 files having 777 permission under mageshm account
[email protected] [/home/mageshm]# ll -h
total 861M
drwx------.  6 mageshm mageshm 4.0K Jan  7 03:41 ./
drwx--x--x. 16 root    root    4.0K Jan  7  2014 ../
-rw-------.  1 mageshm mageshm 1.4K Jan  7 03:18 .bash_history
-rw-r--r--.  1 mageshm mageshm   18 May 11  2012 .bash_logout
-rw-r--r--.  1 mageshm mageshm  176 May 11  2012 .bash_profile
-rw-r--r--.  1 mageshm mageshm  124 May 11  2012 .bashrc
-rw-r--r--.  1 root    root    649M Nov 29 22:39 CentOS-6.5-x86_64-LiveCD.iso
-rw-r--r--.  1 root    root      26 Dec 27 17:55 liks
drwxr-xr-x.  3 mageshm mageshm 4.0K Dec 31 14:24 links/
drwxrwxr-x.  2 mageshm mageshm 4.0K Nov 16 15:44 perl5/
drwxr-xr-x.  2 mageshm mageshm 4.0K Nov 16 15:37 public_ftp/
drwxr-xr-x.  3 mageshm mageshm 4.0K Nov 16 15:37 public_html/
lrwxrwxrwx.  1 root    root      31 Dec  7 15:11 s-link-file -> /links/soft-link/test-soft-link
lrwxrwxrwx.  1 root    root      38 Dec  7 15:12 s-link-folder -> /links/soft-link/test-soft-link-folder
-rwxr-xr-x.  1 root    root    100M Jan 31  2006 test100.dat* (755)
-rw-r--r--.  1 root    root    101M Dec 30  2012 test100.zip
-rw-r--r--.  1 root    root     12M Dec 30  2012 test10.zip
-rwxr-xr-x.  1 root    root      61 Dec 27 19:05 test2-bzip2.txt* (755)
-rwxr-xr-x.  1 root    root      61 Dec 31 14:24 test3-bzip2.txt* (755)
-rwxrwxrwx.  1 root    root      60 Dec 27 19:01 test-bzip2.txt* (777)
[email protected] [/home/mageshm]# cd links

[email protected] [/home/mageshm/links]# ll -h
total 16K
drwxr-xr-x. 3 mageshm mageshm 4.0K Dec 31 14:24 ./
drwx------. 6 mageshm mageshm 4.0K Jan  7 03:41 ../
drwxr-xr-x. 3 mageshm mageshm 4.0K Dec 31 14:25 soft-link/
-rwxr-xr-x. 1 root    root      60 Dec 27 19:40 test-bzip2.txt* (755)
[email protected] [/home/mageshm/links]# cd soft-link

[email protected] [/home/mageshm/links/soft-link]# ll -h
total 16K
drwxr-xr-x. 3 mageshm mageshm 4.0K Dec 31 14:25 ./
drwxr-xr-x. 3 mageshm mageshm 4.0K Dec 31 14:24 ../
-rwxrwxrwx. 1 root    root      61 Dec 27 19:40 test2-bzip2.txt* (777)
drwxr-xr-x. 2 mageshm mageshm 4.0K Dec 31 14:25 test-soft-link-folder/
[email protected] [/home/mageshm/links/soft-link]# cd test-soft-link-folder

[email protected] [/home/mageshm/links/soft-link/test-soft-link-folder]# ll -h
total 12K
drwxr-xr-x. 2 mageshm mageshm 4.0K Dec 31 14:25 ./
drwxr-xr-x. 3 mageshm mageshm 4.0K Dec 31 14:25 ../
-rwxr-xr-x. 1 root    root      61 Dec 27 19:40 test1-bzip2.txt* (755)

2) How to find all the files with 755 file permission /home/mageshm directory.

Use the below command to find all the files with 755 permission.

[email protected] [~]# find /home/mageshm -type f -perm 755 -print
/home/mageshm/test100.dat
/home/mageshm/links/test-bzip2.txt
/home/mageshm/links/soft-link/test-soft-link-folder/test1-bzip2.txt
/home/mageshm/test2-bzip2.txt
/home/mageshm/test3-bzip2.txt

The above output is showing 5 files having 755 permission under “/home/mageshm” directory.

3) How to find all the files with 777 file permission /home/mageshm directory.

Use the below command to find all the files with 777 permission.

[email protected] [~]# find /home/mageshm -type f -perm 777 -print
/home/mageshm/test-bzip2.txt
/home/mageshm/links/soft-link/test2-bzip2.txt

The above output is showing 2 files having 777 permission under “/home/mageshm” directory.

4) How to change the bulk file permission from 755 to 644.

To perform this action we need to combine find & chmod commands and do it. Like below.

[email protected] [~]# find /home/mageshm -type f -perm 755 -print -exec chmod 644 {} \;
/home/mageshm/test100.dat
/home/mageshm/links/test-bzip2.txt
/home/mageshm/links/soft-link/test-soft-link-folder/test1-bzip2.txt
/home/mageshm/test2-bzip2.txt
/home/mageshm/test3-bzip2.txt

Here i’m going to check whether the file permission got changed or not. See the output.

[email protected] [~]# find /home/mageshm -type f -perm 755 -print

The above command not showing anything, Its mean none of the files having 755 permission under “/home/mageshm” directory and their subdirectory.

5) How to change the bulk file permission from 777 to 644.

To perform this action we need to combine find & chmod commands and do it. Like below.

[email protected] [~]# find /home/mageshm -type f -perm 777 -print -exec chmod 644 {} \;
/home/mageshm/test-bzip2.txt
/home/mageshm/links/soft-link/test2-bzip2.txt

Here i’m going to check whether the file permission got changed or not. See the output.

[email protected] [~]# find /home/mageshm -type f -perm 777 -print

The above command not showing anything, Its mean none of the files having 777 permission under “/home/mageshm” directory and their subdirectory.

We are preparing all articles in-depth to understand by all level/stage Linux administrators. If the article is useful for you, then please spend less than a minute to share your valuable comments in our commenting section.

Please stay tune with us…Good Luck 🙂

Magesh Maruthamuthu

Love to play with all Linux distribution

You may also like...