fbpx

Top 100 Unix Interview Questions and Answers

Top 100 Unix Interview Questions and Answers
Contents show

1. What is a shell in Unix?

Answer: A shell in Unix is a command-line interface that allows users to interact with the operating system. It interprets user commands and communicates them to the operating system for execution. Examples include Bash, Zsh, and Fish.


2. How do you list files in a directory using the ls command?

Answer:

ls

This command lists all files and directories in the current directory.


3. What is the purpose of the pwd command?

Answer:

pwd

This command prints the current working directory, showing the user’s current location in the file system.


4. How do you create a new directory in Unix?

Answer:

mkdir directory_name

This command creates a new directory with the specified name.


5. Explain the cd command in Unix.

Answer:

cd directory_path

This command is used to change the current working directory. directory_path should be the path to the desired directory.


6. How do you copy files in Unix?

Answer:

cp source_file destination

This command copies source_file to the specified destination.


7. Explain the purpose of the mv command.

Answer:

mv source destination

This command is used to move or rename files and directories. It can also be used for renaming.


8. How do you remove a file in Unix?

Answer:

rm file_name

This command deletes the specified file.


9. Explain the purpose of the rmdir command.

Answer:

rmdir directory_name

This command is used to remove empty directories.


10. How do you create an empty file in Unix?

Answer:

touch file_name

This command creates a new, empty file with the specified name.


11. Explain the cat command in Unix.

Answer:

cat file_name

This command is used to concatenate and display the contents of files.


12. How do you view the contents of a file page by page in Unix?

Answer:

less file_name

This command displays the contents of file_name one page at a time.


13. What is a symbolic link in Unix?

Answer:

ln -s source_file link_name

This command creates a symbolic link named link_name that points to source_file.


14. Explain the grep command in Unix.

Answer:

grep pattern file_name

This command searches for pattern in file_name and prints matching lines.


15. How do you find files in Unix based on various criteria?

Answer:

find directory -type f -name "*.txt"

This command finds all .txt files in the specified directory.


16. What is the purpose of the tar command in Unix?

Answer:

tar -cvf archive.tar files

This command creates a tar archive named archive.tar containing the specified files.


17. How do you compress and decompress files in Unix?

Answer:

# Compress
gzip file_name

# Decompress
gzip -d file_name.gz

This compresses and decompresses files using the gzip algorithm.


18. Explain the purpose of the top command in Unix.

Answer:

top

This command provides a dynamic view of system processes, displaying CPU and memory usage.


19. How do you check disk space usage in Unix?

Answer:

df -h

This command displays disk space usage in a human-readable format.


20. What is a cron job in Unix?

Answer:

* * * * * command_to_execute

This creates a cron job that runs command_to_execute at specified intervals.


21. Explain the purpose of the chmod command.

Answer:

chmod permissions file_name

This command changes the permissions of a file.


22. How do you change the ownership of a file in Unix?

Answer:

chown new_owner file_name

This command changes the owner of file_name to new_owner.


23. What is the purpose of the ps command in Unix?

Answer:

ps

This command lists the currently running processes.


24. How do you kill a process in Unix?

Answer:

kill process_id

This command terminates the process with the specified process_id.


25. Explain the purpose of the ssh command.

Answer:

ssh user@host

This command allows you to connect to a remote server securely.


26. How do you check the version of Unix you’re running?

Answer:

uname -a

This command displays information about the system.


27. What is the purpose of the ifconfig command in Unix?

Answer:

ifconfig

This command displays information about network interfaces.


28. How do you set environment variables in Unix?

Answer:

export VARIABLE_NAME=value

This command sets the value of the environment variable VARIABLE_NAME.


29. Explain the purpose of the grep command in Unix.

Answer:

grep pattern file_name

This command searches for pattern in file_name and prints matching lines.


30. How do you find and replace text in a file using sed in Unix?

Answer:

sed 's/old_text/new_text/g' file_name

This command finds and replaces old_text with new_text in file_name.


31. What is a shell script in Unix?

Answer: A shell script in Unix is a file containing a sequence of shell commands that can be executed by the shell. It allows for automation of tasks and can be run as a program.


32. How do you define a function in a shell script?

Answer:

function_name() {
    # commands
}

This defines a function named function_name.


33. What is process substitution in Unix?

Answer: Process substitution in Unix allows you to use the output of a command as a file. It’s a way to pass information from one process to another.


34. How do you use awk to process text files in Unix?

Answer:

awk '/pattern/ {print $1}' file_name

This command uses awk to print the first field of lines containing pattern in file_name.


35. Explain the purpose of the tee command in Unix.

Answer:

command | tee output_file

