Shell Scripting & Linux Interview Cheatsheet

๐น 1. Commonly Used Shell Commands
bashCopyEditls, cp, mv, mkdir, touch, vim, grep, find, df -h
๐น 2. List All Processes
bashCopyEditps -ef
โ Only Process IDs:
bashCopyEditps -ef | awk '{print $2}'
๐น 3. Extract Errors from Remote Log
bashCopyEditcurl <log_url> | grep -i "error"
๐ Replace <log_url> with actual log file URL (S3, GitHub, etc.)
๐น 4. Print Numbers Divisible by 3 & 5 (but not 15)
bashCopyEditfor i in {1..100}; do
if (( i % 3 == 0 && i % 5 == 0 && i % 15 != 0 )); then
echo $i
fi
done
๐น 5. Count โsโ in Mississippi
bashCopyEditecho "Mississippi" | grep -o "s" | wc -l
๐น 6. Debug a Shell Script
bashCopyEditset -x
๐น 7. Crontab (Schedule a Task)
bashCopyEdit0 0 * * * /path/to/script.sh
๐ Runs daily at midnight.
๐น 8. Open a File in Read-Only Mode
bashCopyEditvim -R test.txt
๐น 9. Difference Between Soft Link & Hard Link
In Linux, links are used to create references to files. There are two types: Soft (Symbolic) Links and Hard Links.
| Feature | Hard Link | Soft Link (Symbolic Link) |
| Points to | Actual data on disk (inode) | File name/path |
| File deletion | Data stays if original is deleted | Link breaks if original is deleted |
| Cross-filesystem | โ Cannot span across filesystems | โ Can link across filesystems |
| Directories | โ Cannot link directories (usually) | โ Can link directories |
| Command | ln source.txt hardlink.txt | ln -s source.txt softlink.txt |
๐ In summary:
Hard links are real clones of the original file (same inode).
Soft links are shortcuts โ useful but fragile if the source file is removed.๐น 10.
breakvscontinue
| Command | Behavior |
break | Exits the loop |
continue | Skips current iteration |
Example:
bashCopyEditif (( i % 15 == 0 )); then
continue
fi
๐น 11. Disadvantages of Shell Scripts
While shell scripts are powerful for automation and system-level tasks, they come with several limitations:
โ Errors are frequent and costly
- A single typo or wrong character can change the entire command behavior.
๐ข Slow execution speed
- Scripts are interpreted, not compiled โ leading to slower performance.
๐ Language syntax bugs or limitations
- Minimal error handling, no robust debugger, and loosely typed variables can cause issues.
๐ Not ideal for large/complex tasks
- Shell is best for small tasks. For complex logic or UI, use a more structured language.
๐ Minimal data structures
- Unlike Python or JavaScript, shell offers only basic variables and arrays.
โ๏ธ New process for each command
- Every shell command spawns a new process, which adds overhead and affects performance.
โ
Use shell for: automation, quick scripts, file management.
โ Avoid for: complex business logic, large-scale applications, data-intensive tasks.
๐น 12. Types of Loops
| Loop | Use Case |
for | Known number of iterations |
while | Loop while condition true |
until | Loop until condition true |
๐น 13. Is Bash Dynamically Typed?
โ Yes. No need to declare variable types โ just assign and go.
๐น 14. Network Troubleshooting Utilities
bashCopyEdittraceroute google.com
tracepath google.com
๐ Used to trace the route and diagnose network issues.
๐น 15. Sort Names in a File
bashCopyEditsort names.txt
๐น 16. Manage Huge Log Files (with logrotate)
Example Config:
bashCopyEdit/var/log/myapp.log {
daily
rotate 7
compress
missingok
}
๐ฆ logrotate compresses and rotates old logs automatically.
โ
Pro Tip: Practice commands and scripts regularly. Mastering small things like grep, awk, and conditionals will make you stand out in interviews.



