Manage Directory and File Permissions with Chmod Recursive

choubertsprojects

The Best WordPress plugins!

1. WP Reset

2. WP 301 Redirects

3. WP Force SSL

A couple of weeks ago, I wrote about the importance of file permissions in Linux. As it turns out, the command chmod is a powerful utility for managing these file and directory permissions. Here’s how to use this tool:

The “chown recursive” is a command-line tool that allows users to manage directory and file permissions with the chmod command.

Manage Directory and File Permissions with Chmod Recursive

Are you sick of manually changing file and directory permissions? Are you seeking for a quicker, more efficient method to change the permissions on your files and directories? If that’s the case, you’ve come to the right place since this article will teach you how to recursively alter file and directory permissions using the command chmod!

Continue reading to find out more!

Prerequisites

To follow along, you’ll need a Linux device and a directory with practice rights for files and subdirectories.

Understanding Linux Users and Groups

In Linux, owners and groups are essential for securely managing files and directories. The owner is the person to whom a file or directory is allocated, which is usually the creator. Files and folders in Linux are also divided into groups. The chown command controls both users and groups.

The material highlighted in green in the picture below displays the person who owns the file or directory, in this case mihail. The material highlighted in blue indicates to which group the file or directory belongs, in this case the staff group.

The owner and group of a collection of files and folders are listed. The owner and group of a collection of files and folders are listed.

Users and File Permissions: A Windows Guy in a Linux World

Permissions for Linux Files and Directories

Permissions define what actions a person or group may do on a certain file or directory. They have a character representation and a numerical value given to them.

  • Read (r or 4): Indicates whether the contents of the file or directory may be read by a user or group. For all parties, the read permission is provided by default to all newly created files and folders.
  • Write (w or 2): Indicates whether a person or group has the ability to alter an object’s contents. Only the owner of a file or directory has this permission by default.
  • Execute (x or 1): Indicates whether a person or group has the ability to execute a file or instructions inside a directory. Only directories and not files have the execute permission by default.

If you don’t provide the user (u) execute access to a directory, they won’t be able to display its contents since they won’t be able to run commands inside it!

The result of the command ls -l is provided below as an example of the various permission categories. A directory is indicated by permissions preceded by the d value.

Permissions for files and directories are listed. Permissions for files and directories are listed.

So why do each item seem to have three sets of permission? Granular permission control is performed by splitting permissions into the portions (referred to as parties in this tutorial) shown below.

  • The owner of a file or directory is indicated by the letter u, which is highlighted in blue.
  • Green highlights the members of the group to which a file or directory belongs.
  • Other (o): All other users and groups that have not been expressly allocated are marked in red.

Using a file or directory to demonstrate the combined permissions. Using a file or directory to demonstrate the combined permissions

Using the chmod recursive command to change file permissions

Continue reading to discover how to adjust file permissions with a basic grasp of Linux permissions. The chmod recursive command is used to change permissions, as seen in the examples below.

1. Use the ls -l command to examine the current permissions. The file my dir/index.js is shown here.

Checking index.js's current permissions. Checking index.js’s current permissions.

2. Use the chmod command with the a (all) party and the rwx (read/write/execute) permissions. chmod a=rwx index.js is the full command.

Changing index.js' permissions. Changing the permissions of index.js.

3. As you may see, chmod recursive does not provide any results. Run the command ls -l again to confirm that permissions have changed.

Checking the index.js permissions Checking the index.js permissions

Using Numeric Values to Control File Permissions

Remember the numbers associated with read and write permissions? Instead of supplying character values, you may use a number to define certain rights. You may build a custom permission by combining the values, such as 6, which is a mix of write (2) and read (4) permissions.

Set permissions for the main.py file using the command chmod 664 main.py in the example below. The following is a breakdown of the numerical values.

  • Reading and writing (6).
  • Read and Write in a Group (6).
  • Read something else (4).

Main.py permissions are being changed. Main.py permissions are being changed.

Verify the updated permissions for the main.py file using the command ls -l once the permissions have been adjusted.

main.py permissions are being checked.. main.py permissions are being checked.

Removing and Adding Permissions to Files

To establish specific permissions, the non-numerical commands you’ve executed so far utilized the assignment argument (=) with chmod recursive. Instead, you may modify existing permission sets by removing (-) or adding (+) permissions.

1. Use the command ls -l README.md to list the existing permissions for the README.md file.

Checking the current permissions in README.md. Checking the current permissions in README.md