This command redirects output to both the screen and output_file.


36. How do you schedule a job to run at a specific time using at in Unix?

Answer:

echo "command" | at HH:MM

This schedules command to run at the specified time.


37. What is a shebang line in a shell script?

Answer: The shebang line (#!/bin/bash) in a shell script specifies the interpreter that should be used to execute the script. It’s placed at the beginning of the script.


38. Explain the purpose of the tr command in Unix.

Answer:

echo "string" | tr 'a-z' 'A-Z'

This command translates characters in string from lowercase to uppercase.


39. How do you calculate the factorial of a number in a shell script?

Answer:

factorial=1
for ((i=1; i<=n; i++)); do
    factorial=$((factorial * i))
done

This shell script calculates the factorial of n.


40. What is a signal in Unix?

Answer: In Unix, a signal is a software interrupt delivered to a process. It can be used to communicate specific events or requests to a running process.


41. How do you handle signals in a shell script?

Answer:

trap 'command' SIGNAL

This command sets up a trap to execute command when the specified SIGNAL is received.


42. Explain the purpose of the basename command in Unix.

Answer:

basename /path/to/file

This command extracts and prints the base name (file name) from the given path.


43. How do you check if a file exists in a shell script?

Answer:

if [ -f file_name ]; then
    echo "File exists."
fi

This shell script checks if file_name exists.


44. What is a process group in Unix?

Answer: A process group in Unix is a collection of one or more processes that can be controlled as a single entity.


45. Explain the purpose of the nohup command in Unix.

Answer:

nohup command &

This command runs command in the background, and ensures it continues to run even if the user logs out.


46. How do you search for a string in multiple files using grep?

Answer:

grep -r "pattern" directory

This command searches for pattern in all files within directory

and its subdirectories.


47. What is a zombie process in Unix?

Answer: A zombie process in Unix is a process that has completed execution but still has an entry in the process table. It’s a process that has finished its execution, but its parent process hasn’t yet called wait() to read its exit status.


48. How do you find and kill zombie processes in Unix?

Answer:

ps aux | grep "Z"
kill -s SIGCHLD process_id

The first command lists zombie processes, and the second command kills the parent process to clean them up.


49. Explain the purpose of the cut command in Unix.

Answer:

cut -d, -f2 file_name

This command extracts the second field from file_name, using , as the delimiter.


50. How do you count the number of lines in a file in Unix?

Answer:

wc -l file_name

This command counts the number of lines in file_name.


51. What is the purpose of the sort command in Unix?

Answer:

sort file_name

This command sorts the lines of file_name alphabetically.


52. How do you find the most frequently occurring word in a file using Unix commands?

Answer:

tr -cs A-Za-z '\n' < file_name | tr A-Z a-z | sort | uniq -c | sort -nr | head -n 1

This command finds the most frequently occurring word in file_name.


53. Explain the purpose of the curl command in Unix.

Answer:

curl url

This command is used to transfer data to or from a server.


54. How do you download a file from the internet using wget in Unix?

Answer:

wget url

This command downloads the file specified by url.


55. What is a pipe (|) in Unix?

Answer: A pipe (|) in Unix is a way to redirect the output of one command as input to another command.


56. How do you find and replace text in multiple files in Unix?

Answer:

find . -type f -exec sed -i 's/old_text/new_text/g' {} +

This command finds and replaces old_text with new_text in all files within the current directory and its subdirectories.


57. Explain the purpose of the tail command in Unix.

Answer:

tail -n 10 file_name

This command displays the last 10 lines of file_name.


58. How do you search for a file in Unix?

Answer:

find directory -name file_name

This command searches for file_name within the specified directory.


59. What is a semaphore in Unix?

Answer: A semaphore in Unix is a synchronization primitive used for inter-process communication.


60. Explain the purpose of the awk command in Unix.

Answer:

awk '{print $1}' file_name

This command processes and analyzes text files.


61. How do you display the contents of a file in reverse order using Unix commands?

Answer:

tac file_name

This command displays the contents of file_name in reverse order.


62. What is the purpose of the watch command in Unix?

Answer:

watch command

This command executes command repeatedly and displays the output in real-time.


63. How do you list open ports in Unix?

Answer:

netstat -tuln

This command lists all open ports.


64. Explain the purpose of the du command in Unix.

Answer:

du -sh directory

This command displays disk usage for the specified directory.


65. How do you archive and compress files in Unix using tar and gzip?

Answer:

tar -cvf archive.tar files
gzip archive.tar

This command creates a compressed tar archive named archive.tar.gz.


66. What is a kernel in Unix?

Answer: The kernel in Unix is the core component of the operating system. It manages resources, schedules processes, and facilitates communication between hardware and software components.


67. How do you list the environment variables in Unix?

Answer:

printenv

This command lists all environment variables.


68. Explain the purpose of the nohup command in Unix.

Answer:

nohup command &

This command runs command in the background, ensuring it continues to run even if the user logs out.


69. How

do you display the first 10 lines of a file using Unix commands?

Answer:

head file_name

This command displays the first 10 lines of file_name.


70. What is a symbolic link in Unix?

Answer:

ln -s source_file link_name

This command creates a symbolic link named link_name that points to source_file.


71. How do you check if a process is running in Unix?

Answer:

ps -ef | grep process_name

This command checks if a process named process_name is running.


72. What is a mount point in Unix?

Answer: A mount point in Unix is a directory where a file system is attached for access.


73. How do you change file permissions in Unix using chmod?

Answer:

chmod permissions file_name

This command changes the permissions of file_name.


74. Explain the purpose of the tr command in Unix.

Answer:

echo "string" | tr 'a-z' 'A-Z'

This command translates characters in string from lowercase to uppercase.


75. What is a cron job in Unix?

Answer:

* * * * * command_to_execute

This creates a cron job that runs command_to_execute at specified intervals.


76. How do you search for a string in multiple files using grep?

Answer:

grep -r "pattern" directory

This command searches for pattern in all files within directory and its subdirectories.


77. Explain the purpose of the cut command in Unix.

Answer:

cut -d, -f2 file_name

This command extracts the second field from file_name, using , as the delimiter.


78. What is a pipe (|) in Unix?

Answer: A pipe (|) in Unix is a way to redirect the output of one command as input to another command.


79. How do you find and replace text in multiple files in Unix?

Answer:

find . -type f -exec sed -i 's/old_text/new_text/g' {} +

This command finds and replaces old_text with new_text in all files within the current directory and its subdirectories.


80. Explain the purpose of the tail command in Unix.

Answer:

tail -n 10 file_name

This command displays the last 10 lines of file_name.


81. What is the purpose of the sort command in Unix?

Answer:

sort file_name

This command sorts the lines of file_name alphabetically.


82. How do you search for a file in Unix?

Answer:

find directory -name file_name

This command searches for file_name within the specified directory.


83. What is a semaphore in Unix?

Answer: A semaphore in Unix is a synchronization primitive used for inter-process communication.


84. Explain the purpose of the awk command in Unix.

Answer:

awk '{print $1}' file_name

This command processes and analyzes text files.


85. How do you display the contents of a file in reverse order using Unix commands?

Answer:

tac file_name

This command displays the contents of file_name in reverse order.


86. What is the purpose of the watch command in Unix?

Answer:

watch command

This command executes command repeatedly and displays the output in real-time.


87. How do you list open ports in Unix?

Answer:

netstat -tuln

This command lists all open ports.


88. Explain the purpose of the du command in Unix.

Answer:

du -sh directory

This command displays disk usage for the specified directory.


89. How do you archive and compress files in Unix using tar and gzip?

Answer:

tar -cvf archive.tar files
gzip archive.tar

This command creates a compressed tar archive named archive.tar.gz.


90. What is a kernel in Unix?

Answer: The kernel in Unix is the core component of the operating system. It manages resources, schedules processes, and facilitates communication between hardware and software components.


91. How do you list the environment variables in Unix?

Answer:

printenv

This command lists all environment variables.


92. Explain the purpose of the nohup command in Unix.

Answer:

noh

up command &

This command runs command in the background, ensuring it continues to run even if the user logs out.


93. How do you display the first 10 lines of a file using Unix commands?

Answer:

head file_name

This command displays the first 10 lines of file_name.


94. What is a symbolic link in Unix?

Answer:

ln -s source_file link_name

This command creates a symbolic link named link_name that points to source_file.


95. How do you check if a process is running in Unix?

Answer:

ps -ef | grep process_name

This command checks if a process named process_name is running.


96. What is a mount point in Unix?

Answer: A mount point in Unix is a directory where a file system is attached for access.


97. How do you change file permissions in Unix using chmod?

Answer:

chmod permissions file_name

This command changes the permissions of file_name.


98. Explain the purpose of the tr command in Unix.

Answer:

echo "string" | tr 'a-z' 'A-Z'

This command translates characters in string from lowercase to uppercase.


99. What is a cron job in Unix?

Answer:

* * * * * command_to_execute

This creates a cron job that runs command_to_execute at specified intervals.


100. How do you search for a string in multiple files using grep?

Answer:

grep -r "pattern" directory

This command searches for pattern in all files within directory and its subdirectories.