0% found this document useful (0 votes)
17 views

Script 1

Shell scripting
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Script 1

Shell scripting
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Linux Shell Scripting.

Follow – Krishan Bhatt

Linux Shell Script Examples

1. Hello World Script

#!/bin/bash

echo "Hello, World!"

2. Simple Backup Script

#!/bin/bash

tar -czf backup.tar.gz /path/to/directory

3. File Existence Check

#!/bin/bash

if [ -f /path/to/file ]; then

echo "File exists"

else

echo "File does not exist"

fi

4. Check for Root User

#!/bin/bash
if [ "$(id -u)" -ne 0 ]; then

echo "Run as root."

exit 1

fi

5. Print System Uptime

#!/bin/bash

uptime

6. Monitor Disk Usage

#!/bin/bash

df -h > disk_usage_report.txt

7. Rename Files in a Directory

#!/bin/bash

for file in *.jpg; do

mv "$file" "${file%.jpg}.png"

done

8. Ping Multiple Hosts

#!/bin/bash

for ip in 192.168.1.1 8.8.8.8; do

ping -c 4 $ip

done
9. Create Multiple Directories

#!/bin/bash

for dir in dir1 dir2 dir3; do

mkdir $dir

done

10. Display CPU Information

#!/bin/bash

cat /proc/cpuinfo

11. Send an Email Notification

#!/bin/bash

echo "Backup completed" | mail -s "Backup Status" [email protected]

12. Check Service Status

#!/bin/bash

service apache2 status

13. Loop Through a List of Files

#!/bin/bash

for file in *.txt; do

echo "Processing $file"


done

14. Delete Old Files

#!/bin/bash

find /path/to/directory -type f -mtime +30 -exec rm {} \;

15. Check Network Connectivity

#!/bin/bash

ping -c 4 google.com

16. Add Users from a File

#!/bin/bash

while IFS= read -r user; do

useradd "$user"

done < users.txt

17. Find Large Files

#!/bin/bash

find / -type f -size +1G

18. Create a File with Timestamp

#!/bin/bash

touch "file_$(date +%Y%m%d).txt"


19. Monitor Memory Usage

#!/bin/bash

free -m

20. Check OS Version

#!/bin/bash

cat /etc/os-release

21. User Login Notification

#!/bin/bash

who | grep "$USER"

22. Simple Calculator

#!/bin/bash

echo "Enter two numbers: "

read a b

echo "Sum: $((a + b))"

23. List Active Processes

#!/bin/bash

ps aux
24. Kill a Process by Name

#!/bin/bash

pkill -f process_name

25. Monitor Specific Process

#!/bin/bash

while true; do

ps aux | grep nginx

sleep 5

done

26. Compress Files

#!/bin/bash

gzip *.log

27. Copy Files with Progress

#!/bin/bash

rsync -avh --progress /source/ /destination/

28. Backup MySQL Database

#!/bin/bash

mysqldump -u root -p database_name > backup.sql


29. Auto Reboot System at Midnight

#!/bin/bash

echo "0 0 * * * /sbin/reboot" | crontab -

30. Download a File

#!/bin/bash

wget http://example.com/file.zip

31. Monitor User Logins

#!/bin/bash

last

32. Create an Archive of Files

#!/bin/bash

tar -cvf archive.tar /path/to/files

33. Batch File Rename

#!/bin/bash

for file in *.txt; do

mv "$file" "new_${file}"

done

34. Check System Load


#!/bin/bash

uptime

35. Monitor CPU Load in Real-Time

#!/bin/bash

top

36. Create a Swap File

#!/bin/bash

dd if=/dev/zero of=/swapfile bs=1M count=1024

mkswap /swapfile

swapon /swapfile

37. List Installed Packages (Debian-based)

#!/bin/bash

dpkg -l

38. Auto Update Packages (Debian)

#!/bin/bash

apt-get update && apt-get upgrade -y

39. Check if File is Empty


#!/bin/bash

if [ ! -s /path/to/file ]; then

echo "File is empty"

fi

40. Check RAM Usage

#!/bin/bash

free -h

41. Find All Running Docker Containers

#!/bin/bash

docker ps

42. Display Disk Space Usage

#!/bin/bash

df -h

43. Monitor Network Interfaces

#!/bin/bash

ifconfig

44. Create a New User

#!/bin/bash
useradd newuser

45. Generate SSH Keys

#!/bin/bash

ssh-keygen -t rsa -b 2048

46. Automate Database Backup

#!/bin/bash

mysqldump -u user -p password dbname > backup.sql

47. Schedule Cron Job for Backup

#!/bin/bash

echo "0 2 * * * /path/to/backup.sh" | crontab -

48. Watch File Changes in Real-Time

#!/bin/bash

tail -f /var/log/syslog

49. Simple Countdown Timer

#!/bin/bash

for i in {10..1}; do

echo $i

sleep 1
done

echo "Time's up!"

50. Display Current User

#!/bin/bash

whoami

You might also like