close
The Wayback Machine - https://web.archive.org/web/20240927080655/https://www.geeksforgeeks.org/chmod-command-linux/
Open In App

How to Make Script Executable in Linux | chmod Command

Last Updated : 10 Jan, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

In Unix operating systems, the chmod command is used to change the access mode of a file. The name is an abbreviation of change mode. Which states that every file and directory has a set of permissions that control the permissions like who can read, write or execute the file. In this the permissions have three categories: read, write, and execute simultaneously represented by `r`, `w` and `x`. These letters combine together to form a specific permission for a group of users.

The `chmod` command is used to modify this permission so that it can grant or restrict access to directories and files. Let’s have a look at the syntax and options for the `chmod` command in Linux Operating System.

Syntax of chmod command

chmod [options] [mode] [File_name] 

Here,

  • Options: Optional flags that modify the behavior of the chmod command.
  • Mode: The permissions to be set, represented by a three-digit octal number or symbolic notation (e.g., u=rw,go=rx).
  • File_name: The name of the file or directory for which the permissions are to be changed.

Options Available in chmod Command Linux

Options Description
`-R` Apply the permission change recursively to all the files and directories within the specified directory.
`-v` It will display a message for each file that is processed. while indicating the permission change that was made.
`-c` It works same as `-v` but in this case it only displays messages for files whose permission is changed.
`-f` It helps in avoiding display of error messages.
`-h` Change the permissions of symbolic links instead of the files they point to.

Note: Options in `chmod` are basically used for making changes in bulk and modifying permissions across multiple files or directories at once.

Modes in chmod Command in Linux

The “mode” helps in setting new permissions that have to be applied to files or directories.

This mode can be specified in several ways, we will discuss two modes: Symbolic and Octal mode. 

1) Symbolic mode

If we talk about symbolic mode, we can say that it is the most common method used for specifying fir permissions. In this we have to make a combination of letters and operators to set or tell what to do with permissions.

The following operators can be used with the symbolic mode:

Operators Definition
`+` Add permissions
`-` Remove permissions
`=` Set the permissions to the specified values

The following letters that can be used in symbolic mode:

Letters Definition
`r` Read permission
`w` Write permission
`x` Execute permission

The following Reference that are used:

Reference Class
u Owner
g Group
o Others
a All (owner,groups,others)

Examples of Using the Symbolic mode:

  • Read, write and execute permissions to the file owner:
chmod u+rwx [file_name]


  • Remove write permission for the group and others:
chmod go-w [file_name]


  • Read and write for Owner, and Read-only for the group and other:
chmod u+rw,go+r [file_name]


2) Octal mode

It is also a method for specifying permissions. In this method we specify permission using three-digit number. Where..

  •  First digit specify the permission for Owner.
  •  Second digit specify the permission for Group. 
  • Third digit specify the permission for Others. The digits 

NOTE: The digits are calculated by adding the values of the individual permissions.

Value Permission
4 Read Permission
2 Write Permission
1 Execute Permission

Examples of Using the Octal mode:

Suppose if we to give read and write permission to the file Owner. Read, write and executable permission to the Group. Read-only permission to the Other. They our command would be.

  chmod 674 [file_name]


Here.

  • 6 represent permission of file Owner which are (rw).
  • 7 represent permission of Group which are (rwx).
  • 4 represent permission of Other which is (r).

Practical Implementaion of How to Make script executable in Linux

Understanding Script Execution in Linux

In Linux, scripts are typically written in languages like Bash, Python, or Perl. While the script’s content is crucial, ensuring it has executable permissions is equally important. Without execution permissions, Linux won’t allow the script to run.

Step 1: Navigate to the Script’s Directory

Open the terminal and use the cd command to navigate to the directory where your script is located.

For example:

cd /path/to/your/script

Step 2: Check Current Permissions

Use the `ls` command with the `-l` option to list the files in the directory along with their permissions. This step helps you identify the current permissions of your script:

ls -l

checking current permission

checking current permission

Step 3: Make the Script Executable

To make a script executable, you need to use the `chmod` command. Assuming your script is named `example.sh`, you can make it executable with the following command:

chmod +x example.sh

This command adds the execute (+x) permission to the script.

Step 4: Verify Changes

