Skip to main content

Command Palette

Search for a command to run...

bash scripting for devops

Updated
•16 min read•View as Markdown
bash scripting for devops
S

*Shreyash Bhise | Aspiring Mern Stack Developer and DevOps enthusiast,

  1.         ls
    

    List files and directories in the current directory.

    • Use: To see what files and folders are in the current directory.
  1.         mkdir
    

    Create a new directory.

    • Use: To make a new folder or directory.
  1.         rm or rmdir
    

    Remove files or directories.

    • Use: To delete files or directories.
  1.         cd
    

    : Change the current directory.

    • Use: To navigate to different directories within the filesystem.
  1.         pwd
    

    Print the current working directory.

    • Use: To find out the full path of the current directory.
  1.         cat
    

    Display the contents of a file.

    • Use: To view the contents of a text file.
  1.         mkdir -p random/test/folder
    

    use: To create middle directory between two directory

  2.         touch
    

    Create an empty file or update the timestamp of an existing file.

    • Use: To create an empty file or update the timestamp of an existing file.
  1.         cp
    

    Copy files and directories.

    • Use: To duplicate files or directories.
  1.  mv
    

    Move or rename files and directories.

    • Use: To change the name of a file or move it to a different location.
  1.  cp -R
    

We use this command to copy the directory

  1.         sudo
    

    Execute a command as a superuser or with elevated privileges.

    • Use: To run commands with administrative permissions.
  1.         df
    

This command is use to disply disk space usage

  1.         du
    

This command is used to estimate and display the disk space usage by file and directory.

  1.         head and tail
    

    Display the beginning or end of a file.

    • Use: To view the first or last few lines of a file.
  1.         diff
    
  2. This command is used to compare the content of two text files and display the difference between them.

  3.         locate
    

    This command is used for quickly search file and directory in linux.

  4.         find  or find . -type d or find . -type f or find . -type f -name file.txt
    

    Search for files and directories within a directory hierarchy.

    • Use: To locate files and directories based on various criteria.
  1.         chmod 700 file.txt
    

In Linux and Unix, there are three primary permissions: read (r), write (w), and execute (x). These permissions are set for three different categories of users:

  1. Owner (u): The user who owns the file or directory.

  2. Group (g): A group of users who share common permissions.

  3. Others (o): Everyone else who is not the owner or a member of the group.

Here are some commonly used permissions:

  • r: Read permission (4 in octal).

  • w: Write permission (2 in octal).

  • x: Execute permission (1 in octal).

  1.         chmod u=rwx,g=rx,o=r upper.txt
    

Creating a New Group:

To create a new group, use the groupadd command, followed by the name of the group you want to create. For example, to create a group named "developers," use the following command:

sudo groupadd developers

Creating a New User:

To create a new user, use the useradd command, followed by the name of the user you want to create. For example, to create a user named "john," use the following command:

sudo useradd john

Adding user to group:

sudo usermod -aG groupname username

1. List all groups: You can use the getent command to list all the groups on your system. This command retrieves entries from databases, including the /etc/group file, which contains group information.

bash
getent group

2. List all users: You can use the getent command to list all the users on your system. This command retrieves entries from databases, including the /etc/passwd file, which contains user information.

bash
getent passwd

Deleting a User:

To delete a user, you can use the userdel command, followed by the username you want to remove. Additionally, you can use the -r option to remove the user's home directory and files.

Here's the basic syntax to delete a user without removing the home directory:

sudo userdel username
sudo passwd username
  1.         chmod
    

    Change file and directory permissions.

    • Use: To modify file and directory permissions to control access.
  1.         sudo
    
    1. Execute a command as a superuser or with elevated privileges.

      • Use: To run commands with administrative permissions.
  1.        grep
           grep "kunal" names.txt
           grep -w "python devops" names.txt
           grep -n "kunal" names.txt
           grep -win "kunal" names.txt
    

    Search for text in files using patterns.

    • Use: To search for specific text patterns within files.

Basic Navigation:

  • Ctrl + A or Home: Move the cursor to the beginning of the line.

  • Ctrl + E or End: Move the cursor to the end of the line.

  • Ctrl + U: Delete from the cursor to the beginning of the line.

  • Ctrl + K: Delete from the cursor to the end of the line.

  • Ctrl + W: Delete the word before the cursor.

  • Ctrl + Y: Paste the text that was deleted using Ctrl + U, Ctrl + K, or Ctrl + W.

  1.         wget
    

This command download the file from specified url and saves it as.

  1.         top
    

