Unix Interview Questions and Answers for 7 years experience

100 Unix Interview Questions and Answers
  1. What is the difference between hard links and symbolic links?

    • Answer: A hard link is a directory entry that points to the same inode as another file. Multiple hard links can exist to the same file data. Deleting one hard link doesn't affect the others, only when the last hard link is deleted is the data removed. A symbolic link (symlink) is a separate file that contains the path to another file. Deleting a symlink doesn't affect the target file, and the symlink can point to a file on a different filesystem.
  2. Explain the concept of process states in Unix.

    • Answer: A Unix process can be in several states: Running (actively using the CPU), Ready (waiting for CPU time), Blocked (waiting for an I/O operation or other event), Stopped (paused by a signal), Zombie (finished execution but not yet removed from the process table).
  3. How do you find the largest file in a directory?

    • Answer: Use the `find` command with the `-printf` option to output file sizes, then pipe to `sort` and `head`: `find . -type f -printf "%s %p\n" | sort -nr | head -n 1`
  4. Describe the role of the shell in a Unix-like system.

    • Answer: The shell is a command-line interpreter. It acts as an interface between the user and the operating system, allowing users to execute commands, run programs, and manage files and processes.
  5. What are the different types of shells available in Unix?

    • Answer: Common shells include Bash (Bourne Again Shell), Zsh (Z Shell), Ksh (Korn Shell), and Csh (C Shell).
  6. Explain the difference between grep, egrep, and fgrep.

    • Answer: `grep` uses basic regular expressions. `egrep` (or `grep -E`) uses extended regular expressions, offering more features. `fgrep` (or `grep -F`) performs a fixed string search, ignoring regular expression metacharacters.
  7. How do you find all files modified in the last 24 hours?

    • Answer: `find . -type f -mtime -1`
  8. What is the purpose of the `chmod` command?

    • Answer: `chmod` changes the file permissions, controlling read, write, and execute access for the owner, group, and others.
  9. Explain the significance of the `/etc/passwd` file.

    • Answer: `/etc/passwd` stores user account information, including usernames, user IDs (UIDs), group IDs (GIDs), home directories, and login shells.
  10. What is the purpose of the `/etc/shadow` file?

    • Answer: `/etc/shadow` stores encrypted user passwords, enhancing security by keeping passwords separate from the publicly readable `/etc/passwd` file.
  11. How do you redirect the output of a command to a file?

    • Answer: Use the `>` operator: `command > output.txt`
  12. How do you append the output of a command to a file?

    • Answer: Use the `>>` operator: `command >> output.txt`
  13. What is the difference between `>` and `>>` redirection operators?

    • Answer: `>` overwrites the file if it exists; `>>` appends to the file.
  14. Explain the concept of pipes in Unix.

    • Answer: Pipes connect the standard output of one command to the standard input of another, allowing chaining of commands.
  15. How do you use pipes to chain commands together?

    • Answer: Use the `|` operator: `command1 | command2 | command3`
  16. What is the purpose of the `awk` command?

    • Answer: `awk` is a pattern-scanning and text-processing language used for manipulating text files.
  17. What is the purpose of the `sed` command?

    • Answer: `sed` is a stream editor used for performing text transformations on an input stream.
  18. How do you kill a process by its process ID (PID)?

    • Answer: Use the `kill` command: `kill `
  19. What is the purpose of the `ps` command?

    • Answer: `ps` displays information about currently running processes.
  20. What is the purpose of the `top` command?

    • Answer: `top` displays dynamic real-time information about running processes.
  21. What is the purpose of the `df` command?

    • Answer: `df` displays disk space usage.
  22. What is the purpose of the `du` command?

    • Answer: `du` displays disk usage of files and directories.
  23. What is the purpose of the `chown` command?

    • Answer: `chown` changes the owner and/or group of a file or directory.
  24. What are file permissions in Unix?

    • Answer: File permissions control read, write, and execute access for the owner, group, and others.
  25. Explain the concept of symbolic links (symlinks).

    • Answer: Symlinks are pointers to other files or directories. They create shortcuts.
  26. How do you create a symbolic link?

    • Answer: Use the `ln -s` command: `ln -s target link_name`
  27. How do you create a hard link?

    • Answer: Use the `ln` command: `ln target link_name`
  28. What is the difference between `ln` and `ln -s`?

    • Answer: `ln` creates a hard link; `ln -s` creates a symbolic link.
  29. What is a process in Unix?

    • Answer: A process is an instance of a running program.
  30. What is a signal in Unix?

    • Answer: A signal is an asynchronous notification sent to a process.
  31. How do you send a signal to a process?

    • Answer: Use the `kill` command with signal numbers: `kill - `
  32. What is the purpose of the `cron` daemon?

    • Answer: `cron` schedules and executes tasks at specified times.
  33. How do you create a cron job?

    • Answer: Edit the `/etc/crontab` file or create a file in `/etc/cron.d/` or a user's crontab file.
  34. What is the role of the init process?

    • Answer: The init process is the first process to run when the system boots; it manages other processes.
  35. What is a system call?

    • Answer: A system call is a request from a program to the operating system's kernel to perform an action.
  36. What are the different file types in Unix?

    • Answer: Regular files, directories, symbolic links, character devices, block devices, pipes, sockets.
  37. How do you determine the type of a file?

    • Answer: Use the `file` command or the `ls -l` command (look at the first character).
  38. What is an inode?

    • Answer: An inode is a data structure that stores metadata about a file, such as permissions, ownership, and location of the file data on the disk.
  39. What is the purpose of the `umask` command?

    • Answer: `umask` sets the file creation mask, determining the default permissions of newly created files and directories.
  40. Explain the concept of process groups in Unix.

    • Answer: A process group is a collection of processes that are managed together. Signals can be sent to entire process groups.
  41. What is a shell script?

    • Answer: A shell script is a file containing a sequence of commands that are executed by the shell.
  42. How do you make a shell script executable?

    • Answer: Use `chmod +x script_name`.
  43. What are environment variables in Unix?

    • Answer: Environment variables are dynamic named values that can be accessed by processes.
  44. How do you view environment variables?

    • Answer: Use the `env` or `printenv` command.
  45. How do you set an environment variable?

    • Answer: Use `export VARIABLE=value`.
  46. What is the difference between absolute and relative paths?

    • Answer: Absolute paths start from the root directory (`/`); relative paths are relative to the current working directory.
  47. What is the purpose of the `find` command?

    • Answer: `find` searches for files and directories based on specified criteria.
  48. What is the purpose of the `locate` command?

    • Answer: `locate` quickly finds files by name using a pre-built database.
  49. What is the difference between `find` and `locate`?

    • Answer: `find` searches in real-time; `locate` uses a database, which may be outdated.
  50. How do you compress and decompress files in Unix?

    • Answer: Use commands like `gzip`, `bzip2`, `tar`, `zip`, `unzip` etc.
  51. What is the purpose of the `tar` command?

    • Answer: `tar` archives and optionally compresses files.
  52. What are regular expressions?

    • Answer: Regular expressions are patterns used to match strings of text.
  53. Explain some common regular expression metacharacters.

    • Answer: `.`, `*`, `+`, `?`, `[ ]`, `^`, `$`, `{ }`, `|`, `()`, `\`
  54. What are the different types of I/O redirection?

    • Answer: Standard input (stdin), standard output (stdout), standard error (stderr).
  55. How do you redirect standard error?

    • Answer: Use `2>` or `2>>`.
  56. How do you redirect both standard output and standard error?

    • Answer: Use `&>` or `&>>`.
  57. What are the differences between the `cat`, `head`, and `tail` commands?

    • Answer: `cat` displays entire file content; `head` displays the beginning; `tail` displays the end.
  58. How do you display the last 10 lines of a file?

    • Answer: `tail -n 10 file.txt`
  59. How do you display the first 5 lines of a file?

    • Answer: `head -n 5 file.txt`
  60. What is a shell variable?

    • Answer: A shell variable is a named storage location that holds a value within a shell session.
  61. How to you check the disk space on a Linux system?

    • Answer: Use the `df -h` command for a human-readable output.
  62. How would you monitor system performance?

    • Answer: Use tools like `top`, `htop`, `iostat`, `vmstat`, `mpstat`, etc depending on the specific aspect you want to monitor.
  63. Explain the concept of process scheduling in Unix.

    • Answer: The operating system's kernel uses a scheduler to determine which processes get CPU time and when. Various scheduling algorithms (e.g., round-robin, priority-based) exist.
  64. What is a daemon process?

    • Answer: A daemon is a background process that runs without a controlling terminal.
  65. How do you check running daemon processes?

    • Answer: Use the `ps aux | grep` command, filtering by relevant daemon names.
  66. How would you troubleshoot a disk I/O bottleneck?

    • Answer: Use `iostat` to monitor disk activity. Analyze disk utilization, response times, and queue lengths. Consider factors like disk fragmentation, hardware limitations, and file system structure.
  67. What are some common Unix system administration tasks?

    • Answer: User and group management, file system maintenance, software installation and updates, system monitoring and logging, network configuration, security management, and backup and recovery.
  68. What is the importance of system logging in Unix?

    • Answer: System logs record events and errors, crucial for troubleshooting, security auditing, and system monitoring.
  69. How would you search within log files?

    • Answer: Use `grep`, `egrep`, `awk`, or other text processing tools with appropriate regular expressions.
  70. Explain the concept of permissions inheritance in Unix.

    • Answer: When a new file or directory is created, it inherits permissions from its parent directory, unless modified explicitly.
  71. How do you remotely access a Unix system?

    • Answer: Using SSH (Secure Shell) is the most common and secure method.
  72. What is the importance of regular backups?

    • Answer: Regular backups protect against data loss due to hardware failure, accidental deletion, or other issues.

Thank you for reading our blog post on 'Unix Interview Questions and Answers for 7 years experience'.We hope you found it informative and useful.Stay tuned for more insightful content!