Unix Interview Questions and Answers for freshers

100 Unix Interview Questions and Answers for Freshers
  1. What is Unix?

    • Answer: Unix is a family of multitasking, multiuser computer operating systems that derive from the original AT&T Unix, developed in the late 1960s and early 1970s. It's known for its portability, efficiency, and influence on modern operating systems like Linux and macOS.
  2. What is the kernel in Unix?

    • Answer: The kernel is the core of the Unix operating system. It manages the computer's resources, such as memory, CPU, and I/O devices. It acts as an intermediary between the hardware and the user applications.
  3. Explain the difference between a process and a thread.

    • Answer: A process is an independent execution environment with its own memory space, while a thread is a lightweight unit of execution within a process, sharing the same memory space. Threads within a process can communicate more easily than separate processes.
  4. What is a shell in Unix?

    • Answer: A shell is a command-line interpreter that allows users to interact with the Unix kernel. It translates commands typed by the user into system calls understood by the kernel.
  5. Name some popular Unix shells.

    • Answer: Bash (Bourne Again Shell), Zsh (Z Shell), Ksh (Korn Shell), Csh (C Shell).
  6. What is the command to list files and directories?

    • Answer: ls
  7. How do you list hidden files using ls?

    • Answer: ls -a
  8. What does the command `pwd` do?

    • Answer: pwd (print working directory) displays the current working directory.
  9. How do you create a new directory?

    • Answer: mkdir [directory_name]
  10. How do you remove a directory?

    • Answer: rmdir [directory_name] (for empty directories) or rm -r [directory_name] (for non-empty directories, use with caution).
  11. What is the command to change directories?

    • Answer: cd [directory_name]
  12. How do you copy a file?

    • Answer: cp [source_file] [destination_file]
  13. How do you move or rename a file?

    • Answer: mv [source_file] [destination_file]
  14. How do you remove a file?

    • Answer: rm [file_name]
  15. What is the command to display the contents of a file?

    • Answer: cat [file_name] or less [file_name] (for large files).
  16. What does `grep` do?

    • Answer: grep searches for patterns within files.
  17. How would you search for the word "example" in a file named "my_file.txt"?

    • Answer: grep "example" my_file.txt
  18. What is the purpose of the `find` command?

    • Answer: find searches for files and directories based on specified criteria.
  19. How would you find all files ending with ".txt" in the current directory?

    • Answer: find . -name "*.txt"
  20. What does `chmod` do?

    • Answer: chmod changes the permissions of a file or directory.
  21. Explain file permissions in Unix (using the example of `chmod 755 myfile`).

    • Answer: chmod 755 myfile sets permissions to read, write, and execute for the owner (7), read and execute for the group (5), and read and execute for others (5).
  22. What is a pipe in Unix?

    • Answer: A pipe is a mechanism to connect the output of one command to the input of another command.
  23. Give an example of using a pipe.

    • Answer: ls -l | grep "txt" (lists files in long format, then filters to show only those containing "txt").
  24. What is input/output redirection?

    • Answer: It redirects the standard input, output, or error streams of a command to a file or another command.
  25. How would you redirect the output of a command to a file?

    • Answer: command > output.txt
  26. How would you append the output of a command to a file?

    • Answer: command >> output.txt
  27. What is the command to display the current date and time?

    • Answer: date
  28. What is the command to view system logs? (Give one example)

    • Answer: dmesg (for kernel messages), tail /var/log/syslog (for recent system logs - path may vary depending on the system).
  29. What is `wc` command used for?

    • Answer: wc counts lines, words, and characters in a file.
  30. What is `sort` command used for?

    • Answer: sort sorts lines of text files.
  31. What is `uniq` command used for?

    • Answer: uniq filters out repeated consecutive lines from a sorted file.
  32. What is the difference between hard links and symbolic links?

    • Answer: A hard link is another name for the same file, sharing the same inode. A symbolic link (symlink) is a pointer to a file or directory.
  33. How do you create a symbolic link?

    • Answer: ln -s [original_file] [link_name]
  34. What is the purpose of the `chown` command?

    • Answer: chown changes the owner and/or group of a file or directory.
  35. What is the purpose of the `chgrp` command?

    • Answer: chgrp changes the group of a file or directory.
  36. What is a regular expression?

    • Answer: A regular expression (regex or regexp) is a sequence of characters that define a search pattern.
  37. Give an example of a simple regular expression.

    • Answer: a.*b (matches "a" followed by any characters, followed by "b").
  38. What is the `tar` command used for?

    • Answer: tar is used to create and manipulate archive files (e.g., .tar, .tar.gz, .tar.bz2).
  39. How would you create a tar archive of a directory?

    • Answer: tar -czvf myarchive.tar.gz mydirectory
  40. How would you extract a tar archive?

    • Answer: tar -xzvf myarchive.tar.gz
  41. What is the `gzip` command used for?

    • Answer: gzip compresses and decompresses files using the gzip algorithm.
  42. How would you compress a file using gzip?

    • Answer: gzip myfile.txt
  43. How would you decompress a gzip file?

    • Answer: gunzip myfile.txt.gz
  44. What is the `bzip2` command used for?

    • Answer: bzip2 compresses and decompresses files using the bzip2 algorithm (generally offers higher compression than gzip).
  45. What are environment variables?

    • Answer: Environment variables are dynamic named values that can affect the behavior of programs and the shell.
  46. How do you view environment variables?

    • Answer: env or printenv
  47. How do you set an environment variable?

    • Answer: export MY_VARIABLE="value"
  48. What is the `echo` command used for?

    • Answer: echo displays text on the terminal.
  49. What is a process ID (PID)?

    • Answer: A unique numerical identifier assigned to each running process by the operating system.
  50. How do you find the PID of a running process?

    • Answer: ps aux | grep [process_name]
  51. What is the `kill` command used for?

    • Answer: kill sends signals to processes, often used to terminate them.
  52. How would you kill a process with PID 1234?

    • Answer: kill 1234
  53. What is the difference between `kill` and `kill -9`?

    • Answer: kill sends the TERM signal (allowing the process to clean up), while kill -9 sends the SIGKILL signal (forcing immediate termination without cleanup).
  54. What is the `top` command used for?

    • Answer: top displays dynamic real-time information about running processes.
  55. What is the `ps` command used for?

    • Answer: ps displays information about running processes (a snapshot).
  56. What is the `head` command used for?

    • Answer: head displays the first few lines of a file (default is 10).
  57. What is the `tail` command used for?

    • Answer: tail displays the last few lines of a file (default is 10).
  58. How would you display the last 20 lines of a log file?

    • Answer: tail -n 20 logfile.txt
  59. What is the `diff` command used for?

    • Answer: diff compares two files and shows the differences between them.
  60. What is the `cut` command used for?

    • Answer: cut extracts sections from each line of files.
  61. What is the `awk` command used for?

    • Answer: awk is a powerful text processing tool that can perform pattern scanning and text manipulation.
  62. What is the `sed` command used for?

    • Answer: sed is a stream editor for filtering and transforming text.
  63. What is a shell script?

    • Answer: A shell script is a program written in a shell scripting language (like Bash) that automates tasks.
  64. What is the shebang line in a shell script?

    • Answer: The shebang line (e.g., #!/bin/bash) specifies the interpreter to use for executing the script.
  65. How do you make a shell script executable?

    • Answer: chmod +x myscript.sh
  66. What is the `cron` utility used for?

    • Answer: cron schedules tasks to run at specific times or intervals.
  67. What is a crontab?

    • Answer: A crontab is a file containing a list of scheduled tasks for a user.
  68. How do you edit your crontab?

    • Answer: crontab -e
  69. What are some common crontab entries? (Give one example)

    • Answer: 0 0 * * * /path/to/myscript.sh (runs myscript.sh at midnight every day).
  70. What is the `umask` command used for?

    • Answer: umask sets the default permissions for newly created files and directories.
  71. What is the `whoami` command used for?

    • Answer: whoami displays the current user's username.
  72. What is the `finger` command used for?

    • Answer: finger displays information about users on the system (information available depends on system configuration).
  73. What is the `hostname` command used for?

    • Answer: hostname displays the system's hostname.
  74. What is the `uname` command used for?

    • Answer: uname prints system information (kernel name, node name, etc.).
  75. What is a hard link?

    • Answer: A hard link is a directory entry that points to the same inode as another file. Deleting one hard link does not affect others.
  76. What is a symbolic link (symlink)?

    • Answer: A symbolic link is a special file that points to another file or directory. It's essentially a shortcut.
  77. What is the difference between `>` and `>>` in redirection?

    • Answer: `>` overwrites the file, `>>` appends to the file.
  78. How do you redirect standard error to a file?

    • Answer: command 2> error.txt
  79. How do you redirect both standard output and standard error to a file?

    • Answer: command &> output.txt or command > output.txt 2>&1

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