The Short Answer
On Linux and Unix-like operating systems (MacOS included), the chmod command is used to change the permissions of files and directories. The x option specifically sets the execute permission on a file, allowing it to be run as a program.
For example, to make a script executable by every user on the system, you can use the following command:
$ chmod +x script.sh
Run in Warp
Then run the script directly:
$ ./script.sh
Run in Warp
Note that the + sign here translates to “add permission”.
The Execute Permission For Directories
In the case of directories, the execute permission has a slightly different meaning than for files, as when set, it allows users to list their content using the ls command, or enter them using the cd command.
Setting The Execute Permission For Specific Users
In some cases, you might want to control which user is allowed to execute a file.
To do so, you can use a combination of the letters:
- a for all; which designates all users on the system.
- u for user; which is the user account that created the file or has been assigned ownership of the file
- g for group; which is a collection of user accounts, such as members of the same team, that have been granted certain permissions on the file.
- o for others; which are all other users who are not the owner or members of the group associated with the file.
Use u+x to give execute permission to the owner of the file only
Use ug+x to give execute permission to the owner of the file and the group
Use a+x to give execute permission to everyone
Note that in this last case, executing chmod a+x has the same effect as chmod +x.
Setting The Execute Permission Conditionally
The X (uppercase x) option allows to conditionally set the execute permission of files and directories.
When used:
- If the target is a directory, then it sets the execute permission for the owner, the group, and the others, allowing them to list and enter the directory.
- If the target is a regular file, then it doesn’t set the execute permission, unless it is already enabled on at least one of the owner, the group, or the others.
This option is particularly useful when you want to recursively change the permission of a directory and its subdirectories, without affecting the regular files themselves.
For example, let’s consider the following tmp directory:
Running the chmod -R g+X command on this directory will set the execute permission for the group on:
- The tmp directory.
- The dir_1 subdirectory.
- The script_1.sh file.
But will leave the permissions of the script.sh file unchanged.
Note that running chmod +X will have the same effect as running chmod a+X.
Using the chmod command with sudo
The sudo command is used to execute a command as the superuser (or root).
Executing the chmod command with sudo allows you to modify the permissions of files or directories that you do not have access to as the current user.
For example, you can use the sudo chmod +x command on a system file to give permission to all users to execute it.
$ sudo chmod +x /sbin/reboot
Run in Warp
It is important to note that giving the execute permission to all users to a file or directory belonging to the system may lead to security vulnerabilities and have unintended consequences.
Written by
Razvan Ludosanu
Founder, learnbackend.dev
Filed Under
Related Articles
List Open Ports in Linux
Learn how to output the list of open TCP and UDP ports in Linux, as well as their IP addresses and ports using the netstat command.
Count Files in Linux
Learn how to count files and folders contained in directories and subdirectories in Linux using the ls, find, and wc commands.
How to Check the Size of Folders in Linux
Learn how to output the size of directories and subdirectories in a human-readable format in Linux and macOS using the du command.
Linux Chmod Command
Understand how to use chmod to change the permissions of files and directories. See examples with various chmod options.
POST JSON Data With Curl
How to send valid HTTP POST requests with JSON data payloads using the curl command and how to avoid common syntax pitfalls. Also, how to solve the HTTP 405 error code.
Format Command Output In Linux
Learn how to filter and format the content of files and the output of commands in Linux using the awk command.
Create Groups In Linux
Learn how to manually and automatically create and list groups in Linux.
Switch Users In Linux
Learn how to switch between users, log in as another user, and execute commands as another user in Linux.
Remover Users in Linux
Learn how to remove local and remote user accounts and associated groups and files in Linux using the userdel and deluser commands.
Delete Files In Linux
Learn how to selectively delete files in Linux based on patterns and properties using the rm command.
Find Files In Linux
Learn how to find and filter files in Linux by owner, size, date, type and content using the find command.
Copy Files In Linux
Learn how to safely and recursively copy one or more files locally and remotely in Linux using the cp and scp command.