Part 5: Memory Commands, While Loop, and Vim Command
In this section, we will explore memory management commands, while loops, the Vim text editor, and more. We’ll also create a shell script that ties everything together to monitor disk space.
Important Memory Commands
1. free
Command
The free
command displays the system’s memory usage, including total, used, and free memory.
Example:
free -h
Output:
total used free shared buff/cache available
Mem: 8.0G 2.1G 4.5G 100M 1.4G 5.5G
Swap: 2.0G 500M 1.5G
2. top
Command
The top
command provides real-time information about system processes, including memory and CPU usage.
Example:
top
You can sort the processes by memory usage by pressing Shift + M
while top
is running.
3. df -h
Command
The df -h
command displays disk space usage in a human-readable format.
Example:
df -h
Output:
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 50G 35G 15G 70% /
tmpfs 2.0G 0 2.0G 0% /dev/shm
The awk
Command in Linux
The awk
command is a powerful text-processing tool that allows you to filter and format output.
Example 1: Extracting Specific Columns
df -h | awk '{print $1, $5}'
Output:
Filesystem Use%
/dev/sda1 70%
tmpfs 0%
Example 2: Filtering Rows
df -h | awk '$5 > 70 {print $1, $5}'
Output:
/dev/sda1 70%
This filters filesystems where disk usage is above 70%.
Example 3: Calculating Total Memory
free -h | awk '/Mem:/ {print "Total Memory: " $2 " Used: " $3 " Free: " $4}'
Output:
Total Memory: 8.0G Used: 2.1G Free: 4.5G
While Loop
The while
loop in shell scripting executes a block of code repeatedly while a condition is true.
Example: Display Disk Details Using awk
#!/bin/bash
while read -r line; do
echo "$line" | awk '{print "Disk: "$1, "Used: "$3}'
done < <(df -h)
This script reads disk details line by line and prints the disk name and memory used.
Vim Command
Vim is a powerful text editor that offers advanced features compared to Nano.
Advantages of Vim Over Nano
Modes: Vim offers normal, insert, and visual modes for efficient editing.
Customizability: Supports plugins and extensive configuration.
Performance: Faster for large files.
Basic Usage of Vim
Open a file:
vim filename
Insert mode: Press
i
to edit.Save and exit: Press
Esc
, then:wq
.Exit without saving: Press
Esc
, then:q!
.
The cut
Command
The cut
command extracts specific parts of a file or output.
Example in Vim Command
Suppose we have a file data.txt
:
Name Age
Alice 25
Bob 30
Using cut
to extract the first column:
cut -d' ' -f1 data.txt
Output:
Name
Alice
Bob
Disk Space Warning Script
Here’s a script that monitors disk space and warns when usage exceeds 90%:
#!/bin/bash
# Check disk space and send a warning if usage exceeds 90%
while read -r line; do
usage=$(echo "$line" | awk '{print $5}' | sed 's/%//')
if [ "$usage" -gt 90 ]; then
disk=$(echo "$line" | awk '{print $1}')
echo "Warning: $disk is ${usage}% full!"
fi
done < <(df -h | grep '^/dev')
Explanation:
Read disk details line by line: Use
df -h
output filtered for mounted devices.Extract usage percentage: Use
awk
to get the fifth column and remove%
usingsed
.Check if usage exceeds 90%: Compare the value and print a warning.
This part of the blog focuses on essential commands and techniques, enabling DevOps engineers to manage memory, monitor disk space, and efficiently edit configuration files. Experiment with these commands to master them!