In Linux, file permissions are an important aspect of ensuring system security. The chmod command is a powerful utility that allows system administrators to easily change file and directory permissions. In this article, you’ll learn how to use the chmod command to recursively change the permissions of directories and files.
Key Takeaways
- Learning about read, write & execute permission in Linux.
- Learning to change file and directory permissions recursively using the chmod.
- Knowing about frequently asked questions and their answers regarding file permission.
Requirements
- You must have root/sudo access to Ubuntu.
- You need to determine the desired permission for all the files.
Process Flow Chart
Distro Used Throughout the Tutorial: Ubuntu 22.04.1 LTS
Read, Write & Execute Permissions in Linux
File and directory permissions are used in Linux to control resource access. Read, write, and execute permissions, which are denoted by the letters “r,” “w,” and “x,” respectively, are three basic forms of permissions. These permissions can be assigned to 3 types of users: the file or directory’s owner, the group that owns the file or directory, and others.
- Read permission (r): Allows a user to view the contents of a file or lists of a directory, but cannot modify them.
- Write permission (w): Allows a user to modify the contents of a file or to create, delete, or rename files in a directory. For example, a user with only written permission on a file can modify its contents but can’t view or execute it.
- Execute permission (x): Allows a user to run an executable file or change into a directory, but can not view or write onto it.
Read, write, and execute permissions can be represented using either octal or binary numbers. Here is a table below showing octal representation, binary representation, file permission set, and corresponding permissions of any file/directory.
Octal Representation | Binary Representation | File Permission Set | Permission | ||
---|---|---|---|---|---|
0 | 000 | — | No permission | ||
1 | 001 | –x | Execute | ||
2 | 010 | -w- | Write | ||
3 | 011 | -wx | Write | Execute | |
4 | 100 | r– | Read | ||
5 | 101 | r-x | Read | Execute | |
6 | 110 | rw- | Read | Write | |
7 | 111 | rwx | Read | Write | Execute |
Watch How to Change Permissions of Directories and Files Recursively with “chmod”
Steps to Change Permissions of Directories and Files Recursively with “chmod”
In Linux, you can easily change the permissions of directories and files’ permission recursively using the chmod command. To change the Linux folders and files’ permissions recursively, you can use the chmod command with the –recursive or -R option. Here, I am going to recursively change the permissions of the subdirectories Flowers, Fruits & Vegetables of the Summer directory and files Daisy.html, Orchid.html, Jackfruit.txt, Mango.txt, Cucumber.txt & Tomato.txt inside the subdirectories of the Summer directory. To do this follow the steps below.
Steps to Follow >
➊ At first, open the Ubuntu terminal.
➋ Copy the following command in the command prompt to view the current permissions of the subfolders and files inside the subfolders of the Summer folder:
ls -lR Summer
- ls: Shows all the files in a specific folder.
- option -l: Long listing format.
- option -R: Enables Recursive mode.
- Summer: The folder to change permission.
➌ Hit the ENTER button.In the image above, you can see the current permissions of the subfolders and the files inside the subfolders of the Summer folder.
➍ Now, run the following command to change permissions recursively:
chmod -R 777 Summer
- chmod: Changes file permissions.
- -R option: Enables recursive mode.
- 777: Read, Write & Execute Permission.
- Summer: The folder to change permission.
ls -lR Summer
- ls: Shows all the files in a specific folder.
- option -l: Long listing format.
- option -R: Enables Recursive mode.
- Summer: The folder to change permission.
Complementary Information
You will find the following information helpful along with learning how to change directories and files recursively with the chmod command.
How to Change Permissions of Files Only, not Directory in Linux
You can change the permissions of only files without changing directory permissions using the chmod command with the find command Here, I am going to change the permissions of the files inside the Summer directory without changing directory permissions. To do the same follow the following process.
Steps to Follow >
➊ Initially, open the Ubuntu terminal.
➋ Copy the following command in the command prompt to view the current permissions of the Summer folder and its files:
ls -ld Summer
ls -l Summer
- ls: Shows all the files in a specific folder.
- option -l: Long listing format.
- option -d: Shows information about the given directory.
- Summer: The folder to change permission.
➌ Hit the ENTER button.As you can see in the image above, the terminal is displaying the current permissions of the Summer folder and the files inside it.
➍ Now, run the following command to change permissions:
find ~/Summer -type f -exec chmod 644 {} \;
- find: Used to perform search operations.
- ~/Summer: Absolute path of the directory.
- –type f: Specifies only regular files.
- -exec: Used to execute the chmod command on each file found by the find command.
- chmod: Changes file permissions.
- 644: Read, Write & Execute permission.
- {}: Placeholder for the current pathname.
- \: Used before (;) to treat it like an argument rather than a separator.
- ;: Indicates the end of the command.
ls -ld Summer
ls -l Summer
- ls: Shows all the files in a specific folder.
- option -l: Long listing format.
- option -d: Shows information about the given directory.
- Summer: The folder where all the files are.
How to Change Permissions of Directory Only, not Files in Linux
You can change the permissions of the directory only using the chmod command with the find command Here, I am going to change the permissions of the Summer directory but not the files’ permission. Now, to do the same follow the instructions below.
Steps to Follow >
➊ Start by opening the Ubuntu terminal.
➋ Type the following command in the command prompt to view the current permissions of the Summer folder and its files:
ls -ld Summer
ls -l Summer
- ls: Shows all the files in a specific folder.
- option -l: Long listing format.
- option -d: Shows information about the given directory.
- Summer: The folder to change permission.
➌ Strike the ENTER button.As you can see in the image above, the terminal is displaying the current permissions of the Summer folder and the files inside it.
➍ After that, execute the following command to change permissions:
find ~/Summer -type d -exec chmod 776 {} \;
- find: Used to perform search operations.
- ~/Summer: Absolute path of the directory.
- –type f: Specifies only regular files.
- -exec: Used to execute the chmod command on each file found by the find command.
- chmod: Changes file permissions.
- 644: Read, Write & Execute permission.
- {}: Placeholder for the current pathname.
- \: Used before (;) to treat it like an argument rather than a separator.
- ;: Indicates the end of the command.
ls -ld Summer
ls -l Summer
- ls: Shows all the files in a specific folder.
- option -l: Long listing format.
- option -d: Shows information about the given directory.
- Summer: The folder to change permission.
- How to Give Permission to User in Linux? [4 Methods]
- 2 Ways to Change Folder Permissions from Root to User in Linux
How to Change Ownership of a File in Linux
You can change a file’s ownership using the chown command in Ubuntu. You can change the ownership to a specific user, specific group, or both the user and the group. I have shown you how you can change the ownership of a file to these user types below.
Change the Ownership of a File to a Specific User in Linux
You can change the ownership of a file to a specific user in Ubuntu. Here I am changing ownership of myfile.txt from user sylvie to user rynvie. Follow the process below to do that.
Steps to Follow >
➊ Open the Ubuntu Terminal.
➋ To view the current owner/user of the file myfile.txt, run the following command in the command prompt:
ls -l
- ls: Shows all the files in a specific folder.
- option -l: Long listing format.
➌ To change the ownership of the file to a specific user, execute the following command in the command prompt:
sudo chown rynvie myfile.txt
- sudo: Grants administrative privileges.
- chown: Changes the file’s ownership.
- rynvie: name of the user.
- myfile.txt: The file to change ownership.
❹ Then, type the password and press the ENTER button.❺ Finally, execute the following command to check if the ownership of the myfile.txt file is changed:
ls -l
- ls: Shows all the files in a specific folder.
- option -l: Long listing format.
Change the Ownership of a File to a Specific Group in Linux
You can change the ownership of a file to a specific group in Ubuntu. Here I am changing ownership of myfile.txt from group sylvie to group rynvie. Follow the procedure below to do that.
Steps to Follow >
➊ Open the Terminal in Ubuntu.
➋ To view the current group of the file myfile.txt, run the following command in the command prompt:
ls -l
- ls: Shows all the files in a specific folder.
- option -l: Long listing format.
➌ To change the ownership of the file to a specific group, execute the following command in the command prompt:
sudo chown :rynvie myfile.txt
- sudo: Grants administrative privileges.
- chown: Changes the file’s ownership.
- rynvie: name of the group.
- myfile.txt: The file to change ownership.
❹ Then, type the password and press the ENTER button.❺ Finally, execute the following command to check if the group of the myfile.txt file is changed:
ls -l
- ls: Shows all the files in a specific folder.
- option -l: Long listing format.
- How to Change File Permissions to 777 in Ubuntu?
- 2 Ways to Change Folder Permissions Recursively in Linux
Change Both the User and Group Ownership of a File in Ubuntu
You can change both the user and group ownership of a file in Ubuntu. Here I am changing both the user and group ownership of myfile.txt from sylvie to rynvie. Follow the instructions below to do that.
Steps to Follow >
➊ Initially open the Ubuntu Terminal.
➋ To view the current owner/user and group of the file myfile.txt, run the following command in the command prompt:
ls -l
- ls: Shows all the files in a specific folder.
- option -l: Long listing format.
➌ To change both the user and group ownership of a file, execute the following command in the command prompt:
sudo chown rynvie:rynvie myfile.txt
- sudo: Grants administrative privileges.
- chown: Changes the file’s ownership.
- rynvie: name of the user and group.
- myfile.txt: The file to change ownership.
❹ Then, type the password and press the ENTER button.❺ Finally, execute the following command to check if the ownership of both the user and group of the myfile.txt file are changed:
ls -l
- ls: Shows all the files in a specific folder.
- option -l: Long listing format.
Conclusion
The chmod command is a powerful tool that allows system administrators to change file and directory permissions in Linux. You can simply change the permissions of entire folders and their contents by using the -R option, which allows you to change permissions recursively. While changing permissions, keep in mind to proceed with caution to avoid unwanted consequences.
People Also Ask
Related Articles
- How to Change Permissions of All Files in a Folder in Linux?
- Manage File Permissions in Ubuntu Using Command?
- How to Change File Permissions in Linux with 6 Examples
- 2 Cases to Give User Permission to Folder and Subfolders in Ubuntu
- How to Change Folder Permissions in Linux? [2 Methods]
- Change Permissions on Mounted Drive in Linux