Manage Directory and File Permissions with Chmod Recursive (2024)

XFacebookLinkedIn

Are you tired of having to change file and directory permissions manually? Are you looking for a faster, more efficient way to modify your files and directories permissions? If so, then you’ve come to the right place because, in this article, you will learn how to change file and directory permissions using the command chmod recursively!

Not a reader? Watch this related video tutorial!

Not seeing the video? Make sure your ad blocker is disabled.

Read on to learn more!

Prerequisites

To follow along, you need a Linux device and a directory whose files and sub-directories permissions are available to practice with.

Learning About Linux Users and Groups

Owners and groups are important in Linux, necessary to securely manage files and directories. The owner is to whom a file or directory is assigned, which is the creator, by default. In Linux, files and directories also belong to groups. Both users and groups are set via the chown command.

In the screenshot below, the content highlighted in green shows the user who owns the file or directory, mihail in this example. The content highlighted in blue shows which group the file or directory belongs to, owned by the staff group in this example.

Manage Directory and File Permissions with Chmod Recursive (1)

Related:A Windows Guy in a Linux World: Users and File Permission

Understanding Linux File and Directory Permissions

Permissions set what actions a user or group may perform on a given file or directory. They are indicated by a character representation and also assigned a numerical value.

  • Read (r or 4): Indicates whether a user or group may read the contents of the file or directory. The read permission is granted by default to all newly created files and directories for all parties.
  • Write (w or 2): Indicates whether a user or group can edit the contents of an object. This permission is granted by default only to a file or directory owner.
  • Execute (x or 1): Indicates whether a user or group can execute a file or execute commands inside a directory. The execute permission is granted by default only to directories but not to files.

If you do not give the user (u) execute permissions to a directory, they will not be able to list contents as the user will not be able to execute commands within the directory!

An example is shown below, the output of the command ls -l demonstrates the different permission types. Those permissions preceded by the d value, indicate a directory.

Manage Directory and File Permissions with Chmod Recursive (2)

Why then are there seemingly three sets of permission for each object? Granular control of permissions is achieved by dividing permissions into the sections (referenced as parties for the purpose of this tutorial) listed below.

  • User (u): The owner of a file or directory, highlighted in blue.
  • Group (g): Members of the group to which a file or directory belongs, highlighted in green.
  • Other (o): All additional users and groups not explicitly assigned, highlighted in red.
Manage Directory and File Permissions with Chmod Recursive (3)

Changing File Permissions via the chmod recursive Command

With the proper Linux permission understanding, read on to learn how to change file permissions. Permissions are modified via the chmod recursive command as shown in the below examples.

1. First, check the current permissions with the ls -l command. Here, the my_dir/index.js file is shown.

Manage Directory and File Permissions with Chmod Recursive (4)

2. Run the chmod command, specifying the party, a (all), and the permissions, rwx, or read/write/execute. The full command follows: chmod a=rwx index.js.

Manage Directory and File Permissions with Chmod Recursive (5)

3. As you can see, running chmod recursive doesn’t return any output. To verify that permissions have changed run the command ls -l again.

Manage Directory and File Permissions with Chmod Recursive (6)

Setting File Permissions via Numeric Values

Remember the numbers associated with permissions such as read or write? Instead of specifying the character values, you may specify specific permissions via a number. By adding the values, you create a specific permission such as 6 which is a combination of write (2) and read (4) permissions.

In the example below, set permissions for the main.py file via the command chmod 664 main.py. The numerical values break down as follows.

  • User: Read and Write (6).
  • Group: Read and Write (6).
  • Other: Read (4).
Manage Directory and File Permissions with Chmod Recursive (7)

Once permissions are changed, verify the new permissions for the main.py file with the command ls -l.

Manage Directory and File Permissions with Chmod Recursive (8)

Removing and Adding File Permissions

The non-numerical commands you have run so far used the assignment parameter (=) with chmod recursive, to set explicit permissions. Instead, you may remove (- ) or add (+) permissions to existing permission sets.

1. First, list the existing permissions for the README.md file via the command ls -l README.md.

Manage Directory and File Permissions with Chmod Recursive (9)

2. Next, add (+) execute (x) permissions to the user party (u), while removing (-) read (r) permissions from the other party (o) for README.md, with the following command chmod u+x,o-r README.md. Differing party permissions are separated by commas (,).

