Scenario-Based Unix Shell Scripting Interview Questions and Answers (2025)
Top Scenario-Based Unix Shell Scripting Interview Questions and Answers (2025)
1. Scenario: Find and archive all
.log files older than 7 days Question:
How would you write a shell script to find all
Answer:
Queries: shell script to archive log files, find old files in Unix, automate log rotation in bash.
2. Scenario: Monitor disk space usage and send an alert
Question:
Write a Unix shell script to monitor disk usage and email an alert if usage exceeds 90%.
Answer:
Queries: shell script for disk monitoring, Unix alert on high disk usage, automate server health check.
How would you write a shell script to find all
.log files in /var/logs older than 7 days and archive them into a .tar.gz file?Answer:
#!/bin/bashfind /var/logs -name "*.log" -type f -mtime +7 > old_logs.txttar -czf archived_logs_$(date +%F).tar.gz -T old_logs.txtQueries: shell script to archive log files, find old files in Unix, automate log rotation in bash.
2. Scenario: Monitor disk space usage and send an alert
Question:
Write a Unix shell script to monitor disk usage and email an alert if usage exceeds 90%.
Answer:
#!/bin/bashTHRESHOLD=90USAGE=$(df / | grep / | awk '{print $5}' | sed 's/%//') if [ "$USAGE" -ge "$THRESHOLD" ]; then echo "Disk usage is critically high: ${USAGE}%" | mail -s "Disk Space Alert" admin@example.comfiQueries: shell script for disk monitoring, Unix alert on high disk usage, automate server health check.
3. Scenario: Remove duplicate lines from a file
Question:
How would you write a shell script that removes duplicate lines from a file while preserving the order?
Answer:
#!/bin/bashawk '!seen[$0]++' input.txt > output.txtQueries: remove duplicates in Unix file, awk to clean data, deduplicate records shell script.
4. Scenario: Rename all files with a specific extension
Question:
Write a shell script to rename all
.txt files in a directory by adding today’s date as a
prefix.Answer:
#!/bin/bashfor file in *.txt; do mv "$file" "$(date +%F)_$file"doneQueries: bulk rename files bash, rename script with date, Unix shell automation tasks.
5. Scenario: Parse and extract data from a CSV file
Question:
Create a script that extracts the second column from a CSV file.
Answer:
#!/bin/bashcut -d ',' -f2 data.csv > column2.txtQueries: extract column from CSV Unix, bash script parse CSV, data extraction shell script.
6. Scenario: Monitor a process and restart if it fails
Question:
How can you monitor a critical process and automatically restart it if it stops?
Answer:
#!/bin/bashPROCESS="apache2" if ! pgrep $PROCESS > /dev/nullthen systemctl start $PROCESS echo "$PROCESS restarted on $(date)" >> process_monitor.logfiQueries: auto-restart Unix process, shell script process monitoring, Linux service watch script.
7. Scenario: Compare two files and find differences
Question:
Write a shell script to compare two files and list lines that are different.
Answer:
#!/bin/bashdiff file1.txt file2.txt > differences.txtQueries: compare files bash, find differences in Unix, diff command script example.
8. Scenario: Schedule a script to run every 15 minutes
Question:
How would you schedule your shell script to execute every 15 minutes?
Answer:
Use
crontab -e and add:*/15 * * * * /path/to/your_script.shQueries: schedule script in crontab, run every 15 minutes cron, automate shell job timing.
9. Scenario: Check if a file is empty and handle accordingly
Question:
Write a script to check if a file is empty and print a message.
Answer:
#!/bin/bashif [ ! -s filename.txt ]; then echo "File is empty"else echo "File has content"fiQueries: check empty file Unix, shell script file validation, file condition bash.
10. Scenario: Count occurrences of a word in a file
Question:
How would you count the number of times a word appears in a log file?
Answer:
#!/bin/bashgrep -o "ERROR" logfile.txt | wc -lQueries: count word in Unix file, grep count bash, search logs script.
Bonus: Tips to Tackle Shell Scripting Scenario Questions
Think in terms of automation and system tasks.
Use tools like
awk, sed, grep, cut, find, xargs.Practice combining commands with pipes (
|) and redirection (>, >>).