“It’s easier to ask forgiveness than it is to get permission” ~ Rear Admiral Grace Hopper
There is a certain way to add executable permission to either files or folders in any Linux operating system distribution. It is normally can be done by using a general command of ‘chmod’. Off course adding an executable permission in this context is about executing the right command in a CLI (Command Line Interface).
The command is chosen to apply changes to files or folders with certain mode, so to remember it easily we can say that ‘chmod’ is an abbreviation of ‘change mode‘. We are changing permission of files or folders by changing the mode of file bits. That is the information acquired from the command manual page. It is retrieved by executing the following command in the bash prompt :
Output of the command :
The command itself is going to be executed within a text mode using the Command Line Interface with the help of bash prompt in Linux operating system distribution.
Below is how to execute the ‘chmod’ command and an example of its usage :
chmod +x file_or_folder_name
This is a scenario which is going to be done to prove the execution of the command :
1. Create a file which is going to be tested with the command. Below is the process of creating the file :
user@hostname:~$ cd /home/user/test-folder/ user@hostname:~/test-folder$ touch my-bash-script
2. Fill the newly created file with the following content. And in this article, I am using vim text editor to modify the file specififed in step 1 :
username@hostname:~/test-folder$ vim my-bash-script
Below is the content :
#!/bin/bash # Script test echo "Hello World !"
3. Execute the script.
user@hostname:~/test-folder$ ./my-bash-script bash: ./my-bash-script: Permission denied user@hostname:~/test-folder$
As shown above, a user called ‘user’ whom execute the script named my-bash-script doesn’t have any permission to execute the script. The condition above can be changed by altering the permission of the script so that the user can execute the script. Before altering the permission of the file, below is the original permission of the file. The information about the original permission of the file can be retrieved by executing the following command :
It is a command to list the all the content of a certain directory in a listing format.
user@hostname:/home/username/test-folder# ls -al total 44 drwxrwxr-x 2 user user 4096 Sep 3 21:30 . drwxr-xr-x 112 user user 20480 Sep 3 09:13 .. -rw-r--r-- 1 user user 52 Sep 3 21:30 my-bash-script ... user@hostname:/home/user/test-folder#
As we can see from the above output, the file called my-bash-script doesn’t have any executable permision although it is owned by the user itself whom previously tried to execute the file. It can be concluded that way since there wasn’t any ‘x’ character or identifier in the permission information label as shown above. It will be shown again below :
-rw-r--r-- 1 user user 52 Sep 3 21:30 my-bash-script
So, in order for user to be able to execute the file, the permission needs to be modified by adding executable permission to the file itself.
4. Add executable permission to the file. It can be done by switching to ‘root’ user as follows :
sudo su -
By executing the above command in bash prompt, a normal user can have a ‘root’ privilege so that the user itself can exercise one of the root default privilege which is modifying permission of files or folders.
user@hostname:/home/user/test-folder# [sudo] password for user: root@hostname:~# cd /home/user/test-folder/
In the context of this article, modification of permission of a file by adding executable permission to the file itself. The step is being displayed as follows :
chmod +x file_or_folder_name
chmod : the command used to change permission of files or folders.
+x : it is a parameter of the command which is used to add executable permission to every attribute of owner, group and other.
file_or_folder_name : the name of file of folder we want to change the file bits mode.
Below is the output of exercising the above command pattern :
root@hostname:/home/user/test-folder# chmod +x my-bash-script
5. Switch back to normal user by executing the following command :
Below is the output of executing the command :
root@hostname:/home/user/test-folder# exit logout user@hostname:~/test-folder$
6. Re-execute the above script which has its permission changed. We can see the change of the permission information of the file by re-execute the following command :
user@hostname:~/test-folder$ ls -al total 44 drwxrwxr-x 2 user user 4096 Sep 3 22:31 . drwxr-xr-x 112 user user 20480 Sep 4 06:32 .. -rwxr-xr-x 1 user user 52 Sep 3 21:30 my-bash-script ... user@hostname:~/test-folder$
As we can see from the above output command, the file has already have executable permission. So, we can just execute it with the following command in the bash prompt :
Below is the output command which is showing that the file can be executed :
user@hostname:~/test-folder$ ./my-bash-script Hello World ! user@hostname:~/test-folder$