Must-Know Linux Questions for Stress-Free Job Preparation
Watch full video
Question 1:
Your boss asks you to create 100 directories named project1
, project2
, …, up to project100
in a specific directory /home/user/projects
. How would you achieve this in a single command?
Answer:
mkdir -p /home/user/projects/project{1..100}
Explanation:
- The
mkdir
command creates directories. - The
-p
flag ensures the parent directory exists. {1..100}
generates numbers from 1 to 100 automatically.
Question 2:
Your boss wants you to find and list all .log
files larger than 50MB in the /var/log
directory. How would you do this?
Answer:
find /var/log -type f -name "*.log" -size +50M
Explanation:
find /var/log
: Search within the/var/log
directory.-type f
: Look for files only.-name "*.log"
: Match files with the.log
extension.-size +50M
: Find files larger than 50MB.
Question 3:
Your boss asks you to archive and compress the entire /var/www/html
directory into a tarball named website_backup.tar.gz
. How would you do this?
Answer:
tar -czvf website_backup.tar.gz /var/www/html
Explanation:
tar
: Command to create archives.-c
: Create an archive.-z
: Compress with gzip.-v
: Verbose mode to show progress.-f
: Specify the file name./var/www/html
: Directory to be archived.
Question 4:
Your boss wants you to change the permissions of all files in /home/user/data
so that only the owner has read and write permissions. How would you do it?
Answer:
chmod -R 600 /home/user/data
Explanation:
chmod
: Change file permissions.-R
: Apply changes recursively to all files and directories.600
: Permissions where the owner has read and write permissions only.
Question 5:
Your boss asks you to write a one-liner to display the 10 most memory-consuming processes on the system. How would you do this?
Answer:
ps aux --sort=-%mem | head -n 11
Explanation:
ps aux
: Displays detailed information about running processes.--sort=-%mem
: Sort processes by memory usage in descending order.head -n 11
: Show the top 10 processes plus the header row.
Question 6
Your boss asks you to create 100 empty files named file1.txt
, file2.txt
, …, up to file100.txt
inside the /home/user/docs
directory in a single command. How would you do this?
Answer:
touch /home/user/docs/file{1..100}.txt
Explanation:
touch
: Command to create empty files./home/user/docs/file{1..100}.txt
: Generates filenames fromfile1.txt
tofile100.txt
.
Question 7
Your boss wants you to find and delete all .tmp
files older than 30 days in /var/tmp
. How would you do it?
Answer:
find /var/tmp -type f -name "*.tmp" -mtime +30 -exec rm {} \;
Explanation:
-type f
: Search for files.-name "*.tmp"
: Match files ending with.tmp
.-mtime +30
: Files modified more than 30 days ago.-exec rm {} \;
: Delete the matched files.
Question 8
Your boss asks you to find and count the number of lines containing the word ERROR
in a log file /var/log/syslog
. How would you do this?
Answer:
grep -c "ERROR" /var/log/syslog
Explanation:
grep
: Searches for patterns in files.-c
: Counts matching lines."ERROR"
: The pattern to search for.
Question 9
Your boss asks you to continuously monitor the disk usage of /dev/sda1
every 5 seconds and output the result to the terminal. How would you do this?
Answer:
watch -n 5 df -h /dev/sda1
Explanation:
watch -n 5
: Run a command every 5 seconds.df -h
: Show disk space usage in human-readable format./dev/sda1
: Disk partition to monitor.
Question 10
Your boss asks you to replace all occurrences of localhost
with 127.0.0.1
in the file /etc/hosts
. How would you achieve this using a single command?
Answer:
sed -i 's/localhost/127.0.0.1/g' /etc/hosts
Explanation:
sed
: Stream editor for text manipulation.-i
: Edit the file in place.'s/localhost/127.0.0.1/g'
: Substitutelocalhost
with127.0.0.1
.
Question 11
Your boss asks you to check the number of active connections to a web server running on port 80. How would you do this?
Answer:
netstat -an | grep ':80' | grep ESTABLISHED | wc -l
Explanation:
netstat -an
: Show network connections.grep ':80'
: Filter connections on port 80.grep ESTABLISHED
: Find active connections.wc -l
: Count the number of lines.
Question 12
Your boss wants you to schedule a cron job that backs up the /home/user/docs
directory every day at midnight. How would you configure this?
Answer:
- Open the crontab editor:bashCopyEdit
crontab -e
- Add the following line to schedule the job
0 0 * * * tar -czf /home/user/backup_$(date +\%F).tar.gz /home/user/docs
Explanation:
0 0 * * *
: Schedule the job at midnight every day.tar -czf
: Create a compressed tarball.$(date +%F)
: Add the current date to the backup file name.
Question 13
Your boss asks you to display the top 5 largest files in the /var
directory. How would you do this?
Answer:
du -ah /var | sort -rh | head -n 5
Explanation:
du -ah /var
: Show sizes of all files and directories.sort -rh
: Sort in reverse order by size.head -n 5
: Display the top 5 results.
Question 14
Your boss wants you to check whether the service nginx
is running and restart it if it isn’t. How would you do this in a single command?
Answer:
systemctl is-active --quiet nginx || systemctl restart nginx
Explanation:
systemctl is-active --quiet nginx
: Check if thenginx
service is active without producing output.|| systemctl restart nginx
: Restart the service if it is not active.
Question 15
Your boss wants you to archive and transfer the /home/user/data
directory to a remote server 192.168.1.100
using scp
. How would you do this?
Answer:
tar -czf - /home/user/data | ssh user@192.168.1.100 "cat > data_backup.tar.gz"
Explanation:
tar -czf -
: Create an archive and write it to standard output.ssh user@192.168.1.100
: Connect to the remote server."cat > data_backup.tar.gz"
: Save the archive on the remote server.
Checkout the courses Python Automation for Linux admins