Manage Directory and File Permissions with Chmod Recursive (10)

3. Run the ls -l README.md command to verify that permissions were modified.

Manage Directory and File Permissions with Chmod Recursive (11)

Recursively Changing File and Directory Permissions

Although you are making progress, you are only operating on one file at a time. What if you have a large number of files to change permissions for? Time to step up your game and learn to change permissions for multiple files.

One of the options to change multiple files is to run chmod recursive with the -R (recursive, and not the capital) option. The recursive option will change the permissions for all the files, including those under sub-directories, inside a given path.

1. Consider the following command, chmod -R a=r,u=rwx my_dir. Most of the options you have already seen before.

This command will change the permissions for all files in the directory, my_dir and sub-directories, via the recursive option (-R). The files are set to readable (r) for all (a), with differing permission for the current user (u) set to full permissions (read, write, execute).

In the screenshot below, you see the resulting permissions after executing the command, which has no resulting output.

Manage Directory and File Permissions with Chmod Recursive (12)

2. Suppose you run the command, chmod -R a=rwx my_dir, on the same directory as before, my_dir. Once again, you are changing all files in my_dir, and its subdirectories, to set give all (a) full permissions (read, write, execute). Here is the result of the command, as chmod recursive does not display output.

Manage Directory and File Permissions with Chmod Recursive (13)

3. How about the numeric method? As shown below, the recursive method also works with numeric permissions, chmod -R 770 my_dir. Here you are giving full permissions to the user and group, but no permissions to the other party.

Manage Directory and File Permissions with Chmod Recursive (14)

4. Run ls -l to check that my_dir files and sub-directories permissions are set with the numeric method.

Manage Directory and File Permissions with Chmod Recursive (15)

Defining File and Folder Behavior via Special Permissions

Special permissions allow for several additional privileges unique from the standard permission sets. There are three special permissions. Here is how these special permissions work and how you can add them to a file or directory.

Setting the SUID (User + S) Permissions

Commonly noted asSUID, it is a special permission for the user. The SUID has a single function: a file withSUIDalways executes as the user who owns the file, regardless of who is executing the file.

For example, consider index.js. To give the additional SUID permission, run chmod u+s index.js.

Now, if you run ls -l index.js, you’ll find that the user has an s in their permissions instead of an x.

Manage Directory and File Permissions with Chmod Recursive (16)

Setting the SGID (Group + S) Permissions

Commonly noted asSGID, this special permission has two functions:

  • If set for a file, it allows the file to be executed as thegroupthat owns the file, regardless of who is executing the file.
  • If set for a directory, any files created in the directory will have theirgroupownership set to that of the directory owner.

Practice on the index.php file. To add the SGID permission to this file, run the following command: chmod g+s index.php.

Now, if you run ls -l index.php, you’ll find that the group has an s in their permissions.

Manage Directory and File Permissions with Chmod Recursive (17)

Move on to a directory: my_dir. You can add the SGID permission to this directory using the following command: chmod g+s my_dir. Any files created in this directory will now have theirgroupownership set to the directory owner. You can check the directory new permissions using ls -ld my_dir (the d parameter limits output to directories only).

Manage Directory and File Permissions with Chmod Recursive (18)

Changing the Sticky Bit Permission

The last special permission is also known as the “sticky bit.” This permission does not affect individual files. But, at the directory level, it restricts file deletion. Only theowner of a file can remove a file within that directory.

Add the sticky bit to the old familiar my_dir directory. To do that, run the command as follows: chmod +t my_dir.

Note that, to add the sticky bit, you do not indicate the other group(o) before the +t.

When you check my_dir‘s new permissions, you will see a capital T in the other permissions, as shown in the screenshot below.

Manage Directory and File Permissions with Chmod Recursive (19)

Conclusion

Congratulations! You covered a lot of topics on chmod recursive. You are now ready to tackle any pesky permission problems that arise with the almighty chmod tool. You even learned how to leverage the power of the recursive (-R) option.

How do you intend to implement chmod versatile functionality to your advantage next time you’ll need to manage file or directory permissions?

Related:Your One and Only Linux Shell Scripting Tutorial

Manage Directory and File Permissions with Chmod Recursive (2024)

FAQs

