Shell Scripting & Linux Interview Cheatsheet

AnjuAnju
4 min read

๐Ÿ”น 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

In Linux, links are used to create references to files. There are two types: Soft (Symbolic) Links and Hard Links.

FeatureHard LinkSoft Link (Symbolic Link)
Points toActual data on disk (inode)File name/path
File deletionData stays if original is deletedLink breaks if original is deleted
Cross-filesystemโŒ Cannot span across filesystemsโœ… Can link across filesystems
DirectoriesโŒ Cannot link directories (usually)โœ… Can link directories
Commandln source.txt hardlink.txtln -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. break vs continue

CommandBehavior
breakExits the loop
continueSkips 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:

    1. โŒ Errors are frequent and costly

      • A single typo or wrong character can change the entire command behavior.
    2. ๐Ÿข Slow execution speed

      • Scripts are interpreted, not compiled โ€” leading to slower performance.
    3. ๐Ÿž Language syntax bugs or limitations

      • Minimal error handling, no robust debugger, and loosely typed variables can cause issues.
    4. ๐Ÿ”„ Not ideal for large/complex tasks

      • Shell is best for small tasks. For complex logic or UI, use a more structured language.
    5. ๐Ÿ“‰ Minimal data structures

      • Unlike Python or JavaScript, shell offers only basic variables and arrays.
    6. โš™๏ธ 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

LoopUse Case
forKnown number of iterations
whileLoop while condition true
untilLoop 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.

0
Subscribe to my newsletter

Read articles from Anju directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Anju
Anju