Run the `ls -l` command again to verify that the script now has execute permissions. You should see an ‘x’ in the permission field for the script:

ls -l

verifying changes after making script executable

verifying changes after making script executable

Step 5: Execute the Script

Now that your script has executable permissions, you can run it using the `./` notation followed by the script’s name. For example:

./example.sh

execute the script

execute the script

This notation tells Linux to look in the current directory (.) for the script named example.sh.

Frequently Asked Questions on chmod – FAQs

How do I view the current permissions of a file or directory?

ls -l


ls -l

all the permissions that a current directory have

To see all the permissions that a particular directory or file has.

ls -l example


listed all permissions this file has.

listed all permissions this file has.

Here example is a file_name.

What are the different types of permissions in Linux, and what do they mean?

There are three types of permissions in Linux: 

read (`r`)

write (`w`)

execute (`x`)

They are applied to:

owner (`u`)

group (`g`)

other (`o`)

What is “chmod 777 “, “chmod 755” and “chmod +x “or “chmod a+x”?

chmod 777 [file_name]


This command gives all three permissions to everyone (owner, group and other)

chmod a+x [file_name]


It makes a file executable for everyone. It is the most used command after we install an executable file, we still need to add a permission to actually make it an executable file.

chmod 755 [file_name]


In this case the owner can write, read and execute a file, group and other can only read and execute a file.

How can we revert changes made by “chmod” command in Linux?

To undo or revert changes made by “chmod” command in Linux , we can use the `chmod` command again but this time we should mention the correct permission we want. 

 Here are the steps to undo or revert changes:

  • Determine the correct permission you want and use `chmod` command again. 
    For Example: If we want to revert the changes to “rw-r–r–” (read and write permission for owner, read-only permission for group and others), according to this our octal value would be “644” (read = 4, write=2).
  • Now open the directory and write the given command :
chmod 644 [file_or_directory_name]


Here instead of “[file_or_directory_name]” use your file or directory name.

