bash scripting for devops

*Shreyash Bhise | Aspiring Mern Stack Developer and DevOps enthusiast,
lsList files and directories in the current directory.
- Use: To see what files and folders are in the current directory.
mkdirCreate a new directory.
- Use: To make a new folder or directory.
rm or rmdirRemove files or directories.
- Use: To delete files or directories.
cd: Change the current directory.
- Use: To navigate to different directories within the filesystem.
pwdPrint the current working directory.
- Use: To find out the full path of the current directory.
catDisplay the contents of a file.
- Use: To view the contents of a text file.
mkdir -p random/test/folderuse: To create middle directory between two directory
touchCreate 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.
cpCopy files and directories.
- Use: To duplicate files or directories.
mvMove or rename files and directories.
- Use: To change the name of a file or move it to a different location.
cp -R
We use this command to copy the directory
sudoExecute a command as a superuser or with elevated privileges.
- Use: To run commands with administrative permissions.
df
This command is use to disply disk space usage
du
This command is used to estimate and display the disk space usage by file and directory.
head and tailDisplay the beginning or end of a file.
- Use: To view the first or last few lines of a file.
diffThis command is used to compare the content of two text files and display the difference between them.
locateThis command is used for quickly search file and directory in linux.
find or find . -type d or find . -type f or find . -type f -name file.txtSearch for files and directories within a directory hierarchy.
- Use: To locate files and directories based on various criteria.
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:
Owner (u): The user who owns the file or directory.
Group (g): A group of users who share common permissions.
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).
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
chmodChange file and directory permissions.
- Use: To modify file and directory permissions to control access.
sudoExecute a command as a superuser or with elevated privileges.
- Use: To run commands with administrative permissions.
grep grep "kunal" names.txt grep -w "python devops" names.txt grep -n "kunal" names.txt grep -win "kunal" names.txtSearch for text in files using patterns.
- Use: To search for specific text patterns within files.
Basic Navigation:
Ctrl + AorHome: Move the cursor to the beginning of the line.Ctrl + EorEnd: 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 usingCtrl + U,Ctrl + K, orCtrl + W.
wget
This command download the file from specified url and saves it as.
top
onitor system performance and view running processes.
- Use: To monitor system resource usage in real-time.
ifconfig or ip: Display network interface information.
- Use: To view network configuration and IP addresses.
psList currently running processes.
- Use: To see a list of running processes on the system.
killTerminate processes.
- Use: To forcefully stop a running process.
lscpuThis command is used to display detailed information about central processing unit and characteristic.
The
unamecommand 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 useunamewith different options to extract specific information from your system.Here are some common uses of the
unamecommand:Display the system's kernel name:
uname -sThis will show the kernel name, which is typically "Linux."
Display the network node hostname:
uname -nThis shows the network node name, which is often the system's hostname.
Display the kernel release version:
uname -rThis displays the kernel release version, which can provide information about the kernel's build and patch level.
Display the kernel version information:
uname -vThis shows the kernel version information, which may include details about the kernel's compilation date and time.
Display the machine hardware name:
uname -mThis shows the machine's hardware name or system architecture, such as "x86_64" for 64-bit systems.
Display the processor type:
uname -pThis provides information about the processor type.
Display the operating system name:
uname -oThis shows the operating system name, which is often "GNU/Linux" for Linux-based systems.
Display all system information:
To display all available information, you can use:
uname -aThis 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.
nslookup or hostResolves domain name to ip address and vice versa
netstatDisplays network statistic , Routing tables,interface information, and active network connections.
pingTests network connectivity by sending ICMP echo requests to host or ip address.
Bash Scripting For DevOps :
Write your first bash script: Print Hello World !
#!/bin/bash echo "Hello World"chmod +x myscript.sh ./myscript.sh
Variable in Bash Scripting:
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
#!/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
#!/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.
#!/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
variableis a user-defined variable that takes on the values from thelist.listis 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
conditionis 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:
Set the
source_dirvariable to the directory you want to back up.Set the
backup_parent_dirvariable to the directory where you want to store the backup directories.Generate a timestamp in the format "YYYYMMDDHHMMSS."
Create a backup directory with the timestamp as part of the directory name.
Use
mkdir -pto create the backup directory if it doesn't already exist.Use the
cpcommand to recursively copy the contents of the source directory to the backup directory.Check the exit status of the
cpcommand. 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:
- 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!"
- How to check the computer name or hostname in Linux ?
Ans: Using "hostname" command
- How to check name of current user in Linux terminal ?
Ans: whoami
- How to check your current path/directory you are working in ?
Ans: pwd
- 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
- Which command is used to create file in Linux ?
Ans:
touch
vi
vim
nano
- How will you edit an existing file on Linux ?
Ans: Using vi , vim, nano etc
- How to rename a file in Linux ?
Ans: mv
- How to search a string in a file ?
Ans: Using grep command
- Difference between grep and egrep command ?
Ans: In egrep, you can search for more than one strings at same time.
- How can you read a file without using cat command ?
Ans: Using less,more,vi,nano command
- 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.
- How to check a file permission ?
Ans: ls -la , ll , getfacl filename.
- How to check the IP of your Linux Server ?
Ans: ifconfig , ip addr
- How to read Top 5 lines in a file ?
Ans: head -5 file_name
- How to read Last 5 lines in a file ?
Ans: tail -5 file_name
- How to list hidden files ?
Ans: ls -la
- How to see all the recently used commands ?
Ans: history
- What is root ?
Ans: Admin or super user /root directory
- What is inode and how to find it for a file ?
Ans: ls -li , inode is an index node .
- Which command can you use for finding files on Linux system ?
Ans: Using find and locate command
- Command for counting words and lines ?
Ans: wc and wc -l
- 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
- How to view the difference between two files ?
Ans: diff
- 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
- How to check system architecture info ?
Ans: dmidecode and lscpu
- How to combine two files ?
Ans:
cat file1 file2
cat file1 file2 > file3
- How can you find the type of file ?
Ans: file file1.txt
- How to sort the content of a file ?
Ans:
sort file1.txt
cat file_name | sort
- Different ways to access Linux server remotely ? (from a windows machine )
Ans:
putty
git bash
cmd
- What are different type of permission for file in Linux ?
Ans:
read (r)
write (w)
executable (x)
- How to write a output of a command into a file ?
Ans: command > file_name
- How to write something in a file without deleting the existing content ?
Ans: We can append the file using >>
- 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
- How to automate any task or script ?
Ans: Using cron jobs, For which we have crontab and at command
- How to check scheduled jobs ?
Ans: crontab -l
- What is the meaning of this cronjob ? *****
Ans: Minute, hour, Day of the month, Month, Day of the week
- If your cron job didn't work ? How would you check ?
Ans:
Check system time
Crontab entry
check /var/log/message
- What is daemon service ?
Ans: Service that keep running in background . Example: httpd, sshd, chronyd
- How to check if service is running or not ?
Ans: systemctl status service_name
- How to start/stop any service ?
Ans: systemctl start service_name, systemctl stop service_name
- How to check for free Disk space ?
Ans: We can use df command .
- How to check the size of a directory content ?
Ans: We can use du command ?
- How to check CPU usage for process ?
Ans: We can use top command
- 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)
- How to check processs/application is running or not ?
Ans: Using ps command
- How to terminate/stop running process ?
Ans: Using kill command
- Differnece between kill and kill -9
Ans: Kill -9 will terminate a program forcefully
- How to check if IP/Server is accessible or not ?
Ans: We can use ping or telnet command.
- Which command to use to get info about ports ?
Ans: We can use netstat command
- How to check open port on Linux system ?
Ans: netstat putan | grep port_no
- How to check network interfaces in Linux ?
Ans: We can use ifconfig and netstat command
- Difference between Telnet and ssh ?
Ans: ssh is secured and telnet is not .
- Which service should be running on server to allow you to connect remotely ?
Ans: ssh or sshd
- What is ssh ?
Ans: ssh or secure shell is a network communication protocol that enables two computer/devices to communicate and share data.
Why it is called as secure shell ?
Ans : Because communication betweeen host and client will be in encrypted formate.
What is default port for ssh ?
Ans: 22




