Shell Scripting for Devops Beginners : Part 1

Linux is the backbone of DevOps, cloud engineering, and backend systems. Whether you’re debugging servers, analyzing logs, or writing CI/CD scripts these commands will be your daily tools.

This guide gives you the most used file, directory, process, networking, and shell scripting commands with simple explanations.

  1. File and Directory Commands

2. How to Execute a Shell Script

There are two ways :-

./script.sh
sh script.sh

(Requires chmod +x script.sh, incase of permissions.
But what is chmod ?

3. Good Practices for Shell Scripts

  • Add metadata/comments at the top
  • Use safety flags:
set -x // Enables debug mode prints each command and its output before running it.
set -e // If an error occurs anywhere in the script, then the script fails immediately

4. What is sudo?

sudo = super user do

Allows to execute commands as a root/admin user.

nd if we want to switch user and execute something we do this to Switch user:

sudo su pradyumna

5. chmod = Change file permissions

It controls who can read, write, and execute a file or directory.

chmod <permissions> <file>

Examples:

chmod 755 script.sh
chmod 644 myfile.txt
chmod +x script.sh

Permissions have three roles:

  • Owner or Administrator
  • Group
  • Others

and the permissions are these three :

  • r = read
  • w = write
  • x = execute
Two Ways to Use chmod
  • Octal method
chmod 755 script.sh // means owner has (rwx) privileges and group and users have (r - x) permissions
  • Symbolic method

Using letters:

  • u = user (owner)
  • g = group
  • o = others
  • a = all
chmod +x script.sh // makes file executable
chmod o-w file.txt // removes write permission for others 
chmod g+r file.txt // adds read access for groups
chmod +x deploy.sh // the deploy shell script can be executed (it adds   +x to all owners, groups and users)
chmod 644 file.txt // means files are readable by all
chmod 777 file.txt // complete open permission to do whatever to alll

6. chown - Change owner

It is just like chmod, but instead of changing permissions, it changes ownership.

It changes the owner and/or group of a file or folder.

chown pradyumna file.txt
chown pradyumna:admin file.txt // this is for changing ownership of group
chown :admin file.txt // change group only).

We mostly use chown when we create a file with root, but want user to edit them.

So chown changes the owner of the file and chmod changes what permission the file has

We will talk about some Networking Commands, Process Management commands and loops and conditional operators in Shell scripting along with some interview questions in the next one.

Hope you have a nice day.
If you liked this article, then please do like, subsscribe and share it with your friends and family.

Subscribe to my Substack

Get thoughtful insights on technology, AI, and software engineering delivered straight to your inbox. No spam, ever.

Weekly Updates
I will send you an update each week to keep you filled in on what I have been up to.
No Spam
Your email stays private, I’ll never share or sell it.