2. Then, using the command chmod u+x,o-r README.md, add (+) execute (x) rights to the user party (u), while deleting (-) read (r) capabilities from the other party (o). The permits of different parties are separated by commas (,).

README.md permissions change. README.md permissions change

3. Verify that permissions have been changed using the ls -l README.md command.

Checking new permissions in README.md Checking new permissions in README.md

Changing File and Directory Permissions in a Recursive Way

Even if you’re progressing, you’re only working on one file at a time. What if you need to update permissions on a huge number of files? It’s time to boost your game and learn how to modify multiple file permissions.

Running chmod recursive with the -R (recursive, not capital) option is one way to update numerous files. The recursive option changes the permissions of all files on a specified path, including those in subdirectories.

1. Run the chmod -R a=r,u=rwx my dir command. You’ve probably seen most of the choices before.

Using the recursive option, this program will alter the permissions for all files in the directory, my dir, including subdirectories (-R). The files are set to readable (r) for everyone (a), with different permissions set to full permissions for the current user (u) (read, write, execute).

The resultant permissions are shown in the image below after running the command with no output.

new permissions for my dir files new permissions for my dir files

2. Assume you perform the command chmod -R a=rwx my dir on the same directory, my dir, as previously. You’re updating all files in my dir and its subdirectories to have (a) full permissions once again (read, write, execute). Because chmod recursive does not produce output, here is the outcome of the command.

new permissions for my dir files to confirm every part now has every permission. new permissions for my dir files to confirm every part now has every permission

3. What about the numerical approach? The recursive approach, chmod -R 770 my dir, also works with numeric permissions, as illustrated below. You’re granting the user and group all access, but no permissions to the other party.

Using the numeric approach to recursively change permissions Using the numeric approach to recursively change permissions

4. Use ls -l to verify that the numeric technique is used to set permissions on my dir files and subdirectories.

Using the numeric approach to check the permissions specified once again. Using the numeric approach to check the permissions specified once again.

Using Special Permissions to Define File and Folder Behavior

Special permissions provide extra rights not available in the normal permission sets. Three unique permissions are available. This article explains how special permissions operate and how to add them to a file or directory.

Permissions for the SUID (User + S)

It is a specific authorization for the user, known as SUID. The SUID has just one function: regardless of who is running the file, it always runs as the person who owns it.

Consider the index.js file. Run chmod u+s index.js to provide the extra SUID permission.

If you run ls -l index.js now, you’ll see that the user’s permissions have a s instead of an x.

examining the new permissions in index.js examining the new permissions in index.js

Permissions for the SGID (Group + S)

This special permit, often known as SGID, serves two purposes:

  • If this option is enabled for a file, it permits the file to be executed as the group that owns it, regardless of who is running it.
  • If configured for a directory, any files created in the directory will inherit the directory owner’s group ownership.

Use the index.php file to practice. Run the command chmod g+s index.php to grant this file the SGID permission.

If you run ls -l index.php now, you’ll see that the group’s permissions have an s.

new permissions for index.php new permissions for index.php

Go to my dir, which is a directory. The SGID permission may be added to this directory using the command chmod g+s my dir. The group ownership of any files generated in this directory will now be assigned to the directory owner. ls -ld my dir will show you the updated directory permissions (the d parameter limits output to directories only).

Checking the updated permissions for my dir Checking the updated permissions for my dir

Changing the Permissions for the Sticky Bit

The “sticky bit” refers to the last special authorization. Individual files are unaffected by this permission. However, file deletion is restricted at the directory level. Only the file owner has the ability to delete a file from a directory.

In the old familiar my dir directory, paste the sticky bit. Run the command chmod +t my dir to do this.

You don’t state the other group(o) before the +t to add the sticky bit.

When you look at the updated permissions for my dir, you’ll see a capital T in the others, as shown in the picture below.

new and special permissions in my dir new and special permissions in my dir

Conclusion

Congratulations! You went over a lot of ground with chmod recursive. You’re now ready to use the powerful chmod tool to solve any permission issues that occur. You even learnt how to use the recursive (-R) option to your advantage.

When you need to control file or directory permissions again, how do you plan to use chmod’s diverse features to your advantage?

Your Only Linux Shell Scripting Tutorial

The “chmod recursive 777” is a command-line tool that allows users to manage directory and file permissions with the recursive flag. The recursive flag will allow you to change the permissions of all files in a folder, recursively.

Related Tags

  • chmod recursive directories and files
  • chmod 777
  • chmod all files in directory recursively
  • chmod add write permission
  • chmod directory permissions

Table of Content