-3

I need to change the file permission of all files inside my public_html directory and all the sub directories therein. I have endeavored to use the following command but this only changed the file permission for the files inside the root folder.

chmod -Rv 644 *.php

Any help in this regard would be highly appreciated.

Desper
  • 83
  • 2
  • 11
  • 1
    Just because you're trying to change permissions on PHP files doesn't make this a programming problem - this belongs over at [unix.se] or [su]. – tink Sep 01 '23 at 07:02
  • Well noted sire, My heartfelt apologies for the ignorance – Desper Sep 01 '23 at 07:22

2 Answers2

3

Your command changes the permissions in all entries ending in *.php in your working directory, and if you happen to have a directory where the name ends in .php, it would also go recursively into this directory. However, if you really have a directory named, say, xx.php, your chmod would remove the x-bit, and it would not be possible anymore to cd into this directory after this chmod. This is explained in detail here.

For changing only the files ending in .php, which are in the directory tree below some base directory, do a

find /path/to/basedir -name '*.php' -type f -exec chmod -v 0644 {} +

find is by its nature recursive, and -type f ensures that you leave directories unharmed.

user1934428
  • 19,864
  • 7
  • 42
  • 87
  • 2
    @Fravadona : Good point! – user1934428 Aug 31 '23 at 13:02
  • bro i'm getting this error find: "missing argument to `-exec'" – Desper Sep 01 '23 at 03:15
  • 1
    @Desper: I forgot that, if you use `+` as a terminator, you **have** to use `{}` as placeholder, while if you terminate with `;`, `find` does not request a `{}`. – user1934428 Sep 01 '23 at 05:38
  • bro what does the curely braces imply. what is the purpose of using it. – Desper Sep 01 '23 at 07:20
  • @Desper: See the man-page for `find`: The braces are replaced by the matching files. If you end your command with `;`, the argument is simply put at the end of the command, and you need the braces only if your command requires the argument in between. With `+`, you **must** explicitly put the braces where you need them. – user1934428 Sep 01 '23 at 08:03
  • Understood bro. Thanks for your time appreciate it. – Desper Sep 01 '23 at 13:46
2

You can also do like below,it will recursively search for files with the extension .php inside the public_html directory and its subdirectories and for each file found, it will execute the chmod 644 command to change the file permissions to 644,just don't forget to run this command from the parent directory that contains the public_html directory!!!

thnx to @Fravadona it's better to use + insted of /; because the + symbol executes the command once with all the files as arguments, while the \; symbol executes the command individually for each file.

find public_html -type f -iname "*.php" -exec chmod 644 {} +
Freeman
  • 9,464
  • 7
  • 35
  • 58