[email protected]Support 24/7 · Billing 09:00 – 00:00LinkedInXFacebook
NexonHost
Netherlands Dedicated ServersAmsterdam · Equinix AM6Germany Dedicated ServersFrankfurt · Equinix FR4Romania Dedicated ServersBucharest · VoxilityAustria Dedicated ServersVienna · Interxion VIEBulgaria Dedicated ServersSofia · TelepointFrance Dedicated ServersParis · Interxion PAR — Marseille · Digital Realty MRS3Ireland Dedicated ServersDublin · Equinix DB2Italy Dedicated ServersMilan · Irideos AvalonPoland Dedicated ServersWarsawSpain Dedicated ServersMadrid · Equinix MD2United States Dedicated ServersAshburn · Equinix DC
All articles
Getting startedAugust 11, 2023

How to understand File Permissions in Bash Scripting.

How to understand File Permissions in Bash Scripting.

In Bash scripting, file permissions refer to the access rights and restrictions that are assigned to files and directories. These permissions determine who can read, write, or execute a file or directory. Bash scripts often interact with files and directories, and understanding and managing file permissions is essential for ensuring data security and controlling access to your system’s resources.

What are the file permissions?

File permissions can be described as who, what, and which activities can be done to a file or directory.

  • who refers to users, groups, or others
  • what means to add, delete and set
  • which means read, write, and execute

Basic list views of file and their permissions

The command we use is ls -l to show us the list of files/directory in the current folder we are present. This screenshot is a common example to show permission (using normal centos7 )

From the above screenshot, let’s take a file let’s say password.generator.sh, and see its file permissions

-rwxr-xr-x    1 root     root             325 Jul 19 00:01 password.generator.sh

1     2      3

(r = read , w= write ,  x = execute)

  • 1 represents the permission of the user, they have all the 3 permission to read, write and execute the file
  • 2 represents the group to which the file is associated  it also has all the permissions
  • 3 represent others  which also contains all the 3 permissions

Manage file permissions

To manage file permissions, we have a command called chmod which we can use to change the permission of files and directories.

Method to use chmod command

There are 2 methods to use the command

  • Symbolic method
  • Numeric method

Symbolic Method

Syntax:

chmod whowhatwhich File|directory

  • who is u(user) , g(group) , o(other)
  • what is to  +(add) ,-(remove) ,+(set)
  • which  is r (read), w(write), x(execute)

Let’s see an example: –

Suppose you want to remove read and write permission of group and others from the file

chmod go-rw  <FILE>    (can be any file name)

Example:

chmod go-rw  test.sh The new permission will be like this

-rwxr-xr-x 1 root root 662 Jul 18 14:58 test.sh

Numeric method

Syntax:

chmod  ###  file|directory

#: represents each digit the permission for user, group and others.

4 =read   2=write  1 = execute 

Script for File permission

We can explain the file permission by creating a simple script to execute and change the permission of the file after understanding file permissions.

Step-By-Step commands in script:

Step 1. First, we create a file or check if an existing file is there as mentioned in the screenshot. If the file is not present then we execute the command

   touch <FILENAME>    (this will create a new file)

Step 2.  Our aim is to remove the write permission of others and groups as mentioned in the screenshot we will execute the command in the script

chmod 744 <FILENAME> 

Step 3. Finally, to check if permission is changed or not, we will use

ls -l 

The Script

#!/bin/bash

# script to change permission of file 

# File variable to store file.txt location 
FILE="file.txt"

# to check the file we want to change 
# exists or not
if [[ ! -e  "${FILE}" ]]
then
   echo "creating  ${FILE} file"
   touch file.txt
fi

# to remove  write permission of other and group of file
chmod 744 "${FILE}"

#to check if the permission are removed
ls -l  "${FILE}"

Now we will execute this script and check if it works.

./file_perm.sh

In writing script and executing it is important to check permission always after creating script ls  -l command

Keep reading
How to create software raid 10 from OS install.
July 14, 2023

How to create software raid 10 from OS install.

How To Create Software Raid 10 (Raid 1+0) From OS Install. RAID 10 is designed to offer superior resilience and speed than other RAID levels, as it combines the mirroring protection of RAID 1 with the performance enhancements that RAID 0 offers. Reset and reformat all disk. From the first disk put…

Read article
How to create software raid 6 from OS install.
July 14, 2023

How to create software raid 6 from OS install.

How To Create Software Raid 6 From OS Install. RAID 6 uses two parity stripes, the practice of dividing data across the set of hard disks or SSDs, on each disk. It allows for two disk failures within the RAID set before any data is lost. Reset and reformat all disk. From the first disk put 1Gb in…

Read article
How to Fix “Connection Refused by Port 22” on Debian/Ubuntu.
July 14, 2023

How to Fix “Connection Refused by Port 22” on Debian/Ubuntu.

How To Fix “Connection Refused By Port 22” On Debian/Ubuntu. In this article we will explore different ways we can resolve the Connection Refused issue on an Ubuntu/Debian system. Verify OpenSSH Installation First, check if OpenSSH is installed on your system or not. The command mentioned below…

Read article
Get in Touch

Together, Let’s Build a Faster, Safer Internet.

Build scalable infrastructure with NexonHost — high-performance dedicated servers, VPS hosting, cloud hosting, and DDoS protection across Europe.

Get Started →Contact Us
[email protected]Support 24/7 · Billing 09:00 – 00:00