Granting Full User Rights to a Folder and Its Contents in Linux

Sometimes it is necessary to grant full user rights on an assortment of files in your Linux system. You may have dozens, hundreds, or even thousands of files on which a user needs read, write, and execute permissions in order to fully access and utilize them. In such scenarios, it can be useful to grant full user rights on an entire directory, which gives blanket permissions on all of the directory’s file contents and subdirectories.

In this tutorial, you will see how to grant full user rights to a folder and its contents on a Linux system. The process involves granting read, write, and execute permissions on all files within the directory, and optionally taking ownership of all files as well. We will go over a few different examples to cover various scenarios in case you would like to apply more specific or granular permissions to a group of files.

In this tutorial you will learn:

  • How to change user ownership of a directory and its contents
  • How to change group ownership of a directory and its contents
  • How to grant read, write, and execute permissions for a directory and its contents
Granting Full User Rights to a Folder and Its Contents in Linux
Granting Full User Rights to a Folder and Its Contents in Linux
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software chown, chmod, chgrp
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

How to Grant Full User Rights to a Folder and Its Contents



DID YOU KNOW?
It is important to keep in mind that file permissions exist for a reason. If you are encountering a permission denied error, your first solution should not be to grant full user rights on tons of files. Remember to only use the minimum file permissions necessary in order to accomplish your task. This helps ensure file security across your operating system.

When it comes to granting full user righs on a directory, we have a few different options. You should select your option carefully from the list below, keeping in mind that you only need to grant the minimum permissions necessary. For example, there may be no need to grant execute permissions on every single file in a directory, unless all of the files are indeed executable scripts.

If you only need to access the files, then read permissions will be sufficient. If you need to edit the files, then write permissions are sufficient. Use blanket permission statements sparingly, and only grant the permissions that are completely necessary.

Full User Rights Command Line Options

We will use the chown command to change file ownership, the chmod command to change individual file and directory permissions (read, write, execute), and the chgrp command to change group permissions. Depending on your scenario, you may only need one or two of the commands. Let’s dive into the different options below:

  1. Taking ownership of files is usually enough to grant all the permissions you will need. This can be accomplished with the chown Linux command. We will also use the -R option to indicate that we want to make these changes recursively. In other words, the ownership change will take place on the directory we specify, as well as all of its contents. In this example, we change ownership on the directory and its contents for the current user $USER executing the command:
    $ sudo chown -R $USER /path/to/directory
    

    Note that this command does not change the group permissions. It only takes ownership of the file, while leaving the group ownership intact. We can see how this works in the screenshot below:

    Granting ownership over files without affecting group ownership
    Granting ownership over files without affecting group ownership




    As you can see in the screenshot above, the linuxconfig user (our currently logged in user, which we used $USER to indicate) is given complete ownership of all files. However, the group ownership stays the same as before. This method should normally grant you all of the expected permissions you will need on a directory full of files.

  2. If taking ownership of the files does not yield the results you would like, or if you would just like to edit permissions instead of ownership, we can use the chmod tool to do so. We will once again use the -R option to specify that we would like these permission changes to be recursive (in other words, on all files and subdirectories within the folder). The following command will grant full permissions (read, write, and execute) to the owner of the files (if you also used option 1, then this would be your user account), while giving read permissions to the group owner, and read permissions to other users:
    $ chmod -R 744 /path/to/directory 
    

    The permissions in the screenshot below show that the owner of the files (linuxconfig) now has read, write, and execute permissions, while the group and other users only have read permissions:

    Checking that the owner of the files now has full permissions
    Checking that the owner of the files now has full permissions
  3. The third option we have is change the permissions for the group owner. Group permissions are not used as often in Linux, but they come in handy if you have a roster of multiple, but select users that all need full permissions to a directory and its contents. As an example, we can use the chgrp command to change the group owner of the files to a group named admins:
    $ sudo chgrp -R admins /path/to/directory
    
    We have successfully changed the group owner to the admins group for all files
    We have successfully changed the group owner to the admins group for all files

    Now that we have proper group ownership, we can grant full permissions (read, write, and execute) to the owner and group owner of the files with the chmod command, while giving all other users just read permissions:

    $ chmod -R 774 /path/to/directory 
    

That’s all there is to it. You should now have full user rights for the directory in question, as well as all of its contents. Depending on your scenario, you may find that one option is more suitable than another, or you may choose to combine two of the options, or even all three. Linux allows us a lot of flexibility when dealing with file permissions, giving us the options for user ownership, group ownership, owner permissions, group permissions, and other user permissions. This way, you can feel free to implement the setup which works best for you while also ensuring that users are only granted the minimum permissions (for security purposes) needed in order to complete their tasks.

Closing Thoughts




In this tutorial, we saw how to grant full user rights to a folder and all of its contents on a Linux system. Linux gives us at least three different ways to accomplish this task, and the option or options you choose will just depend on what kind of scenario you find yourself in. The simplest thing to do is usually just change ownership of the files, but this may not always suffice if you have multiple users that need to access them, in which case changing group ownership is more appropriate. We also learned how to change the read, write, and execute permissions for all files.



Comments and Discussions
Linux Forum