How to Change Permissions of Folder and Subfolders/File in Linux

1. Change Permissions of a Single File

To change the permissions of a single file, use the chmod command followed by the numerical value of the permissions and the file name.

For example, to give read, write, and execute permissions to the owner of a file named “myfile”, use the command:

chmod 755 myfile

2. Change Permissions of a Folder

To change the permissions of a folder, use the chmod command followed by the numerical value of the permissions and the folder name.

For example, to give read, write, and execute permissions to the owner of a folder named “myfolder”, use the command:

chmod 755 myfolder

3. Change Permissions of All Files and Subfolders in a Folder

To change the permissions of all files and subfolders in a folder, use the chmod command followed by the -R (recursive) option, the numerical value of the permissions, and the folder name.

For example, to give read, write, and execute permissions to the owner of all files and subfolders in a folder named “myfolder”, use the command:

chmod -R 755 myfolder

To change the permissions of a folder and its subfolders or files. Permissions define who can access or modify files and directories in Linux. In this tutorial, you will learn process of changing the permissions of a folder and its subfolders or files in Linux.

How to Change Permissions of Folder and Subfolders/File in Linux

Follow these simple steps to secure your folder and Subfolders files and keep them safe from unwanted access.

  • Step 1: Check current permissions
  • Step 2: Change permissions of a single file or folder
  • Step 3: Change permissions of a folder and its subfolders or files

Step 1: Check current permissions

Before you change permissions, it is always a good idea to check the current permissions of the folder or file. You can do this by using the ‘ls -l‘ command, which will display a list of files and directories along with their permissions.

Step 2: Change permissions of a single file or folder

To change the permissions of a single file or folder, you can use the ‘chmod’ command. The ‘chmod’ command allows you to modify the read, write, and execute permissions for the owner, group, and others.

For example, if you want to give the owner of a file read and write permissions, you can use the following command:

chmod u+rw filename

Here, ‘u’ stands for the owner of the file, ‘+’ indicates that we are adding permissions, and ‘rw’ indicates read and write permissions.

Similarly, you can give execute permissions to the group of a file by using the following command:

chmod g+x filename

Here, ‘g’ stands for the group of the file, ‘+’ indicates that you are adding permissions, and ‘x’ indicates execute permissions.

Step 3: Change permissions of a folder and its subfolders or files

To change the permissions of a folder and its subfolders or files, you can use the ‘-R’ option with the ‘chmod’ command. The ‘-R’ option stands for recursive, which means that the permissions will be applied to all files and subdirectories within the specified directory.

For example, if you want to give the owner of a directory and its contents read, write, and execute permissions, you can use the following command:

chmod -R u+rwx directory

Here, ‘-R’ stands for recursive, ‘u’ stands for the owner of the directory, ‘+’ indicates that we are adding permissions, and ‘rwx’ indicates read, write, and execute permissions.

Similarly, you can give read and execute permissions to others for a directory and its contents by using the following command:

chmod -R o+rx directory

Here, ‘o’ stands for others, ‘+’ indicates that we are adding permissions, and ‘rx’ indicates read and execute permissions.

Here’s a list of some common permissions commands in Linux along with their options:

  1. chmod: This command is used to change the permissions of a file or directory. Some of its options are:
    • u: Changes the permissions for the user who owns the file.
    • g: Changes the permissions for the group that the file belongs to.
    • o: Changes the permissions for others (i.e., users who are not the owner or in the group).
    • a: Changes the permissions for all users.
    • +: Adds a permission to the file.
    • -: Removes a permission from the file.
    • =: Sets the permissions to the specified values.
  2. chown: This command is used to change the owner of a file or directory. Some of its options are:
    • -R: Changes the owner recursively for all files and directories in a directory.
    • –from: Changes the owner from the specified user to a new user.
    • –reference: Changes the owner to match the owner of a specified file or directory.
  3. chgrp: This command is used to change the group of a file or directory. Some of its options are:
    • -R: Changes the group recursively for all files and directories in a directory.
    • –reference: Changes the group to match the group of a specified file or directory.
  4. umask: This command is used to set the default permissions for new files and directories. Some of its options are:
    • -S: Shows the current umask value in symbolic notation.
    • -p: Shows the current umask value in octal notation.
    • -o: Sets the umask value for the owner of the file.
    • -g: Sets the umask value for the group of the file.
    • -u: Sets the umask value for others.
  5. su: This command is used to switch to a different user account. Some of its options are:
    • -: Switches to the root user account.
    • -c: Runs a command as the specified user account.
    • -l: Starts a new login shell as the specified user account.

