Kritim Yantra
May 04, 2025
Imagine waking up every morning, opening your laptop, and manually doing the same boring tasks:
Now, what if I told you that your computer could do all this for you β automatically?
Welcome to the magical world of shell scripting.
A shell script is a file that contains a series of commands β just like the ones you type in your terminal. The shell reads this file line-by-line and executes the commands in order.
Itβs like giving your computer a to-do list, and it follows it exactly.
nano
, vim
, or even VS Code)No fancy setup. No installations. Just your OS and some curiosity.
Letβs make your computer greet you like a movie AI assistant:
#!/bin/bash
echo "Good morning, $(whoami)!"
echo "Today is $(date +%A), the time is $(date +%r)."
echo "Hereβs your system uptime: "
uptime
Save this as morning.sh
, then run:
chmod +x morning.sh
./morning.sh
π Boom! Youβve written your first shell script.
Too many junk files? Automate cleanup!
#!/bin/bash
echo "Cleaning /tmp directory..."
rm -rf /tmp/*
echo "Done!"
Add it to a cron job, and your PC will clean itself every day.
Letβs back up your important folder:
#!/bin/bash
source="/home/yourname/projects"
destination="/home/yourname/backups"
date=$(date +%F)
filename="backup-$date.tar.gz"
tar -czf $destination/$filename $source
echo "Backup completed: $filename"
Run this every evening and never worry about losing code.
name="Alice"
echo "Hello, $name"
if [ -f myfile.txt ]; then
echo "File exists"
else
echo "File not found"
fi
for file in *.log
do
echo "Found log file: $file"
done
greet() {
echo "Hi $1, welcome!"
}
greet "Bob"
#!/bin/bash
ping -c 1 google.com &> /dev/null
if [ $? -eq 0 ]; then
echo "Internet is up!"
else
echo "Internet is down!"
fi
#!/bin/bash
echo "CPU Load:"
top -bn1 | grep "load average"
echo "Memory Usage:"
free -h
.txt
Files to .bak
#!/bin/bash
for file in *.txt
do
mv "$file" "${file%.txt}.bak"
done
sudo
echo
before a destructive command like rm
to previewβ
Saves time
β
Automates boring tasks
β
Works on almost every Unix/Linux machine
β
Helps in CI/CD, DevOps, and system admin roles
β
Makes you feel like a tech wizard π§
Now that you know the basics, try these:
cron
bash
scripting tutorials and challengesOpen crontab:
crontab -e
Add this line to run a backup every day at 10 PM:
0 22 * * * /home/yourname/backup.sh
Your script will now run automatically at that time. How cool is that?
Shell scripting is like giving your computer a mini-program β it listens and does exactly what you say. You donβt need to be a Linux expert to get started. Start small, play around, break things (and fix them), and soon, you'll be automating like a DevOps engineer.
So go ahead β write your first script today, and let the automation begin.
No comments yet. Be the first to comment!
Please log in to post a comment:
Sign in with Google