Suppose our file name is “a.txt”

    Before reverting or undo changes:

    ls -l a.txt

    ls -l a.txt   (used to display all the permission a.txt has)

    After reverting or undo changes:

    reverting of chmod changes in Linux

    Here we can see that changes has be done

    Conclusion

    The `chmod` command in Linux is used to modify the permissions and access mode of files and directories. These are the permissions that control who can read, write and execute the file. We have discussed two types of modes for specifying permission: symbolic and octal mode. In symbolic mode, one uses letters and operators to specify the permission. Whereas octal has a three-digit number for specifying the permission. The `chmod` command also provides some options for bulk modifications, for example: `-R` for recursive and `-v` and `-c` for displaying message. The overall conclusion is that `chmod` command in Linux is a very essential tool for managing file and directories permissions.

    ?list=PLqM7alHXFySFc4KtwEZTANgmyJm3NqS_L 



    Previous Article
    Next Article

    Similar Reads

    How to Display Path of an Executable File in Linux | Which Command
    In Linux finding the exact path of an excutable file can be crucial for the system adminstration, scripting and as well for troubleshooting. The `which` command helps with providing a simple and effective way to locate the executable files within the directories that are listed in your system. In this article, we are going to discuss on what is whi
    6 min read
    Bash Script - Difference between Bash Script and Shell Script
    In computer programming, a script is defined as a sequence of instructions that is executed by another program. A shell is a command-line interpreter of Linux which provides an interface between the user and the kernel system and executes a sequence of instructions called commands. A shell is capable of running a script. A script that is passed to
    3 min read
    What Does "chmod +x " Do and How to Use It?
    Chmod is a Linux and Unix command that is performed for some basic operations. In Unix and Unix-like operating systems, the chmod Change mode) command and system call alter the access permissions and special mode flags (specifically, the setuid, setgid, and sticky flags) associated with file system objects, which encompass both files and directorie
    5 min read
    How To Fix - chmod: Operation not permitted
    In Linux, while performing permission operations using the chmod command, you might encounter the "chmod: Operation not permitted" error. This error typically occurs when you lack the necessary privileges to change the permissions of a file or directory. Resolving this issue requires either obtaining appropriate permissions or modifying file owners
    3 min read
    How to Chmod 777 all Subfolders of /var/www
    In Linux, chmod is a command, which is used to change the permissions of files and folders. We can change permissions of Read/Write on a single file, on a single folder, or recursively other subfolders as well. chmod is used to read the current file and write it. S/rwxwxw writes files into one directory or another in RAM using traditional methods l
    6 min read
    Shell Script to Demonstrate Wait Command in Linux
    Wait command is one of the process management commands. There are different process commands in Linux mainly 5 commands are widely used which are ps, wait, sleep, kill, exit. ps is an acronym for process status. It displays information about the active processes. wait command will suspend execution of the calling thread until one of its children te
    4 min read
    script command in Linux with Examples
    The 'script' command in Linux is a versatile tool that allows you to record all terminal activities, including inputs and outputs, making it a valuable resource for developers, system administrators, educators, and anyone who needs to document terminal sessions. This command captures everything displayed on your screen during the session, saving it
    6 min read
    Ccat – Colorize Cat Command Output command in Linux with Examples
    ccat is a command-line tool for Linux and OSX, which is similar to the cat command in Linux. But the difference between cat and ccat is that the ccat shows the content of the file with the syntax highlighted. Currently, ccat supports the following programming languages. JavaScriptJavaRubyPythonGoCJSONInstallation of Ccat First, we are going to see
    2 min read
    How to Display Command History in Linux | history Command
    The command-line interface in Linux provides powerful tools for users, and mastering command history is essential for efficient navigation and retrieval of previously executed commands. The history command is a valuable utility that allows users to view and search through their command history. In this comprehensive guide, we will explore the vario
    4 min read
    Batch Script - How to check if a process is running via a Batch Script
    Given a process or application (.exe) file, the task is to create a batch script that checks whether the process is running or not and output the same. Example: In this example, we have created a batch script by the name process.bat. We can easily check whether a process is currently running or not using the tasklist toolkit. Tasklist allows us to
    2 min read
    How to write a shell script that starts tmux session, and then runs a ruby script
    Scripts are important for streamlining workflows and automating tasks. One such way to streamline workflow is by using Tmux [Terminal Multiplexer]. When used along with shell scripts we will be able to automatically commence a Tmux session and perform certain tasks. In this article, we shall see how to use a shell script that will automatically sta
    4 min read
    Linux make Command with Examples
    The make command for Linux is a very useful utility in the automation of software development and performing tasks in a Linux environment. It simply reads a special file, which is called a Makefile and this file describes how one's program is compiled and linked with another file or another program action. This includes information on what files de
    6 min read
    Bash shell script to find out the largest value from given command line arguments
    Write a shell script to find out the largest value from the given number of command-line arguments. Example: Special variables in bash: $@- All arguments. $#- Number of arguments. $0- Filename. $1, $2, $3, $4 ... - Specific arguments. Approach If the number of arguments is 0, end the program. If not zero, then Initialize a variable maxEle with firs
    1 min read
    Batch Script - Echo Command
    Batch script is a type of scripting language used in Windows operating systems to automate repetitive tasks, perform system administration functions, and execute a series of commands. The echo command is one of the most commonly used commands in batch scripting, used to display text or messages on the console or in a text file. In batch scripting,
    8 min read
    Shell Script to Display the Exit Status Using Grep Command
    Linux provides users a great cool feature of the command-line tool along with a graphical user interface where they can perform tasks via ruining command. All of this command returns a status according to their execution. Its execution value can be used for showing errors or take some other action in a shell script. Exit status is following accordi
    4 min read
    Bash Script - Command Substitution
    In order to understand command substitution, let us first discuss substitution in shell scripts. Substitution is a functionality by following which we can instruct the shell to substitute the actual value of an expression. Example: In the program below we have firstly created variable str and assigned it with the value "GeeksforGeeks" and then subs
    4 min read
    Bash Script - How to use Command Line Arguments
    In this article, let us see about Command Line Arguments usage in Bash script. Arguments are inputs that are necessary to process the flow. Instead of getting input from a shell program or assigning it to the program, the arguments are passed in the execution part. Positional Parameters Command-line arguments are passed in the positional way i.e. i
    4 min read
    Batch Script - Printing / NETPrint Command
    A Bash script is similar to a simple text file that contains a number of commands that the programmer can write in a command line. In Unix-based systems, these commands are used for performing repetitive tasks. A Bash script consists of a bunch of commands, or it may contain elements like loops, functions, conditional constructs, etc. In other word
    3 min read
    Shell Script which Works Similar to the Unix Command HEAD TAIL
    Prerequisites: Bash Scripting, Shell Function Library HEAD and TAIL command print a specified number of lines of a file from the beginning and from the end respectively. If we don't specify a number, the commands print the entire file. We will make a bash script display.sh which takes two to three arguments. -h or -t option to specify whether to pr
    5 min read
    How to Install Lazy Script in Kali Linux?
    Kali Linux is one of the most advanced hacking OS systems from Linux family. Kali Linux is filled with many hacking tools and supporting learners and hackers worldwide. There are many versions of Kali Linux which provides a good user interface and desired environment. The Lazy Script is designed to help many users to save time and work. The script
    2 min read
    Bash Script to get Low Battery Alert in Linux
    We are going to write a bash script that will speak "Battery Low" when the battery's charging percentage will go below some specified threshold value. Before starting this, first, we need to understand some basic commands and terminology which are described below:Some Bash Commands: acpi: This command returns the percentage of battery charged, whet
    3 min read
    Deleting a User in Linux using Python Script
    Deleting a user from your system or server via a python script is a very easy task. You just need to pass the username of the user and the script will remove the details and all the files of that user.This python script uses userdel Linux command to delete the user.You can directly use userdel command if you remember the command but if you don't th
    2 min read
    Difference Between Single and Double Quotes in Shell Script and Linux
    Single quotes and double quotes are both functional in Linux while working with shell scripts or executing commands directly in the terminal but there is a difference between the way the bash shell interprets them. Single quotes: Enclosing characters in single quotation marks (') holds onto the literal value of each character within the quotes. In
    3 min read
    How to Code Your Own Port Scanner Using BASH Script and netcat Tool in Linux?
    The first step of penetration testing is reconnaissance (information gathering) which involves scanning for open ports in the machine. There are various tools available on the internet to perform port scanning but, the ability to make your own port scanner is just felt amazing. So here are the steps to follow in order to achieve the same. To make t
    3 min read
    How to Use Nmap Script Engine (NSE) Scripts in Linux?
    Nmap or Network Mapper is an open-source tool that is used to discover hosts and services on a computer network. It is one of the most powerful and flexible port scanners ever built. To install Nmap on any OS such as Ubuntu or Kali Linux you can use the command. It is highly flexible due to the availability of many NSE scripts available. NSE stands
    5 min read
    How to Enable Shell Script Debugging Mode in Linux?
    Linux is a very popular operating system especially among developers and in the computer science world, as it comes along with its powerful ability to play with the kernel. Major Linux distributions (often referred to as Linux distros) are Open Source (Source Code available and can be modified on request) in nature like Ubuntu, CentOS, etc. Bash is
    5 min read
    How To Pass and Parse Linux Bash Script Arguments and Parameters
    Parsing and Passing of Arguments into bash scripts/ shell scripts is quite similar to the way in which we pass arguments to the functions inside Bash scripts. We'll see the actual process of passing on the arguments to a script and also look at the way to access those arguments inside the script. Passing arguments before running We can pass paramet
    7 min read
    Shell Script to Scrape prices of Cryptocurrency coins in Linux
    Cryptocurrency is such a hype that everyone wants to be a part of it. Even nerds and programmers want to dive into this amazing field because it is quite versatile and interesting in every aspect. So how about scraping the price of cryptocurrency coins such as bitcoin, ethereum, dogecoin, and a ton of other coins using shell script and coingecko.co
    8 min read
    Create multiple users using shell script in Linux
    In Linux, we create users for multiple purposes, so in Linux, it's very common to make new users depending on the tasks. So sometimes we need to create more than one user or multiple users. We can't do it one by one as it would be very time-consuming, so we can use automated scripts to make our tasks easy. Here we have used a shell script to make m
    3 min read
    Shell Script to Automate System Updates and Upgrades in Linux
    Shell Scripts are often used for automating repetitive tasks that we can perform on a daily basis. With shell scripts, you can automate tasks like renaming a bunch of files or making backup scripts and many more complex tasks. As we all know, we often need to update and upgrade Linux packages and delete the older version and unnecessary files and g
    4 min read