onitor system performance and view running processes.

  • Use: To monitor system resource usage in real-time.
  1.        ifconfig or ip
    

    : Display network interface information.

    • Use: To view network configuration and IP addresses.
  1.         ps
    

    List currently running processes.

    • Use: To see a list of running processes on the system.
  1.         kill
    

    Terminate processes.

    • Use: To forcefully stop a running process.
  1.        lscpu
    

    This command is used to display detailed information about central processing unit and characteristic.

  2. The uname command is used in Linux and Unix-based operating systems to display system information. It stands for "Unix Name" and provides various details about the system, such as the kernel version, system architecture, and more. You can use uname with different options to extract specific information from your system.

    Here are some common uses of the uname command:

    1. Display the system's kernel name:

       uname -s
      

      This will show the kernel name, which is typically "Linux."

    2. Display the network node hostname:

       uname -n
      

      This shows the network node name, which is often the system's hostname.

    3. Display the kernel release version:

       uname -r
      

      This displays the kernel release version, which can provide information about the kernel's build and patch level.

    4. Display the kernel version information:

       uname -v
      

      This shows the kernel version information, which may include details about the kernel's compilation date and time.

    5. Display the machine hardware name:

       uname -m
      

      This shows the machine's hardware name or system architecture, such as "x86_64" for 64-bit systems.

    6. Display the processor type:

       uname -p
      

      This provides information about the processor type.

    7. Display the operating system name:

       uname -o
      

      This shows the operating system name, which is often "GNU/Linux" for Linux-based systems.

    8. Display all system information:

      To display all available information, you can use:

       uname -a
      

      This command provides a comprehensive output that includes the system name, network node name, kernel release, kernel version, machine hardware name, and processor type.

The uname command is useful for system administration, scripting, and obtaining specific information about the operating system and hardware, which can be helpful for various tasks and troubleshooting.

  1.       nslookup or host
    

    Resolves domain name to ip address and vice versa

  2.       netstat
    

    Displays network statistic , Routing tables,interface information, and active network connections.

  3.       ping
    

    Tests network connectivity by sending ICMP echo requests to host or ip address.

Bash Scripting For DevOps :

Write your first bash script: Print Hello World !

  1.       #!/bin/bash
          echo "Hello World"
    
     chmod +x myscript.sh
     ./myscript.sh
    

Variable in Bash Scripting:

  1.       myname="jay"
          age="40"
          echo "Hello, my name is $myname."
          echo "Im $age year old."
    

Conditional statements in Bash are used to make decisions in your scripts based on certain conditions. The most commonly used conditional statement in Bash is the if statement, which allows you to execute different commands or code blocks depending on whether a condition is true or false. Here's how you can use the if statement with examples:

Basic if statement:

if [ condition ]; then
    # code to execute if the condition is true
fi

In Bash, [ and ] are used to enclose the condition, and then marks the beginning of the code block to execute if the condition is true.

Example 1: Check if a file exists:

if [ -e /path/to/file ]; then
    echo "The file exists."
fi

In this example, the -e option checks if the file exists. If it does, the message "The file exists" is printed.

Example 2: Check if a number is greater than 10:

number=15

if [ $number -gt 10 ]; then
    echo "The number is greater than 10."
fi

In this example, the -gt operator checks if the variable number is greater than 10. If it is, the message is displayed.

Example 3: Check if a user entered a specific value:

read -p "Enter a number: " user_input

if [ $user_input -eq 5 ]; then
    echo "You entered 5."
fi

In this example, the -eq operator checks if the user's input is equal to 5. If it is, the message is printed.

if-else statement:

You can also use the if-else statement to provide an alternative code block to execute when the condition is false.

if [ condition ]; then
    # code to execute if the condition is true
else
    # code to execute if the condition is false
fi

Example 4: Check if a number is even or odd:

number=12

if [ $((number % 2)) -eq 0 ]; then
    echo "The number is even."
else
    echo "The number is odd."
fi

In this example, the script calculates the remainder when the number is divided by 2. If the remainder is 0, the number is even; otherwise, it's odd.

These are basic examples of conditional statements in Bash. You can use various operators (e.g., -eq, -ne, -lt, -le, -gt, -ge) to compare values and make more complex decisions in your Bash scripts.

Practice Example: Write a bash script to create a 10 file

  1.       #!/bin/bash
          for i in {1..10}
          do
             touch "file_$i.txt"
             echo File $i created
          done
          echo "All 10 file is successfully created"
    

