03 Linux Commands - gannurohith/devops-interview-wiki GitHub Wiki

📁 03 - Linux Commands (Basic to Intermediate Q&A)

  1. How do you find your current working directory in Linux? pwd – prints the present working directory.

  2. How do you list all files in a directory, including hidden ones? ls -la

  3. How do you copy files or directories? cp file1.txt /dest/ or cp -r folder1/ /dest/

  4. What is the command to move or rename a file? mv oldname.txt newname.txt or mv file.txt /destination/

  5. How do you delete files and folders in Linux? rm file.txt, rm -r folder/

  6. How do you create a new empty file? touch filename.txt

  7. How do you create a directory? mkdir foldername

  8. How do you view the contents of a file? cat file.txt, less file.txt, or more file.txt

  9. How do you search for a string inside files? grep 'search_term' *.txt or recursively: grep -r 'term' /path/

  10. How do you count lines, words, or characters in a file? wc -l, wc -w, wc -c

  11. How do you check disk usage per directory? du -sh *

  12. How do you find running processes? ps aux or top or htop

  13. How do you kill a process by PID? kill <PID> or force: kill -9 <PID>

  14. How do you check open ports? netstat -tuln or ss -tuln

  15. How do you change file permissions? chmod 755 file.sh

  16. How do you change ownership of a file? chown user:group file.txt

  17. How do you switch to another user? su - username or sudo -i

  18. How do you schedule tasks in Linux? Use crontab -e, example: 0 5 * * * /path/script.sh

  19. How do you view network configuration? ip a or ifconfig

  20. How do you see system resource usage? top, htop, free -m, df -h

  21. How do you redirect output to a file? command > file.txt (overwrite) or >> (append)

  22. How do you pipe the output of one command to another? command1 | command2

  23. How do you compress and extract files?

  • Tar: tar -czf file.tar.gz folder/
  • Extract: tar -xzf file.tar.gz
  1. How do you find the path of a command? which command or type command

  2. How do you monitor real-time logs? tail -f /var/log/syslog

  3. How do you find files by name? find /path -name 'filename.txt'

  4. How do you get help for a command? man command or command --help

  5. How do you extract specific columns from a file? Use cut or awk: cut -d':' -f1 /etc/passwd

  6. How do you run a command as root? Prefix with sudo: sudo apt update

  7. How do you check system uptime? uptime or who -b


03. Linux Commands (Q&A)

  1. How do you find the top 5 memory-consuming processes on a Linux server? Answer: Use ps aux --sort=-%mem | head -n 6. It sorts by memory usage and shows the top 5 (first line is header).

  2. How do you check for disk space usage per directory? Answer: Use du -sh * or du -h --max-depth=1 inside a directory to see usage.

  3. What's the difference between hard links and soft links? Answer: Hard links point to the same inode; removing the original doesn’t delete the file. Soft links (symbolic) point to a path and can break if the target is removed.

  4. How do you find which process is listening on port 8080? Answer: Use sudo lsof -i :8080 or sudo netstat -tulpn | grep 8080.

  5. How do you monitor CPU, memory, disk I/O in real-time? Answer: Use tools like top, htop, iotop, or vmstat.

  6. How do you analyze system logs for login attempts? Answer: Use last, lastb, and grep on /var/log/auth.log or /var/log/secure.

  7. How do you limit CPU usage for a process? Answer: Use cpulimit or nice/renice for scheduling priorities.

  8. What’s the difference between cron, anacron, and systemd-timers? Answer: cron runs tasks at fixed times but needs system uptime. anacron runs missed tasks after boot. systemd-timers are flexible and integrated with systemd.

  9. How do you kill all processes by name? Answer: pkill <process-name> or killall <name>.

  10. Explain how top, htop, iotop, and nmon differ. Answer: top is basic. htop adds interactivity. iotop monitors disk I/O. nmon is a performance monitoring tool with CPU, disk, memory, and network stats.

  11. Find all files larger than 100MB in /var. Answer: find /var -type f -size +100M -exec ls -lh {} \;

  12. Compress and archive /etc and schedule a weekly cronjob. Answer: Use tar -czf etc-backup.tar.gz /etc. Add cron job: 0 2 * * 0 /path/to/script.sh

  13. What is a zombie process? How do you identify and clean them? Answer: Zombie processes have completed execution but not cleaned by parent. Use ps aux | grep Z. Only cleaning parent or reboot clears them.

  14. How do you recursively change permissions for a directory tree? Answer: Use chmod -R <mode> <dir>. Be cautious—ensure correct depth and scope.

  15. Explain strace and its usage in debugging a hanging script. Answer: strace traces system calls. Run strace -p <PID> to analyze calls. Helpful in detecting file access, permission, or syscall issues.

  16. Use awk, sed, and grep to filter Apache logs for 404s. Answer: grep " 404 " access.log | awk '{print $1}' | sort | uniq -c | sort -nr

  17. How do you mount an NFS share? Answer: mount -t nfs <server>:/path /mnt. Make persistent by editing /etc/fstab.

  18. What’s the difference between /etc/fstab and /etc/mtab? Answer: /etc/fstab defines static mounts; /etc/mtab shows active mounts.

  19. How do you view user login history? Answer: Use last to view past logins, and who to view current logins.

  20. How do you configure network interfaces in Linux? Answer: Use nmcli, ifconfig (deprecated), or edit /etc/network/interfaces or /etc/netplan/*.yaml (Ubuntu).

  21. What is /proc and how is it useful? Answer: Virtual filesystem exposing kernel and process internals. Use it to inspect process status, memory, CPU info, etc.

  22. How do you detect a memory leak in a long-running process? Answer: Use top, smem, or pmap. Track memory growth over time. Use valgrind for C/C++ binaries.

  23. Use find with exec to delete files older than 7 days. Answer: find /path -type f -mtime +7 -exec rm {} \;

  24. Check if a file system is near inode exhaustion. Answer: df -i. If IUse% is close to 100%, inode exhaustion risk is high.

  25. What’s the difference between su, sudo, and doas? Answer: su switches user; sudo runs single commands as another user; doas is a simpler, more secure alternative to sudo.

⚠️ **GitHub.com Fallback** ⚠️