Advanced Bash techniques for automation, optimization, and security
Advanced awk, sed, and grep
Awk, sed, and grep are essential tools for text processing, and their advanced features allow for complex data manipulation with minimal overhead. These utilities are indispensable for parsing logs, extracting configuration details, and automating repetitive tasks.
Consider a scenario where you need to analyze a web server log (/var/log/nginx/access.log
) to identify the most frequent IP addresses accessing the server:
awk '{print $1}' /var/log/nginx/access.log | sort | uniq -c | sort -nr |head -10
In this command, awk
extracts the first field (the IP address), sort
organizes the addresses, and uniq -c
counts occurrences. The final sort -nr
ranks the results numerically in descending order, and head
displays the top 10 IP addresses. This approach is both efficient and scalable, making it ideal for large logfiles.
Sed excels in stream editing, allowing you to modify text in-place without manual intervention. For example, you can replace all instances of http
with https
in a configuration file as follows:
sed -i 's/http/https/g' /etc/nginx/sites-available/default
The -i
flag applies changes directly to the file, and the g
flag ensures all occurrences on a line are replaced. This is particularly useful for bulk updates across multiple configuration files.
For targeted text searches, grep provides unmatched speed and precision. To extract only error lines from a system log while excluding debug messages, you can use:
grep -i "error" /var/log/syslog | grep -v "debug"
Here, the -i
flag makes the search case-insensitive and grep -v
excludes lines containing debug
. Combined with other utilities, grep becomes a versatile tool for data filtering and extraction.
Scheduling
Task scheduling is vital for automation, ensuring that jobs like backups, updates, or log rotations run at specified intervals. The cron utility has been a traditional choice for scheduling, whereas systemd timers offer enhanced flexibility in modern Linux distributions.
To schedule a daily backup using cron, edit the crontab file:
crontab -e
Add the following line to schedule a backup script (/usr/local/bin/backup.sh
) to run at 2:00am daily:
0 2 * * * /usr/local/bin/backup.sh
This format specifies the minute, hour, day of the month, month, and day of the week. You can verify scheduled jobs with:
crontab -l
Managing System Resources
Resource management is a cornerstone of system administration, ensuring optimal performance and quick resolution of issues. Commands like lsof
, ps
, and kill
enable effective monitoring and control over system resources.
lsof
(list open files) is invaluable for identifying processes using specific files or ports. For instance, to identify the process occupying port 80:
lsof -i :80
This command provides details about the process, including its PID, user, and associated files, which are critical for troubleshooting service conflicts.
The ps
command provides detailed information about running processes. To display processes in a tree format, showing parent-child relationships, use:
ps -e --forest
This view is particularly useful for understanding dependencies or investigating rogue processes. To monitor resource usage, combine ps
with sorting:
ps -eo pid,comm,%cpu,%mem --sort=-%cpu | head
This command lists processes by their CPU usage, making it easy to identify resource-hungry tasks. When processes become unresponsive, kill
offers a straightforward way to terminate them. To gracefully stop a process:
kill -15 <PID>
The -15
signal requests termination, allowing the process to clean up before exiting. If the process ignores this signal, force termination with -9
:
kill -9 <PID>
Combining these utilities into scripts allows for automated monitoring and intervention. For example, a script to restart a service if its memory usage exceeds a threshold might use ps
to detect the condition, followed by kill
and a systemctl restart
command.
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.

News
-
System76 Releases COSMIC Alpha 7
With scores of bug fixes and a really cool workspaces feature, COSMIC is looking to soon migrate from alpha to beta.
-
OpenMandriva Lx 6.0 Available for Installation
The latest release of OpenMandriva has arrived with a new kernel, an updated Plasma desktop, and a server edition.
-
TrueNAS 25.04 Arrives with Thousands of Changes
One of the most popular Linux-based NAS solutions has rolled out the latest edition, based on Ubuntu 25.04.
-
Fedora 42 Available with Two New Spins
The latest release from the Fedora Project includes the usual updates, a new kernel, an official KDE Plasma spin, and a new System76 spin.
-
So Long, ArcoLinux
The ArcoLinux distribution is the latest Linux distribution to shut down.
-
What Open Source Pros Look for in a Job Role
Learn what professionals in technical and non-technical roles say is most important when seeking a new position.
-
Asahi Linux Runs into Issues with M4 Support
Due to Apple Silicon changes, the Asahi Linux project is at odds with adding support for the M4 chips.
-
Plasma 6.3.4 Now Available
Although not a major release, Plasma 6.3.4 does fix some bugs and offer a subtle change for the Plasma sidebar.
-
Linux Kernel 6.15 First Release Candidate Now Available
Linux Torvalds has announced that the release candidate for the final release of the Linux 6.15 series is now available.
-
Akamai Will Host kernel.org
The organization dedicated to cloud-based solutions has agreed to host kernel.org to deliver long-term stability for the development team.