03 Linux Commands - gannurohith/devops-interview-wiki GitHub Wiki
-
How do you find your current working directory in Linux?
pwd
– prints the present working directory. -
How do you list all files in a directory, including hidden ones?
ls -la
-
How do you copy files or directories?
cp file1.txt /dest/
orcp -r folder1/ /dest/
-
What is the command to move or rename a file?
mv oldname.txt newname.txt
ormv file.txt /destination/
-
How do you delete files and folders in Linux?
rm file.txt
,rm -r folder/
-
How do you create a new empty file?
touch filename.txt
-
How do you create a directory?
mkdir foldername
-
How do you view the contents of a file?
cat file.txt
,less file.txt
, ormore file.txt
-
How do you search for a string inside files?
grep 'search_term' *.txt
or recursively:grep -r 'term' /path/
-
How do you count lines, words, or characters in a file?
wc -l
,wc -w
,wc -c
-
How do you check disk usage per directory?
du -sh *
-
How do you find running processes?
ps aux
ortop
orhtop
-
How do you kill a process by PID?
kill <PID>
or force:kill -9 <PID>
-
How do you check open ports?
netstat -tuln
orss -tuln
-
How do you change file permissions?
chmod 755 file.sh
-
How do you change ownership of a file?
chown user:group file.txt
-
How do you switch to another user?
su - username
orsudo -i
-
How do you schedule tasks in Linux? Use
crontab -e
, example:0 5 * * * /path/script.sh
-
How do you view network configuration?
ip a
orifconfig
-
How do you see system resource usage?
top
,htop
,free -m
,df -h
-
How do you redirect output to a file?
command > file.txt
(overwrite) or>>
(append) -
How do you pipe the output of one command to another?
command1 | command2
-
How do you compress and extract files?
- Tar:
tar -czf file.tar.gz folder/
- Extract:
tar -xzf file.tar.gz
-
How do you find the path of a command?
which command
ortype command
-
How do you monitor real-time logs?
tail -f /var/log/syslog
-
How do you find files by name?
find /path -name 'filename.txt'
-
How do you get help for a command?
man command
orcommand --help
-
How do you extract specific columns from a file? Use
cut
orawk
:cut -d':' -f1 /etc/passwd
-
How do you run a command as root? Prefix with
sudo
:sudo apt update
-
How do you check system uptime?
uptime
orwho -b
-
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). -
How do you check for disk space usage per directory? Answer: Use
du -sh *
ordu -h --max-depth=1
inside a directory to see usage. -
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.
-
How do you find which process is listening on port 8080? Answer: Use
sudo lsof -i :8080
orsudo netstat -tulpn | grep 8080
. -
How do you monitor CPU, memory, disk I/O in real-time? Answer: Use tools like
top
,htop
,iotop
, orvmstat
. -
How do you analyze system logs for login attempts? Answer: Use
last
,lastb
, andgrep
on/var/log/auth.log
or/var/log/secure
. -
How do you limit CPU usage for a process? Answer: Use
cpulimit
ornice/renice
for scheduling priorities. -
What’s the difference between
cron
,anacron
, andsystemd-timers
? Answer:cron
runs tasks at fixed times but needs system uptime.anacron
runs missed tasks after boot.systemd-timers
are flexible and integrated withsystemd
. -
How do you kill all processes by name? Answer:
pkill <process-name>
orkillall <name>
. -
Explain how
top
,htop
,iotop
, andnmon
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. -
Find all files larger than 100MB in
/var
. Answer:find /var -type f -size +100M -exec ls -lh {} \;
-
Compress and archive
/etc
and schedule a weekly cronjob. Answer: Usetar -czf etc-backup.tar.gz /etc
. Add cron job:0 2 * * 0 /path/to/script.sh
-
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. -
How do you recursively change permissions for a directory tree? Answer: Use
chmod -R <mode> <dir>
. Be cautious—ensure correct depth and scope. -
Explain
strace
and its usage in debugging a hanging script. Answer:strace
traces system calls. Runstrace -p <PID>
to analyze calls. Helpful in detecting file access, permission, or syscall issues. -
Use
awk
,sed
, andgrep
to filter Apache logs for 404s. Answer:grep " 404 " access.log | awk '{print $1}' | sort | uniq -c | sort -nr
-
How do you mount an NFS share? Answer:
mount -t nfs <server>:/path /mnt
. Make persistent by editing/etc/fstab
. -
What’s the difference between
/etc/fstab
and/etc/mtab
? Answer:/etc/fstab
defines static mounts;/etc/mtab
shows active mounts. -
How do you view user login history? Answer: Use
last
to view past logins, andwho
to view current logins. -
How do you configure network interfaces in Linux? Answer: Use
nmcli
,ifconfig
(deprecated), or edit/etc/network/interfaces
or/etc/netplan/*.yaml
(Ubuntu). -
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. -
How do you detect a memory leak in a long-running process? Answer: Use
top
,smem
, orpmap
. Track memory growth over time. Usevalgrind
for C/C++ binaries. -
Use
find
withexec
to delete files older than 7 days. Answer:find /path -type f -mtime +7 -exec rm {} \;
-
Check if a file system is near inode exhaustion. Answer:
df -i
. IfIUse%
is close to 100%, inode exhaustion risk is high. -
What’s the difference between
su
,sudo
, anddoas
? Answer:su
switches user;sudo
runs single commands as another user;doas
is a simpler, more secure alternative tosudo
.