Here are some FAQs for changing or giving read, write, and access permissions of folders and subfolders/files in Linux:

  1. What are file and folder permissions in Linux?
    • File and folder permissions in Linux determine who can access and modify a file or folder. Permissions are typically divided into three categories: owner, group, and other. Each category can be assigned different levels of access, including read, write, and execute.
  2. How do I check the permissions of a file or folder in Linux?
    • You can check the permissions of a file or folder by using the “ls -l” command in the terminal. This will display a detailed list of information about the file or folder, including its permissions.
  3. How do I change the permissions of a file or folder in Linux?
    • You can change the permissions of a file or folder by using the “chmod” command in the terminal. For example, “chmod u+x myfile.txt” will give the owner of the file execute permission.
  4. How do I change permissions for a directory and all of its subdirectories and files?
    • You can use the “chmod” command with the “-R” option to change the permissions recursively. For example, “chmod -R u+rwx mydirectory” will give the owner of the directory and all of its subdirectories and files read, write, and execute permissions.
  5. How do I change the owner of a file or folder in Linux?
    • You can change the owner of a file or folder by using the “chown” command in the terminal. For example, “chown user myfile.txt” will change the owner of the file to “user”.
  6. How do I change the group of a file or folder in Linux?
    • You can change the group of a file or folder by using the “chgrp” command in the terminal. For example, “chgrp group myfile.txt” will change the group of the file to “group”.
  7. What are the risks of changing file and folder permissions in Linux?
    • Changing file and folder permissions can potentially compromise the security of your system. It’s important to understand the risks and only make changes when necessary. Always make sure to set appropriate permissions for sensitive files and folders to prevent unauthorized access.
  8. How do I set default permissions for new files and folders in Linux?
    • You can set default permissions for new files and folders by using the “umask” command in the terminal. The umask value specifies which permissions are removed from the default permissions. For example, “umask 022” will set the default permissions for new files and folders to 644 and 755 respectively.
  9. What is the difference between “chmod” and “chown” in Linux?
    • “chmod” is used to change the permissions of a file or folder, while “chown” is used to change the owner of a file or folder. Both commands can be used together to change both the permissions and owner of a file or folder.
  10. How do I revoke permissions for a specific user or group in Linux?
    • You can use the “setfacl” command in the terminal to revoke permissions for a specific user or group. For example, “setfacl -m u:user:— myfile.txt” will remove all permissions for the “user” user on the “myfile.txt” file.
  11. Can I change permissions for multiple files or folders at once in Linux?
    • Yes, you can use the “chmod” command with a wildcard character to change permissions for multiple files or folders at once. For example, “chmod u+x *.txt” will give execute permission to the owner for all files in the current directory with the “.txt” extension.
  12. How do I change permissions for a symbolic link in Linux?
    • You can use the “chmod” command with the “-h” option to change permissions for a symbolic link. For example, “chmod -h u+rwx mysymlink” will give the owner of the symbolic link read, write, and execute permissions.
  13. Can I restore default permissions for a file or folder in Linux?
    • Yes, you can use the “chmod” command with the numeric value of the default permissions to restore them. For example, “chmod 644 myfile.txt” will restore the default permissions of the “myfile.txt” file.
  14. Can I change Linux permissions in numeric code
    • Yes, You may need to know how to change permissions in numeric code in Linux, so to do this you use numbers instead of “r”, “w”, or “x”.
      • 0 = No Permission
      • 1 = Execute
      • 2 = Write
      • 4 = Read
    • Basically, you add up the numbers depending on the level of permission you want to give.
    • Permission numbers are:
      • 0 = —
      • 1 = –x
      • 2 = -w-
      • 3 = -wx
      • 4 = r-
      • 5 = r-x
      • 6 = rw-
      • 7 = rwx
    • For example:
      • 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.
      • chmod 327 foldername will give write and execute (3) permission for the user, w (2) for the group, and read, write, and execute for the users.

Conclusion

Changing permissions of a folder and its subfolders or files in Linux is a straightforward process. By following the steps outlined above, you can modify permissions as per your requirements. Remember to always check the current permissions before making any changes and use the ‘-R’ option to apply permissions recursively to all files and subdirectories within a directory.

Recommended Tutorials

Jaspreet Singh Ghuman

Jaspreet Singh Ghuman

Jassweb.com/

Passionate Professional Blogger, Freelancer, WordPress Enthusiast, Digital Marketer, Web Developer, Server Operator, Networking Expert. Empowering online presence with diverse skills.

jassweb logo

Jassweb always keeps its services up-to-date with the latest trends in the market, providing its customers all over the world with high-end and easily extensible internet, intranet, and extranet products.

Contact
San Vito Al Tagliamento 33078
Pordenone Italy
Item added to cart.
0 items - 0.00
Open chat
Scan the code
Hello 👋
Can we help you?