Home Command line tools What Does chmod -R Do?

What Does chmod -R Do?

by The Linux Digest Guy
Recursively change permissions

When you first start using Linux or any other Unix-like system, the commands and the various parameters can become confusing. Searching for information can be even more confusing when you have to wade through a detailed technical manual just to get the meaning of one single character option.

This is why I have been writing some short articles that explain one option at a time, like the -R option in chmod. I sincerely hope it helps someone get the information they need.

The simple answer is that chmod -R asks chmod to recursively change all directories and files below. On most systems only upper case -R is used. Lower case -r does nothing.

What is chmod?

Before diving into the -R option, it’s essential to grasp the basics of chmod. Chmod stands for Change Mode. This command is used to set or modify the access permissions of files and directories. Permissions dictate who can read, write, or execute a file. Knowing how to use chmod effectively ensures that your files are accessible to the right users while being protected from unauthorized access.

What Does -R Stand for?

The R option in chmod stands for “recursive”. When applied, chmod -R changes the permissions of the specified directory and all its subdirectories and files. This is particularly useful when you want to apply the same permission settings to an entire directory tree. However, it’s a powerful tool that should be used with caution to avoid unintentionally changing the permissions of critical files.

Practical Usage of chmod -R

  • Setting Permissions: To set permissions recursively, you can combine chmod -R with symbolic or numeric permission values. For instance, chmod -R 755 directoryName will set the read, write, and execute permissions for the owner and read and execute permissions for the group and others for the directory and all its contents.
  • Caution and Best Practices: Always double-check the directory path and permissions you’re setting. A small mistake with chmod -R can lead to significant security issues or data loss. It’s recommended to test the command on a small, non-critical directory first. If you set the same permission on every file and subdirectory in a large directory like the /usr/ directory, you would definitely cause problems or security issues in your system.

Simple Example of chmod -r

Take this directory below as an example:

Image showing a directory tree

As you can see from the image the “others” group has no permissions on the files or subdirectories. In stead of changing all the the files one by one, you can change all of them with a single command:

chmod -R o+rw example/

You can see from the next image that the read and write permissions have been added to every file and subdirectory.

Copy Permissions Between Groups of Users

In some cases it might not be practical to set the permissions the same for every type of file or directory. Say you want to enable the “others” group to read and write files and also execute those that are executable. One way to do this is to copy the permissions from the user or the users group to the others group.

This command would accomplish that by copying the group permissions for every file to the others field:

chmod -R o=g example/

This way the file example/subdir1/script1.sh is also executable for others.

Directory tree showing permissions after copying permissions between groups.

Common Scenarios and Troubleshooting

  • Adjusting Project Permissions: Often, you’ll need to set consistent permissions for all files in a project. chmod -R simplifies this process.
  • Troubleshooting Permission Issues: If you encounter permission-related errors, carefully review the permissions of your directories and files. Remember, chmod -r affects everything within the directory tree, so use it judiciously.

If you made it this far, you should have a thourough understanding of the recursion option in chmod. I hope this article helped you solve whatever problem you were trying to solve. I you want to explore more about chmod, I have a couple more articles about it, explaining different situations like “-rw-r–r–“: Step by Step Explanation or drwxrwxrwT: The meaning of the “T” at the end.

2

Related Posts

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

Privacy & Cookies Policy