Centos – Change all folder permissions with 1 command

centoschmod

Within my web application directory, how can I change all my folder permissions to rwxr-xr-x (755)? And how can I change all file permissions to rw-r--r-- (644)? I want to change all folders and files including sub-sub files and folders.

I know the following command would change all files and folders but I want to discrimate between a file and a folder.

chmod -R 755 ./my_web_app_project

Best Answer

One way to do exactly what you want with one find command is

find my_web_app_project '(' -type f -exec chmod 644 {} ';' ')' -o '(' -type d -exec chmod 755 {} ';' ')'

-o means OR.  This command processes all objects in the project tree that are of type “file” or type “directory”, and executes different commands based on which type each object is.

You can make this slightly more efficient by replacing the semicolons (';') with plus signs (+); this tells find to run chmod 644 once, with all the plain files’ names as arguments, and to run chmod 755 once, with all the directories’ names as arguments.

By the way, you don’t need to say ./ for anything other than running a program in the current directory (although it does occasionally come in handy for manipulating files whose names begin with some special characters).


I wonder what your permissions look like now (before running a recursive tree-wide chmod).  Files generally don’t get their execute permission bits turned on unless they actually executable programs, or you’ve accidentally done something like chmod -R 755.  Directories generally don’t get their execute permission bits turned off unless you’ve done something catastrophic like chmod -R 644.  If you have badly messed up permissions because you have done an ill-advised chmod -R with a numeric mode, ignore this section and stick with the command at the beginning of this answer.

But, if your permissions are generally sane, and you just want to impose a standard of read/write permissions for the user, and read-only permissions for everybody else (group and world), you can do

chmod -R +rX,u+w,go-w my_web_app_project

in which

  • +rX means +r,+X (note that the X is capitalized — this is important).
    • +r means set read permission for user, group and other.
    • +X means set execute permission for user, group and other if it is currently set for any of them or if the object is a directory.
  • u+w means set write permission for user.
  • go-w means turn off write permission for group and other.

Summary of some key points:

  • Of course you can chmod only things that you own (unless you’re privileged) and that are not read-only.  If there are things in your directory that you cannot chmod, any of these commands will cause an error.
  • The find commands will not do anything to things that are not directories or files (e.g., named pipes).
  • The find command with -exec chmod 755 {} + may fail if you do not already have read and execute permission to all folders under your top level directory.
  • The chmod -R +rX,u+w,go-w command will
    • set all directories to mode 755,
    • ignore symbolic links,
    • for all other file types (including, e.g., named pipes):
      • set it to 755 if it already has at least one x bit; otherwise,
      • set it to 644.