Manage Directory and File Permissions with Chmod Recursive? ›

Changing permissions with chmod

How to chmod recursively for a directory? ›

The chmod command with the -R options allows you to change the file's permissions recursively. To recursively set permissions of files based on their type, use chmod in combination with the find command.

What does chmod 777 do to a directory? ›

chmod 777 foldername will give read, write, and execute permissions for everyone. chmod 700 foldername will give read, write, and execute permissions for the user only.

What is the difference between 755 and 644? ›

755 means you can do anything with the file or directory, and other users can read and execute it but not alter it. Suitable for programs and directories you want to make publicly available. 644 means you can read and write the file or directory and other users can only read it.

How do you chown a recursive directory? ›

To use chown recursively, open a terminal and type chown -R [user]:[group] [directory]. Replace [user] with the desired username, [group] with the target group, and [directory] with the directory's name. This command applies the ownership changes to the directory and all contained files and subdirectories.

How do I make a recursive directory? ›

Using the mkdir -p Command to Create Recursive Directories

mkdir -m a= rwx newdir; Here this command gives all the users the permission to read, write and execute the contents of the newly created directory, newdir. The list of permissions that can be set is similar to Linux or other Unix flavors.

What is the difference between chmod 755 and 777? ›

Some file permission examples: 777 - all can read/write/execute (full access). 755 - owner can read/write/execute, group/others can read/execute.

What would be the permissions of a directory with a mode of 755? ›

755 Permissions

The 755 permission in Linux means that the owner of the file can read, write, and execute it, while members of the group and other users can only read and execute it, but cannot modify or delete it.

How can I set file folder permissions to 755 or 644 in Windows? ›

Here's how I was able to do it:
  1. right-click on the directory, go to Properties.
  2. Security tab, Advanced..
  3. Permissions tab, Change Permissions...
  4. Add...
  5. Advanced...
  6. click Find Now, then find and click on "Everyone", click OK.
  7. click OK.
  8. "Everyone" should now show up in the list, with "Read & execute" permissions.
May 4, 2011

What is the difference between chmod 666 and 777? ›

A text file has 666 permissions, which grants read and write permission to everyone. A directory and an executable file have 777 permissions, which grants read, write, and execute permission to everyone.

How do you rm a directory recursively? ›

To remove a directory and all its contents, including any subdirectories and files, use the rm command with the recursive option, -r . Directories that are removed with the rmdir command cannot be recovered, nor can directories and their contents removed with the rm -r command.

What is the command for recursive directory? ›

The dir command displays a list of files and subdirectories in a directory. With the /S option, it recurses subdirectories and lists their contents as well.

How do I get all files in a recursive directory? ›

List all Files from a Directory Recursively
  1. Create a File object representing the root directory.
  2. Utilize the listFiles() method on the File object.
  3. Implement recursion: For each directory in the retrieved array, call the listFiles() method again recursively, passing the directory as the new root.
Mar 3, 2024

How do I chmod a directory? ›

The chmod command allows users to modify access levels, and for directories like /var/www, setting permissions to 777 using “sudo chmod -R 777 /var/www” grants read and write access to all subfolders.

How do I recursively find a directory in Linux? ›

How to get a recursive directory listing in Linux or Unix
  1. ls -R : Use the ls command to get recursive directory listing on Linux.
  2. find /dir/ -print : Run the find command to see recursive directory listing in Linux.
  3. du -a . : Execute the du command to view recursive directory listing on Unix.
Oct 1, 2022

What is chmod 755? ›

$ chmod 755 file.txt. Will give: Read, write, and execute permissions to the owner of the file; which is the user account that created the file or has been assigned ownership of the file.

Top Articles
Latest Posts
Article information

Author: Otha Schamberger

Last Updated:

Views: 5495

Rating: 4.4 / 5 (55 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Otha Schamberger

Birthday: 1999-08-15

Address: Suite 490 606 Hammes Ferry, Carterhaven, IL 62290

Phone: +8557035444877

Job: Forward IT Agent

Hobby: Fishing, Flying, Jewelry making, Digital arts, Sand art, Parkour, tabletop games

Introduction: My name is Otha Schamberger, I am a vast, good, healthy, cheerful, energetic, gorgeous, magnificent person who loves writing and wants to share my knowledge and understanding with you.