Change permission on a file recursively

Hi,

this is the structure of the directory

/local/home/app/cases

under cases directory, below are the sub directories and each directory has files.

/local/home/app/cases/1

/local/home/app/cases/2

/local/home/app/cases/3

/local/home/app/cases/4

File types are .txt .sh and so on. recursively, in each directory, I only like to change the .sh files permission to 744. can someone pls help me on this. thanks:)

find /local/home/app/cases -iname '*\.sh' -type -exec chmod -c 744 {} \;
1 Like

You missed the type:

find /local/home/app/cases -iname '*\.sh' -type f -exec chmod -c 744 {} \;

Escaping the period is not really necessary, and if there is a lot of files, using xargs is more efficient -- it will run only one chmod call on many files. Using -0 will allow for filenames with white characters:

find /local/home/app/cases -iname "*.sh" -type f -print0 | xargs -0 chmod -c 744
1 Like

It is even more efficient to leave xargs out of the mix and let find invoke chmod with multiple operands by using + instead of \; to terminate the list of -exec arguments:

find /local/home/app/cases -iname '*.sh' -type f -exec chmod -c 744 {} +

Note that the find -iname primary and the chmod -c option are extensions to the standards that are not supplied by all implementations of find and chmod . If your system doesn't have these features, as long as you don't have files that end with .sH , Sh , or .SH and don't care about seeing a status message for every file whose permissions were changed, the following command should work on any system that supports the basic requirements of the standards for chmod and find :

find /local/home/app/cases -name '*.sh' -type f -exec chmod 744 {} +

If you do have mixed case filename extensions and your system doesn't support -iname , let us know and we can help you modify the find to look for all four combinations of capitalization using multiple -name primaries.

Just out of curiosity, why do you want these files to have 744 rather than 755 permissions?

2 Likes

man chmod

~/case$ tree
.
 1
    file.sh
    file.txt
 2
    file.sh
    file.txt
 3
     file.sh
     file.txt
~/case$ chmod -c -R 744 {1..3}/*.txt
mode of `1/file.txt' changed from 0755 (rwxr-xr-x) to 0744 (rwxr--r--)
mode of `2/file.txt' changed from 0755 (rwxr-xr-x) to 0744 (rwxr--r--)
mode of `3/file.txt' changed from 0755 (rwxr-xr-x) to 0744 (rwxr--r--)
1 Like

Thanks everyone for their inputs.

the below commands done the job. :slight_smile:

find /local/home/app/cases -iname '*\.sh' -type f -exec chmod -c 744 {} \;