Practice Example: Write a bash script to check the number is even or odd

  1.       #!/bin/bash
          read -p "Enter a number:" number
          if [ $((number % 2)) -eq 0 ]; then
                echo "The number is Even"
          else
                echo "The number is Odd"
          fi
    

practice example3: This scripts checks if specific process is running and if it's not it start the process.

  1.       #!/bin/bash
          process_name="httpd"
          if pgrep -x "$process_name"; then
              echo "$process_name is running.."
          else
               echo "$process_name is not running. starting it .."
               sudo systemctl start $process_name
          fi
    

In Bash scripts, you can use various types of loops to execute a set of commands repeatedly. The most common types of loops in Bash are for loops and while loops. I'll explain each of them with examples.

1. for Loop:

A for loop is used to iterate over a list of items or to execute a set of commands a specified number of times.

Syntax:

for variable in list
do
    # Commands to be executed
done
  • variable is a user-defined variable that takes on the values from the list.

  • list is a space-separated list of items or a range of values.

Example 1: Iterate over a list of items:

fruits="apple banana cherry"
for fruit in $fruits
do
    echo "I like $fruit"
done

In this example, the for loop iterates over a list of fruits and prints a message for each fruit.

Example 2: Iterate through a range of numbers:

for num in {1..5}
do
    echo "Number: $num"
done

This loop counts from 1 to 5 and prints the numbers.

2. while Loop:

A while loop is used to repeatedly execute a set of commands as long as a specified condition is true.

Syntax:

while [ condition ]
do
    # Commands to be executed
done
  • condition is a test that determines whether the loop should continue or terminate.

Example: Use a while loop to count from 1 to 5:

count=1
while [ $count -le 5 ]
do
    echo "Count: $count"
    count=$((count + 1))
done

In this example, the while loop continues as long as the condition (count is less than or equal to 5) is true. It prints the count and increments it with each iteration.

These are the basic constructs of for and while loops in Bash. You can use loops to automate repetitive tasks, process lists of items, or perform actions based on conditions. Bash also supports nested loops and various loop control commands like break and continue to control the flow within loops.

Practice Example 4: Here's a simplified Bash script that creates a backup of a directory by copying its contents to a backup directory with a timestamp in the directory name,

#!/bin/bash

# Source directory to be backed up
source_dir="/path/to/source_directory"

# Backup directory where the backup will be stored
backup_parent_dir="/path/to/backup_parent_directory"

# Create a timestamp in the format "YYYYMMDDHHMMSS"
timestamp=$(date +"%Y%m%d%H%M%S")

# Backup directory with the timestamp in the name
backup_dir="$backup_parent_dir/backup_$timestamp"

# Create the backup directory
mkdir -p "$backup_dir"

