Explore a comprehensive collection of Unix Interview Questions and Answers designed to help you ace your next job interview. Whether you're a seasoned Unix pro or a newcomer, our blog offers valuable insights and expert guidance on handling Unix Interview Questions with confidence. Prepare for success with our in-depth knowledge and expert tips.
Home Resources IT Infrastructure & Networking Top 40 Unix Interview Questions and AnswersUNIX Fundamentals Top Rated Course
Exclusive 40% OFF
Enquire Now Download curriclumWe ensure quality, budget-alignment, and timely delivery by our expert instructors.
-->
Navigating the world of technical interviews can be challenging, especially in specialised areas like UNIX systems. So, how do you prepare effectively? One way is by familiarising yourself with commonly asked UNIX Interview Questions. These questions often probe your understanding of UNIX architecture, shell scripting, file systems, and various command-line utilities. They give interviewers an insight into your practical skills and theoretical knowledge of UNIX environments. Read this blog to learn about the UNIX Interview Questions on basic UNIX commands, file and directory operation, and more and how to answer them.
Table of Contents
1) UNIX Interview Questions on basic UNIX commands
2) UNIX Interview Questions on file and directory operations
3) UNIX Interview Questions on UNIX permissions and ownership
4) UNIX Interview Questions on process management
5) UNIX Interview Questions on UNIX shell scripting
6) UNIX Interview Questions on advanced UNIX concepts
This section of the blog will expand on some commonly asked UNIX Commands interview questions.
Ans: The ls command is fundamental in UNIX. By default, when executed, it displays the files and directories in the current directory. It provides a quick visual overview of directory contents, ensuring efficient navigation and file management.
Ans: To get a detailed listing of files, you can employ the ls -l command. This displays permissions, the number of links, file owner, file group, file size, and the time of the last modification. It's especially useful when monitoring file details and attributes.
Ans: The command cd .. is employed to navigate to the parent directory, offering a step backwards in the directory tree. To directly navigate to the root directory, which is the base of the file system hierarchy, you use cd /.
Ans: The cat command, short for "concatenate and display", is versatile. Primarily, it is used to view the contents of a file, streaming its content to the standard output. This command provides a quick way to read small files or check file content without opening in an editor.
Ans: Utilising the head command proves beneficial for this. By default, executing head followed by a filename displays the file's initial ten lines. It's a great way to preview the beginning of large files or logs.
Ans: The tail command serves this purpose efficiently. In its standard form, it exhibits the last ten lines of a file. It is particularly handy for real-time monitoring logs or checking recent file updates.
Ans: The touch command is the tool of choice. When executed with a filename, e.g., touch filename.txt, it either creates an empty file or updates the timestamp of an existing one. It's a swift way to generate files or refresh their access times.
Ans: To delete a file, the rm command is used. For example, executing rm filename.txt will remove the specified file after confirmation. Care should be exercised as this deletion is irreversible in standard file systems.
Ans: The grep command, derived from "Global Regular Expression Print", is indispensable. To search for a pattern within a file, you'd use grep "pattern" filename.txt. It scans the file line by line, printing lines that match the specified pattern – an essential tool for data parsing and content discovery.
Ans: The find command stands out for this task. For example, find . -name "example.txt" will search the current directory (denoted by ".") and its subdirectories for a file named "example.txt". This highly powerful command offers various flags and parameters for refined searching.
This section of the blog will expand on some commonly asked UNIX Interview Questions and answers on file and directory operations.
Ans: In UNIX, the mkdir command is used to create directories. By executing mkdir directory_name, a new directory with the given name is created in the current location. The command is simple yet powerful, with options like -p, which allows the creation of nested directories in a single command. It's essential for organising files and ensuring structured data storage.
Ans: Directories can be removed using the rmdir command for empty directories. For instance, rmdir directory_name will remove the named directory, provided it's empty. However, if you need to remove a directory and its contents, the rm command with the -r (recursive) option, like rm -r directory_name, becomes useful. Caution is advised, as this operation is irreversible and can quickly delete large amounts of data.
Ans: The mv command serves the dual purpose of moving and renaming files or directories. To rename a file, mv old_filename new_filename is used. Similarly, to move a file to a different directory, the format is mv filename target_directory/. The same logic applies to directories. It’s a versatile command that aids in organising and re-structuring file locations and names.
Ans: To duplicate files or directories, UNIX offers the cp command. For copying files, the syntax is cp source_file destination_directory/. If duplicating a directory, the -r option (meaning recursive) must be added, as in cp -r source_directory destination_directory/. This ensures all sub-files and sub-directories are replicated too. The cp command is fundamental when backing up data or creating duplicate research resources.
Ans: The du command, standing for "disk usage", can be employed to ascertain the size of files and directories. du -sh filename would display the size of a file, while du -sh directory_name reveals the total size of a directory. The -s provides a summary and -h displays sizes in a human-readable format (like KB, MB). It's an invaluable command for monitoring space utilisation and managing storage effectively.
Ans: Symbolic links (often called symlinks or soft links) can be crafted using the ln command with the -s option. The general form is ln -s target source_link. This creates a symbolic link named source_link that points to the target. Unlike hard links, symbolic links can reference directories or files across different file systems. They are especially useful for creating shortcuts or ensuring multiple paths to a single resource.
This section of the blog will expand on some commonly asked UNIX Interview Questions and answers on permissions and ownership.
Ans: UNIX file permissions are typically represented using a combination of characters: r (read), w (write), and x (execute). These permissions are set for three different categories: the file's owner (user), the file's group (group), and others (other). A typical permission set might look like rw-r--r--, where the owner has read and write permissions, while the group and others have only read permissions. This symbolic notation can also be represented numerically, with read as 4, write as 2, and execute as 1, making the previous example 644.
Ans: The chmod command, short for "change mode", is employed to modify file or directory permissions. You can use symbolic or numerical notation with this command. For instance, chmod u+x filename adds execute permission for the user (owner), while chmod 755 filename sets permissions to rwxr-xr-x. The command offers flexibility, allowing users to control access to their files and directories precisely.
Ans: The chown command is used to change the owner of a file or directory. For instance, chown newowner filename would change the file's owner to "newowner". If you want to change both the owner and the group simultaneously, you will use a syntax like chown newowner:newgroup filename. For altering only the group, the chgrp command is used. As with most UNIX commands, care should be taken with these tools, ensuring that ownership is only granted to trusted users.
Ans: The sticky bit is a permission setting that safeguards the files within a directory. When set on a directory, it ensures that only the owner of a file can either delete or rename the file, irrespective of the directory's permissions. This is especially useful in shared directories, like /tmp, preventing users from accidentally or maliciously deleting others' files. Symbolically, the sticky bit is represented as a t in the execute field for others, such as drwxrwxrwt.
Ans: A hard link is an additional reference to the same inode on the disk, meaning it effectively "points" to the same data. Permissions changes on a hard link directly modify the original file since they reference the same data. Conversely, a symbolic (or soft) link is a separate file that points to another file's location. Permissions for the symlink are independent of the target file. Additionally, symlinks have their own set of permissions, but these largely affect the symlink itself and not the access to the target file.
A: The setuid, setgid, and sticky bits are special permissions in UNIX that allow for enhanced security and functionality:
a) setuid: When set on an executable file, it ensures that the program runs with the privileges of the owner of the file rather than the user who runs it. Using chmod, you can set the setuid bit with the symbol u+s or numerically as 4000. For instance, chmod u+s filename or chmod 4755 filename.
b) setgid: This works similarly to setuid but for group permissions. It can be set on a file with g+s or 2000. When set on a directory, it ensures that files created within inherit the directory's group ownership. Use the command like chmod g+s directoryname or chmod 2755 directoryname.
c) sticky bit: As mentioned earlier, it ensures that only the file owner can delete or rename files in a directory where it's set. You can set the sticky bit with o+t or numerically as 1000. For example, chmod o+t directoryname or chmod 1755 directoryname.
This section of the blog will expand on some commonly asked UNIX Interview Questions and answers on process management.
Ans: In UNIX, a 'process' is a running instance of a program, possessing its memory space and assigned a unique process ID (PID). It can be in various states, like running, sleeping, or terminated, and is managed by the kernel.
Ans: The ps command displays currently running processes, showing details like PID, terminal type, CPU time, and the command itself. For a comprehensive list, ps -ef or ps aux is commonly used.
Ans: The kill command sends signals to processes, typically to terminate. For example, kill -9 PID sends the SIGKILL signal to terminate the process with the specified PID forcefully.
A: 'Foreground' processes run interactively, occupying the terminal until they are complete. Conversely, 'background' processes run behind the scenes, allowing terminal usage. Using & after a command starts it in the background.
Ans: The 'nice' value determines a process's priority. A process with a high nice value is 'nicer', getting less CPU time. Conversely, a lower nice value gets more CPU time. The nice command adjusts this value.
Ans: In UNIX, the renice command allows users to change the priority of a running process. Adjusting the 'nice' value influences the process's scheduling priority. For example, renice +5 PID would decrease the process's priority with the given PID, while renice -5 PID would increase its priority. Having the appropriate permissions is essential when trying to change the priority of processes not owned by the user.
This section of the blog will expand on some commonly asked UNIX Interview Questions and answers on shell scripting.
Ans: A shell script is a sequence of commands that are stored in a file that the UNIX shell can execute. It automates repetitive tasks, enhances the command's capabilities, and can combine multiple utilities for complex operations.
Ans: To make a shell script executable, use the chmod command followed by +x. For instance, chmod +x scriptname.sh will grant execute permissions to the script.
Ans: The #! (shebang) at a script's start specifies the interpreter's path for executing the script. For example, #!/bin/bash instructs the system to use the Bash shell.
Ans: Arguments can be passed directly after the script's name, like scriptname.sh arg1 arg2. Inside the script, $1, $2, etc., represent these arguments.
Ans: In shell scripting, $# returns the number of arguments passed to a script, while $* displays all arguments as a single string.
Ans: To check if a file exists, use the -f test operator, as in [ -f filename ]. If true, it confirms the file's presence.
This section of the blog will expand on some commonly asked interview questions and answers on advanced UNIX concepts.
Ans: The cron utility in UNIX is a time-based job scheduler. It allows users and administrators to run scripts, commands, or jobs at predetermined intervals or specific times. Jobs can be scheduled to run by the minute, hour, month, week, or any combination thereof. Scheduled tasks are typically defined in a crontab file. The utility is indispensable for automating repetitive tasks, like taking regular backups, clearing temp directories, or fetching updates.
Ans: An inode (index node) in UNIX is a data structure that stores metadata about a file or directory except for its name or actual data. This metadata includes attributes like permissions, ownership, timestamps, and the location of data blocks. Every file or directory has a unique inode number within a filesystem. Inodes are significant because they underpin the UNIX filesystem's structure, allowing the system to manage and retrieve files efficiently. Operations that seem name-based, like renaming, often manipulate inodes behind the scenes.
Ans: A daemon is a background process or service that runs continuously, typically without any direct user interaction. Daemons perform tasks or provide services essential for the system's smooth running. Examples include sshd (secure shell daemon) for remote connections or crond for scheduling tasks. The naming convention for daemons usually ends in 'd'. They are crucial for server environments where services must run persistently, awaiting requests or performing regular duties.
Ans: 'Pipes', denoted by the | symbol, are a fundamental concept in UNIX that enables the output of one command to be fed as the input to another. This allows users to chain multiple commands together in sequence, effectively creating on-the-fly data processing streams. For instance, cat file.txt | grep "search_term" would display lines from file.txt containing "search_term". Pipes exemplify the UNIX philosophy of small, modular utilities that can be combined to produce complex results, promoting efficiency and reusability.
Ans: UNIX employs a parent-child relationship for processes. When a process (parent) spawns another process (child), it's the parent's responsibility to retrieve the child's exit status. If the parent fails, the child remains in the process table, turning into a 'zombie' process. These zombies consume system resources without performing useful work. They can be identified using the ps command with statuses marked 'Z'. While individual zombies aren't harmful, they can clutter the process table in large numbers or if persistently generated, warranting intervention.
Ans: 'IPC' stands for Inter-Process Communication. It refers to the various mechanisms UNIX provides to allow processes to communicate with each other, either within the same system or across different systems. IPC mechanisms include pipes (both named and unnamed), message queues, semaphores, shared memory, and sockets. These tools are paramount because many applications and systems must coordinate tasks among multiple processes.
UNIX is a versatile Operating System with a rich history. By understanding the foundational concepts and commands, you are better equipped to tackle advanced tasks and troubleshooting in UNIX environments. We hope the aforementioned UNIX Interview Questions will be instrumental in your preparation for UNIX-related interviews or roles.
Unlock your potential in the world of Linux with our wide range of Linux Training Courses. Explore our offerings and sign up today!