Chapter 4: Linux Advanced Commands
Practice Questions and Lab Exercises
- Write a command to count how many times
404appears in an Nginx access log. - Find all
.conffiles under/etcthat were modified in the last 2 days. - Using
awk, print the 1st and 9th columns (IP and status code) of an access log. - Explain
>,>>,2>&1and the pipe symbol with examples. - What is the difference between
service nginx restartandservice nginx reload? Betweenservice nginx startandsystemctl enable nginx? - Which command shows the ports on which a server is listening? How do you know whether an app is exposed to the internet?
- Write a cron entry that runs
/home/ubuntu/backup.shevery Sunday at 11:00 PM. - Lab: Write a script
backup.shthat creates~/backups/web-<date>.tar.gzof/var/www/htmland deletes backups older than 7 days. - Lab: Use
scpto upload a folder to your EC2 instance andrsyncto sync only changed files.
Quick Revision
grep(search text),find(search files),awk(columns),sed(replace).- Pipes, redirection
> >> 2> 2>&1,teefor writing root-owned files, here-docs for multi-line files. tar -czvfcreate,tar -xzvfextract.sudo service <name> start|stop|restart|reload|status; boot-time withsudo systemctl enable <name>; logs withjournalctl -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.