Ravindra BagaleCourses & study guides

Chapter 4: Linux Advanced Commands

Practice Questions and Lab Exercises

  1. Write a command to count how many times 404 appears in an Nginx access log.
  2. Find all .conf files under /etc that were modified in the last 2 days.
  3. Using awk, print the 1st and 9th columns (IP and status code) of an access log.
  4. Explain >, >>, 2>&1 and the pipe symbol with examples.
  5. What is the difference between service nginx restart and service nginx reload? Between service nginx start and systemctl enable nginx?
  6. Which command shows the ports on which a server is listening? How do you know whether an app is exposed to the internet?
  7. Write a cron entry that runs /home/ubuntu/backup.sh every Sunday at 11:00 PM.
  8. Lab: Write a script backup.sh that creates ~/backups/web-<date>.tar.gz of /var/www/html and deletes backups older than 7 days.
  9. Lab: Use scp to upload a folder to your EC2 instance and rsync to sync only changed files.

Quick Revision

  • grep (search text), find (search files), awk (columns), sed (replace).
  • Pipes, redirection > >> 2> 2>&1, tee for writing root-owned files, here-docs for multi-line files.
  • tar -czvf create, tar -xzvf extract.
  • sudo service <name> start|stop|restart|reload|status; boot-time with sudo systemctl enable <name>; logs with journalctl -u <name> -f.
  • Network: ip a, ss -tlnp, curl -I, dig, nc -zv. Disk: df -h, du -sh, lsblk -f, blkid, /etc/fstab.
  • crontab -e (5 fields). export VAR=value, ~/.bashrc. ssh -i, scp -r, rsync -avz.
  • Scripts: #!/bin/bash, chmod +x, variables, if, for, functions.