If you are looking to change file permissions recursively on on a Linux or Unix-like systems, but you didn’t get an easy way to change it. Here we are going to share something which is crucial when dealing with file access and perform an action. The most arised question is that "Change File Permissions Recursively ( conditional )" There are conditions and suppose need to change files with 777 permissions to 755 in directory etc..? so here you will get the right answer you just need to read this article completely and at the end of the session you will end your queries and enjoy this commands.

In order to perform some actions or change file access permissions, you have to use the chmod command. Commands are predefined so we cannot do any changes in it, we can only perform some action.

It has -R or –recursive option for changing files and directories recursively. In order to find files and directories you need to use the find command. One of the chown commands is used for changing user and group permission.

Just see the chmod Command Examples
By using this-this command, you can set permission to 0755:
# chmod -R 0755 directoryNameHere
In any case, you want to apply for conditional file permissions recursively, so you have to use the combination of both find and chmod command. Find command will find all files in /home/hoststud/demo directory, enter:
# find /home/hoststud/demo -type f -print
Now there is condition where to find all files in /home/hoststud/demo directory with permission 777, enter:
# find /home/hoststud/demo -type f -perm 777 -print
At last, just use new permission using the -exec option as given:
# find /home/hoststud/demo -type f -perm 777 -print -exec chmod 755 {} \;
For Choosing directories and sub-directories use the following syntax:
# find /var/www/html -type d -perm 777 -print -exec chmod 755 {} \;
See given below sample Shell Script by which you can Change Permission Recursively and this is very easy to understand just follow all step by step and all command run accurately.
_dir="${1:-.}"
_fperm="0444"
_dperm="0445"
_ugperm="root:root"
_chmod="/bin/chmod"
_chown="/bin/chown"
_find="/usr/bin/find"
_xargs="/usr/bin/xargs"

echo "I will change the file permission for webserver dir and files to restrctive read-only mode for \"$_dir\""
read -p "Your current dir is ${PWD}. Are you sure (y / n) ?" ans
if [ "$ans" == "y" ]
then
echo "Changing file onwership to $_ugperm for $_dir..."
$_chown -R "${_ugperm}" "$_dir"

echo "Setting $_fperm permission for $_dir directory...."
$_chmod -R "${_fperm}" "$_dir"

echo "Setting $_dperm permission for $_dir directory...."
$_find "$_dir" -type d -print0 | $_xargs -0 -I {} $_chmod $_dperm {}
fi
You can use this script:
./script /var/www/html/
./script /usr/lib/cgi-bin/

Hope you will like the information to change the permission and this all are very easy to understand and execute so just try and change the file permission step by step and it can be said that you have learned how to Change File Permissions Recursively ( conditional ) so just enjoy this article.
Author
Bhawani Singh
First release
Last update
Rating
0.00 star(s) 0 ratings

More resources from Bhawani Singh

Top