# Copy the contents of the source directory to the backup directory
cp -r "$source_dir"/* "$backup_dir"

# Check if the backup was successful
if [ $? -eq 0 ]; then
    echo "Backup completed successfully. Contents copied to $backup_dir."
else
    echo "Backup failed. Please check the source and backup directories."
fi

In this script:

  1. Set the source_dir variable to the directory you want to back up.

  2. Set the backup_parent_dir variable to the directory where you want to store the backup directories.

  3. Generate a timestamp in the format "YYYYMMDDHHMMSS."

  4. Create a backup directory with the timestamp as part of the directory name.

  5. Use mkdir -p to create the backup directory if it doesn't already exist.

  6. Use the cp command to recursively copy the contents of the source directory to the backup directory.

  7. Check the exit status of the cp command. If it's successful (exit code 0), it prints a success message; otherwise, it prints a failure message.

You can run this script periodically using a cron job or manually to create backups without using a tarball.

Interview Question:

  1. What is the use of echo command ?

Ans: echo is a command that outputs the strings that are passed to it as arguments.

ex: echo "Hello World!"

  1. How to check the computer name or hostname in Linux ?

Ans: Using "hostname" command

  1. How to check name of current user in Linux terminal ?

Ans: whoami

  1. How to check your current path/directory you are working in ?

Ans: pwd

  1. Explain the differnece between relative and absolute path ?

Ans:

  • Ralative path: Start from current working directory.

  • Absolute path: The full path to a file or directory

  1. Which command is used to create file in Linux ?

Ans:

  • touch

  • vi

  • vim

  • nano

  1. How will you edit an existing file on Linux ?

Ans: Using vi , vim, nano etc

  1. How to rename a file in Linux ?

Ans: mv

  1. How to search a string in a file ?

Ans: Using grep command

  1. Difference between grep and egrep command ?

Ans: In egrep, you can search for more than one strings at same time.

  1. How can you read a file without using cat command ?

Ans: Using less,more,vi,nano command

  1. What is the advantage of using less command ?

Ans:

  • We can easily read big files.

  • Forward and Backward search is easy.

  • Navigation from top to bottom is easy.

  1. How to check a file permission ?

Ans: ls -la , ll , getfacl filename.

  1. How to check the IP of your Linux Server ?

Ans: ifconfig , ip addr

  1. How to read Top 5 lines in a file ?

Ans: head -5 file_name

  1. How to read Last 5 lines in a file ?

Ans: tail -5 file_name

  1. How to list hidden files ?

Ans: ls -la

  1. How to see all the recently used commands ?

Ans: history

  1. What is root ?

Ans: Admin or super user /root directory

  1. What is inode and how to find it for a file ?

Ans: ls -li , inode is an index node .

  1. Which command can you use for finding files on Linux system ?

Ans: Using find and locate command

  1. Command for counting words and lines ?

Ans: wc and wc -l

  1. How can you combine two command ? or what is pipe used for ?

Ans:

  • we can combine two command using |

  • ex: command1 | command2

  • Pipe is used to combine two commands and redirect output of command1 to command2

  1. How to view the difference between two files ?

Ans: diff

  1. What is the used of the shred command do ?

Ans: permanently delete a file which is unable to recover .

  • shred -u file_name

  • shred --remove file_name

  1. How to check system architecture info ?

Ans: dmidecode and lscpu

  1. How to combine two files ?

Ans:

  • cat file1 file2

  • cat file1 file2 > file3

  1. How can you find the type of file ?

Ans: file file1.txt

  1. How to sort the content of a file ?

Ans:

  • sort file1.txt

  • cat file_name | sort

  1. Different ways to access Linux server remotely ? (from a windows machine )

Ans:

  • putty

  • git bash

  • cmd

  1. What are different type of permission for file in Linux ?

Ans:

  • read (r)

  • write (w)

  • executable (x)

  1. How to write a output of a command into a file ?

Ans: command > file_name

  1. How to write something in a file without deleting the existing content ?

Ans: We can append the file using >>

  1. How to redirect an error of a command into a file ?

Ans: To redirect an error we need to use 2> , To redirect both error and output 2>&1

  1. How to automate any task or script ?

Ans: Using cron jobs, For which we have crontab and at command

  1. How to check scheduled jobs ?

Ans: crontab -l

  1. What is the meaning of this cronjob ? *****

Ans: Minute, hour, Day of the month, Month, Day of the week

  1. If your cron job didn't work ? How would you check ?

Ans:

  • Check system time

  • Crontab entry

  • check /var/log/message

  1. What is daemon service ?

Ans: Service that keep running in background . Example: httpd, sshd, chronyd

  1. How to check if service is running or not ?

Ans: systemctl status service_name

  1. How to start/stop any service ?

Ans: systemctl start service_name, systemctl stop service_name

  1. How to check for free Disk space ?

Ans: We can use df command .

  1. How to check the size of a directory content ?

Ans: We can use du command ?

  1. How to check CPU usage for process ?

Ans: We can use top command

  1. What is a process in Linux ?

Ans : An instance of a running program. Whenever you start a program/application or execute a command , a process is created. For every process a unique .no is assigned which is called PID (Process ID)

  1. How to check processs/application is running or not ?

Ans: Using ps command

  1. How to terminate/stop running process ?

Ans: Using kill command

  1. Differnece between kill and kill -9

Ans: Kill -9 will terminate a program forcefully

  1. How to check if IP/Server is accessible or not ?

Ans: We can use ping or telnet command.

  1. Which command to use to get info about ports ?

Ans: We can use netstat command

  1. How to check open port on Linux system ?

Ans: netstat putan | grep port_no

  1. How to check network interfaces in Linux ?

Ans: We can use ifconfig and netstat command

  1. Difference between Telnet and ssh ?

Ans: ssh is secured and telnet is not .

  1. Which service should be running on server to allow you to connect remotely ?

Ans: ssh or sshd

  1. What is ssh ?

Ans: ssh or secure shell is a network communication protocol that enables two computer/devices to communicate and share data.

  1. Why it is called as secure shell ?

    Ans : Because communication betweeen host and client will be in encrypted formate.

  2. What is default port for ssh ?

    Ans: 22

More from this blog

Shreyash Bhise's blog

55 posts