Unix Administrator (20) Welcome to our Unix Interview Questions and Answers page!
Here, you will find a comprehensive collection of Unix-related interview questions and their detailed answers. Whether you are a beginner or an experienced professional, this page is a valuable resource to help you prepare for your Unix job interview. Good luck!
Top 20 Basic Unix interview questions and answers
1. What is Unix?
Unix is a powerful, multi-user, and multitasking operating system developed in the 1970s. It provides a command-line interface and supports a wide range of utilities and applications.
2. What are the main components of Unix?
The main components of Unix are the kernel, shell, and file system. The kernel interacts directly with the hardware, while the shell provides the user interface and interprets commands. The file system handles data organization and storage.
3. What is a shell?
A shell is a user interface between the user and the operating system. It interprets the commands entered by the user and executes them. Popular Unix shells include Bash, Csh, and Ksh.
4. What is a file system?
A file system is a method used for organizing and storing files on a computer. Unix uses a hierarchical file system structure, where files and directories are organized in a tree-like format.
5. How do you list files in a directory?
The “ls” command is used to list files and directories in a directory. Adding options like “-l” provides detailed information about the files (e.g., permissions, size, date modified).
6. How do you change directory in Unix?
The “cd” command is used to change directory in Unix. For example, “cd /path/to/directory” would navigate to the specified directory.
7. How do you create a file in Unix?
The “touch” command is used to create a new file in Unix. For example, “touch filename.txt” would create a file named “filename.txt”.
8. How do you remove a file in Unix?
The “rm” command is used to remove (delete) a file in Unix. For example, “rm filename.txt” would delete the file named “filename.txt”.
9. What is the difference between “rm” and “rmdir” commands?
The “rm” command is used to remove files and directories, while the “rmdir” command is used to remove only empty directories. The “rm” command can be used recursively to remove directory contents.
10. How do you copy a file in Unix?
The “cp” command is used to copy files in Unix. For example, “cp file1.txt file2.txt” would create a copy of “file1.txt” named “file2.txt”.
11. How do you move or rename a file in Unix?
The “mv” command is used to move or rename files in Unix. For example, “mv file1.txt file2.txt” would rename “file1.txt” to “file2.txt”.
12. How do you find files in Unix?
The “find” command is used to search for files in Unix. For example, “find /path/to/search -name filename.txt” would find all instances of “filename.txt” in the specified directory.
13. What is a pipeline in Unix?
A pipeline in Unix allows the output of one command to be used as the input of another command. It uses the “|” symbol to connect commands.
14. How do you display the contents of a file in Unix?
The “cat” command is used to display the contents of a file in Unix. For example, “cat filename.txt” would display the contents of “filename.txt”.
15. How do you count the number of lines in a file?
The “wc” command with the “-l” option is used to count the number of lines in a file. For example, “wc -l filename.txt” would display the line count of “filename.txt”.
16. How do you sort the lines of a file in Unix?
The “sort” command is used to sort the lines of a file in Unix. For example, “sort filename.txt” would sort the lines of “filename.txt” in ascending order.
17. How do you search for a specific pattern in a file?
The “grep” command is used to search for a specific pattern in a file. For example, “grep pattern filename.txt” would display all lines in “filename.txt” containing the specified pattern.
18. What is the purpose of the “chmod” command?
The “chmod” command is used to change the permissions of a file or directory in Unix. It can set or remove read, write, and execute permissions for users, groups, and others.
19. How do you view the manual (help) pages in Unix?
The “man” command is used to view the manual (help) pages for various Unix commands. For example, “man ls” would display the manual page for the “ls” command.
20. What are environment variables in Unix?
Environment variables are dynamic values that can affect the behavior of Unix processes. They are used to store information such as user preferences, system configuration, and program settings. The “env” command is used to display the current environment variables.
Top 20 Advanced Unix Interview Questions and Answers
1. What is the difference between a soft link and a hard link in Unix?
A soft link, also known as a symbolic link, is a pointer to another file or directory. It can be in a different file system. A hard link, on the other hand, is a directory entry that points to the same physical file on disk as the original one. A hard link cannot refer to a file or directory in a different file system.
2. How do you find the number of lines in a file without using the ‘wc -l’ command?
You can use the ‘grep’ command to find the number of lines in a file without using ‘wc -l’ as follows:
“`
grep -c ‘^’ filename
“`
3. How can you find all files modified in the last 24 hours?
You can use the ‘find’ command in combination with the ‘mtime’ option to find all files modified in the last 24 hours:
“`
find /path/to/directory -mtime 0
“`
4. Explain the difference between the ‘cp’ and ‘mv’ commands.
The ‘cp’ command is used to copy files or directories from one location to another, while the ‘mv’ command is used to move files or directories from one location to another. The ‘cp’ command creates a copy of the file/directory, while the ‘mv’ command moves it.
5. How can you delete all files in a directory except for one specific file?
You can use the ‘find’ command with the ‘not’ operator to delete all files in a directory except for one specific file:
“`
find /path/to/directory ! -name ‘specificfile’ -type f -exec rm {} +
“`
6. What is the purpose of the ‘grep’ command?
The ‘grep’ command is used to search for text patterns in files. It can be used to search for specific words, phrases, or regular expressions.
7. How do you check the current disk space usage on a Unix system?
You can use the ‘df’ command to check the current disk space usage on a Unix system. Running ‘df -h’ will display the disk space usage in a more readable format.
8. Explain the ‘chmod’ command in Unix.
The ‘chmod’ command is used to change the permissions of a file or directory. It allows you to specify whether the file can be read, written, or executed by the owner, group, or other users.
9. How do you find the largest files or directories on a Unix system?
You can use the ‘du’ command in combination with the ‘sort’ and ‘head’ commands to find the largest files or directories on a Unix system:
“`
du -hs * | sort -rh | head -5
“`
10. What is a shell script in Unix?
A shell script is a text file that contains a series of Unix commands and is executed as a single command. It allows for automation of tasks by executing a sequence of commands.
11. How can you find the process ID (PID) of a running process in Unix?
You can use the ‘ps’ command to find the PID of a running process in Unix. Running ‘ps aux’ will display a list of all running processes along with their PIDs.
12. How do you kill a process in Unix?
You can use the ‘kill’ command followed by the process ID (PID) to kill a process in Unix. For example, ‘kill 1234’ will send a termination signal to the process with PID 1234.
13. Explain the difference between a pipe (|) and a redirection (> or >>) in Unix.
A pipe (|) allows you to send the output of one command as input to another command. It is used to chain commands together. Redirection (> or >>), on the other hand, allows you to redirect the output of a command to a file. ‘>’ overwrites the file if it already exists, while ‘>>’ appends the output to the end of the file.
14. How can you find and replace text in a file using the ‘sed’ command?
You can use the ‘sed’ command to find and replace text in a file by using the ‘s’ command. For example, ‘sed ‘s/text/replacement/g’ filename’ will replace all occurrences of ‘text’ with ‘replacement’ in the specified file.
15. What is the purpose of the ‘tar’ command in Unix?
The ‘tar’ command is used to create, view, or extract files from a tar archive. It is commonly used for creating backup archives or combining multiple files into a single archive.
16. How do you list the contents of a directory in reverse order?
You can use the ‘ls’ command with the ‘-r’ option to list the contents of a directory in reverse order:
“`
ls -r
“`
17. Explain the difference between a relative path and an absolute path in Unix.
A relative path specifies the location of a file or directory relative to the current working directory. An absolute path, on the other hand, specifies the location of a file or directory from the root directory.
18. How can you find all running processes on a Unix system?
You can use the ‘ps’ command to find all running processes on a Unix system. Running ‘ps aux’ will display a list of all running processes along with their details.
19. What is the purpose of the ‘uptime’ command in Unix?
The ‘uptime’ command is used to display the current uptime of a Unix system. It shows the current time, how long the system has been running, the number of users currently logged in, and the system load averages for the past 1, 5, and 15 minutes.
20. How do you change the default shell for a user in Unix?
You can use the ‘chsh’ command followed by the username to change the default shell for a user in Unix:
“`
chsh -s /bin/newshell username
“`
Unix Administrator (20)