Master Linux Command‑Line Essentials for Interview Success
This comprehensive guide covers the most frequently asked Linux command‑line topics for interviews, including file and directory manipulation, process management, system monitoring, networking, permission handling, and practical scenario‑based exercises, all illustrated with clear examples and command syntax.
File and Directory Operations
Basic file and directory commands are essential in Linux interviews, covering listing, creating, deleting, and moving files.
1. ls – List directory contents
Displays files and subdirectories in the current or specified directory.
Common options: -l: long format with permissions, owner, size, etc. -a: include hidden files (starting with .). -h: human‑readable sizes.
ls -lah /var/log2. cd – Change directory
Switches to the specified directory.
Common usage:
cd /path/to/dir cd ..– parent directory cd ~ – home directory cd - – previous directory
cd /etc/nginx
cd ..3. pwd – Print working directory
Shows the absolute path of the current directory.
pwd4. mkdir – Create directory
Creates a new directory.
Common option: -p creates parent directories as needed.
mkdir -p /tmp/test/dir1/dir25. rm – Remove files or directories
Deletes specified files or directories.
Common options: -r: recursive delete -f: force, no prompt
rm -rf /tmp/test6. cp – Copy files or directories
Copies files or directories to a target location.
Common options: -r: recursive copy -p: preserve attributes
cp -r /etc/nginx /backup/nginx7. mv – Move or rename files/directories
Moves or renames a file or directory.
Common option: -i prompts before overwriting.
mv file1.txt /tmp/file2.txt8. find – Search for files
Searches for files or directories matching criteria.
Common options: -name: pattern matching -type f|d: file or directory -exec: execute a command on results
find /var/log -name "*.log" -type f -mtime +7 -exec ls -lh {} \;9. touch – Create empty file or update timestamp
touch test.txtFile Content Operations
10. cat – View file contents
Outputs file content to standard output.
Common option: -n shows line numbers.
cat -n /etc/passwd11. less / more – Paginated view
Displays file content page by page, useful for large files.
Key commands: / search, q quit.
less /var/log/syslog12. head / tail – View beginning or end of a file
headshows the start; tail shows the end.
Common option: -n number of lines.
tail -f /var/log/nginx/access.log13. grep – Search file contents
Searches for matching strings.
Common options: -i: ignore case -r: recursive -n: show line numbers -v: invert match
grep -r "error" /var/log14. awk – Text processing
Processes columns, e.g., extracting fields.
awk '{print $1,$3}' /etc/passwd15. sed – Stream editor
Performs text substitution, deletion, insertion.
sed 's/old/new/g' file.txtProcess Management
16. ps – View processes
Shows current processes.
Common options: aux (all users), -ef (full format).
ps aux | grep nginx17. top / htop – Real‑time process monitor
Displays live system processes; htop offers a friendlier UI.
Key keys: q quit, k kill process.
htop18. kill / killall – Terminate processes
Kill by PID ( kill) or by name ( killall).
Common signals: -9 (SIGKILL) – force kill -15 (SIGTERM) – graceful termination
kill -9 12345
killall nginx19. jobs / fg / bg – Manage background jobs
List, bring to foreground, or send to background.
sleep 100 &
jobs
fg %1System Monitoring
20. df – Disk usage
Shows filesystem space.
Option: -h human‑readable.
df -h21. du – Directory/file size
Summarizes disk usage for files/directories.
Option: -sh total size, human‑readable.
du -sh /var/log/*22. free – Memory usage
Displays RAM and swap usage.
Option: -h human‑readable.
free -h23. uptime – System uptime and load
uptimeNetwork Related
24. ping – Test connectivity
ping -c 4 baidu.com25. netstat / ss – Network status
Shows connections, listening ports, etc.
Common option (ss): -tuln display TCP/UDP listening ports.
ss -tuln26. curl / wget – Download/request resources
curl -O https://example.com/file.txt27. ifconfig / ip – Configure network interfaces
ip addrPermission Management
28. chmod – Change file permissions
Modify access rights.
Numeric mode: e.g., 755 (rwxr-xr-x).
Symbolic mode: e.g., u+x (add execute for owner).
chmod 755 script.sh29. chown – Change file owner
Change owner and group.
Option: -R recursive.
chown -R user:group /var/www30. sudo – Execute with elevated privileges
sudo apt updateOther Useful Commands
31. tar – Archive and compress
Common options: -c create -x extract -z gzip -f specify file
tar -czvf backup.tar.gz /var/www32. crontab – Schedule tasks
Common options: -e edit, -l list. crontab -e Add a daily 2 AM backup:
0 2 * * * /backup.sh33. man – Command manual
man lsScenario‑Based Interview Questions
Scenario 1 – Find and clean old log files
Find .log files in /var/log older than 7 days, list sizes, and safely delete.
find /var/log -type f -name "*.log" -mtime +7 -exec ls -lh {} \;Save list for review:
find /var/log -type f -name "*.log" -mtime +7 > /tmp/old_logs.txt
cat /tmp/old_logs.txtMove to backup before deletion:
mkdir -p /tmp/log_backup
find /var/log -type f -name "*.log" -mtime +7 -exec mv {} /tmp/log_backup/ \;
rm -rf /tmp/log_backup/*Scenario 2 – Identify and kill high‑CPU process
Use top or htop to locate the highest CPU consumer, note PID, then:
ps -p <PID> -o pid,ppid,cmd,%cpu
kill -15 <PID> # graceful
kill -9 <PID> # force if neededScenario 3 – Batch modify configuration files
Replace listen 80 with listen 8080 in all .conf files under /etc/nginx/conf.d:
cp -r /etc/nginx/conf.d /etc/nginx/conf.d.bak
find /etc/nginx/conf.d -type f -name "*.conf" -exec sed -i 's/listen 80/listen 8080/g' {} \;Scenario 4 – Real‑time log monitoring
tail -f /var/log/nginx/error.log | grep --line-buffered "error"Scenario 5 – Check and free disk space
df -h
du -sh /var/* | sort -hr | head -n 5
cp /var/log/app.log /backup/app.log.bak
> /var/log/app.logScenario 6 – Configure daily backup via cron
# /scripts/backup.sh
#!/bin/bash
DATE=$(date +%Y%m%d)
tar -czf /backup/www_${DATE}.tar.gz /var/www
find /backup -name "www_*.tar.gz" -mtime +30 -delete
chmod +x /scripts/backup.sh
crontab -e # add: 0 2 * * * /scripts/backup.shScenario 7 – Diagnose network connectivity
ping -c 4 baidu.com
nslookup baidu.com
ip addr
ip route
cat /etc/resolv.conf
# optionally add DNS
echo "nameserver 8.8.8.8" | sudo tee -a /etc/resolv.confScenario 8 – Set directory permissions
chown -R www-data:www-data /var/www/html
find /var/www/html -type d -exec chmod 755 {} \;
find /var/www/html -type f -exec chmod 644 {} \;Scenario 9 – Release occupied port
ss -tuln | grep :80 # or lsof -i :80
kill -15 12345 # graceful
kill -9 12345 # forceScenario 10 – Extract recent IPs and URLs from Nginx access log
tail -n 100 /var/log/nginx/access.log | awk '{print $1, $7}' > /tmp/access.txtThese examples provide ready‑to‑use commands and best‑practice explanations to help you excel in Linux‑related interview questions.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
IT Services Circle
Delivering cutting-edge internet insights and practical learning resources. We're a passionate and principled IT media platform.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
