DEV Community

Cover image for Beyond Read and Write: Executing Files with chmod
\144\150\162\165\166(dhruv)
\144\150\162\165\166(dhruv)

Posted on • Edited on

Beyond Read and Write: Executing Files with chmod

In Linux, a non-executable file is one that cannot be directly run by the system as a program. These files typically contain data or instructions that require another program to interpret or process them. There are two main reasons a file might not be executable:

  • Missing permissions: In Linux, file permissions control what users can do with a file. These permissions include read, write, and execute. By default, some files might only have read/write permissions, meaning they can be viewed or modified but not directly executed.

  • File type: Certain file types, like text files (.txt), image files (.jpg), or document files (.pdf), are not meant to be executable programs. They contain data meant to be processed by specific applications.

Examples of non-executable files:

  • Data files: These include various file formats that store information, like images, documents, spreadsheets, audio, and videos. They require specific software to be opened and used.

  • Configuration files: These files contain settings for programs or the system itself. They are typically text files and are read by software to configure its behavior.

  • Log files: These files record system events or application activity. They are mainly for troubleshooting and are not meant to be executed.

  • Shell scripts: Shell scripts are text files containing commands intended to be executed by the shell. However, if they lack the necessary permissions (explained below), they won't be executable directly.

Finding non-executable files:

You can use the find command with the -not operator (if using GNU find) to search for files that lack execute permissions.

Making non-executable files executable:

  • Shell scripts: For shell scripts, you can grant them execute permission using the chmod command. This allows the script to be run from the terminal.

  • Other files: While possible, it's generally not recommended to make non-executable files executable unless you understand why they are non-executable and what the intended use is.

Understanding the 'chmod' Command:

The chmod command is an integral part of any Unix-like operating system and has been around since the early days of Unix. It stands for "change mode" and is used to define the way a file can be accessed.

How to use it
The basic usage of chmod involves invoking the command followed by the permissions you want to set and then the file or directory you're modifying.

$ chmod 755 filename.txt
Enter fullscreen mode Exit fullscreen mode

Let's break down the command:

755: This is the permission setting you are applying. It's represented in octal notation (base-8). Here's how it breaks down:

  • The first digit (7) represents the permissions for the owner of the file:
r (4) - Read permission
w (2) - Write permission
x (1) - Execute permission (since 7 = 4 + 2 + 1)
Enter fullscreen mode Exit fullscreen mode
  • The second digit (5) represents the permissions for the group that owns the file:
  r (4) - Read permission
  x (1) - Execute permission (since 5 = 4 + 1)
Enter fullscreen mode Exit fullscreen mode
  • The third digit (5) represents the permissions for everyone else (users who are not the owner or part of the group):
  r (4) - Read permission
  x (1) - Execute permission (since 5 = 4 + 1)
Enter fullscreen mode Exit fullscreen mode

In simpler terms, this command sets the following permissions for filename.txt:

  1. Owner: can read, write, and execute the file.
  2. Group: can only read and execute the file.
  3. Everyone else: can only read and execute the file. Here are some important points to consider:

This is a common permission setting used for executable files (like shell scripts) that need to be run by users.
Be cautious when using 777 (read, write, and execute for everyone) as it grants unrestricted access to the file, which can be a security risk. You might need to use sudo before the command (sudo chmod 755 filename.txt) if you don't have the necessary permissions to modify the file.

The commonly used parameters

There are several permissions that you can set with chmod:

-u stands for 'user', the owner of the file.

$ chmod u+x filename.txt
Enter fullscreen mode Exit fullscreen mode

-g stands for 'group', users who are members of the file's group.

$ chmod g+w filename.txt
Enter fullscreen mode Exit fullscreen mode

-o stands for 'others', users who are not the owner of the file or members of the group.

$ chmod o-r filename.txt
Enter fullscreen mode Exit fullscreen mode

Other supported parameters:
chmod supports a number of parameters like -f (force), -v (verbose), -c (changes), -R (recursive), --help (display help and exit), and --version (output version information and exit). Each parameter provides a specific functionality to the chmod command.

Warning:
It’s crucial to be careful when changing permissions, especially when using the -R (recursive) option. If misused, you could inadvertently give users unintended access to sensitive files or directories.

Can we Run a Script that's not Executable?

yes, we can!
If a shell script isn’t executable, we cannot run it directly in the terminal. Of course, the execute permission of the script can be changed so that we can run it. But, that may not always be possible. For example, if the script belongs to another user, we may not have the necessary privileges to change the execute permission of the script using the chmod command.

If we have an executable script, we can run it directly in the terminal. Suppose we have the following script, hello_world.sh:

#!/bin/bash
echo “Hello World"
Enter fullscreen mode Exit fullscreen mode

This script just prints Hello World and exits. We specify the interpreter of the script on the first line after the shebang (#!). In our case, the interpreter is /bin/bash. This tells the operating system to use Bash as the interpreter for parsing the remainder of the script.

Let’s first check the execute permission of this script:

$ -l hello_world.sh
-rwxr-xr-x 1 dt dt 23 April  08:32 hello_world.sh
Enter fullscreen mode Exit fullscreen mode

Since the execute permission of the script is set for all users, we can run it directly in the terminal:

$ ./hello_world.sh
Hello World
Enter fullscreen mode Exit fullscreen mode

Now, we’ll consider the case when the script isn’t executable. First, let’s remove the execute permission of the script hello_world.sh using chmod:

$ chmod -x hello_world.sh
$ ls –l hello_world.sh
-rw-r--r-- 1 dt dt 23 April  08:32  hello_world.sh
Enter fullscreen mode Exit fullscreen mode

As it’s apparent from the output of the ls –l command, nobody not even the owner of the script – has the right to execute this script. Let’s try to run it:

$ ./hello_world.sh
-bash: ./hello_world.sh: Permission denied
Enter fullscreen mode Exit fullscreen mode

Our objective is to run the script hello_world.sh in the remainder of this tutorial.

One way to run a script that we don’t have the execute permission is to specify the shell explicitly while running the script. Let’s run the script, hello_world.sh, with the Bash interpreter:

$ /bin/bash ./hello_world.sh
Hello World
Enter fullscreen mode Exit fullscreen mode

If we’re able to specify the interpreter while running the script, we can run the script with another interpreter, for example, the Korn shell:

$ /bin/ksh ./hello_world.sh
Hello World
Enter fullscreen mode Exit fullscreen mode

Another way to run a script that isn’t executable is using the source command. Let’s run the script, hello_world.sh, using source:

$ source ./hello_world.sh
Hello World
Enter fullscreen mode Exit fullscreen mode

Sourcing a script means that we run the script in the current shell. So, if the script contains an exit command, we must be cautious since we may exit from the current shell itself.

We can use the dot or period character (.) instead of source and get the same result:

$ . ./hello_world.sh
Hello World
Enter fullscreen mode Exit fullscreen mode

If you have any feedback, then you can DM me on Twitter or Linkedin.

Top comments (0)