Linux Horizon

How to change file access permission (chmod) recursive

Hacker Emblem

HOW TO CHANGE FILE ACCESS PERMISSION (chmod) RECURSIVE

Introduction: Once I made something wrong (my fault...) and I set chmod 644 for all the files and directories in /var/ftp. And there was 46GB of data. On the server I did not had the Midnight Commander (mc) to repair the damage, so I had to made one line script to restore the rights.

In this example I will use /var/ftp and 755 rights for directories and 644 for files (the default rights).

By using bash scripting:

[root@linuxftp]# cd /var/ftp
[root@linux ftp]# for I in `find -type d` ; do chmod 755 $I ; done
[root@linux ftp]# for I in `find -type f` ; do chmod 644 $I ; done

If you want something more complex (among other things) you can set the deepth of finding (but do not use this script as it is because, probably, your ftp structure is not like my ftp structure):

[root@linux ftp]# cd /var/ftp
[root@linux ftp]# for I in `find -mindepth 1 -maxdepth 3 -type d` ; \
> do chmod 755 $I ; done
[root@linux ftp]# for I in `find -mindepth 1 -maxdepth 3 -type f` ; \
> do chmod 644 $I ; done
For more see the man find and man chmod...

By using the mc:

1. Go to into the folder that is in the top of your structure that want to change (ex.: #cd /var/ftp). I will use the /var/ftp as above...;
2. chmod -R 755 * (ATTENTION !!! Check again where you are before you start this command!)
3. Start mc and go to directory that you want to recursively change (in case that you are not already there), press F9 then go to "Command/Find file" (shortcut for find is ^?) and press OK. The searching will reveal all the files and folders that you have under the directory that you starting the search. Press the "Panelize" button to move the results into the mc panel. Now you can select all the files, F9->Files->Select Group (leave the directories not selected), press ^xc or (F9->File->chmod) and set the desire rights. Press "Mark all" and check the results.
LiNUX Horizon it is a NetXpert Partner

LiNUX Horizon Main Page | LiNUX Horizon Online Shop | LiNUX Horizon Forum


No Banana Union - No Software Patents Say NO to software patents!

Valid XHTML 1.0! Valid CSS! Viewable With Any Browser