Interview

10 Simple Unix Interview Questions and Answers

Prepare for your Unix interview with our comprehensive guide, featuring common and advanced questions to enhance your understanding and skills.

Unix is a powerful and versatile operating system that has been foundational in the development of many modern computing environments. Known for its stability, multitasking capabilities, and robust security features, Unix is widely used in both academic and commercial settings. Its command-line interface and scripting capabilities make it a favorite among system administrators and developers for automating tasks and managing complex systems.

This article provides a curated selection of Unix interview questions designed to test and enhance your understanding of fundamental concepts and practical skills. By working through these questions, you will be better prepared to demonstrate your proficiency in Unix during technical interviews, showcasing your ability to navigate and utilize this essential operating system effectively.

Simple Unix Interview Questions and Answers

1. Explain the purpose of the grep command and provide an example of its usage.

The grep command in Unix searches for specific patterns within files. It stands for “Global Regular Expression Print” and is efficient for searching through large volumes of text. It supports regular expressions, making it a powerful tool for pattern matching.

Example:

grep "error" logfile.txt

This command searches for the word “error” in logfile.txt and prints all lines containing it.

2. Write a command to list all files in a directory, including hidden files, sorted by modification time.

To list all files in a directory, including hidden ones, sorted by modification time, use:

ls -la --sort=time

Explanation:
ls lists directory contents.
-l provides a detailed list.
-a includes hidden files.
--sort=time sorts files by modification time.

3. Describe how you would find and replace text within multiple files in a directory.

To find and replace text within multiple files in a directory, use find with sed or perl.

Example with sed:

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

This searches for all files in the specified directory and replaces old_text with new_text.

Alternatively, use perl for more complex replacements:

find /path/to/directory -type f -exec perl -pi -e 's/old_text/new_text/g' {} +

4. How would you schedule a job to run at 2 AM every day?

To schedule a job to run at 2 AM every day, use cron jobs. Edit the crontab file with:

crontab -e

Add the following line:

0 2 * * * /path/to/your/script.sh

This specifies the job to run at 2 AM daily.

5. How would you change the ownership of a file to another user?

The chown command changes the ownership of a file or directory to another user. The basic syntax is:

chown [new_owner][:new_group] file_name

Example:

chown newuser example.txt

To change the group as well:

chown newuser:newgroup example.txt

6. Write a script to archive and compress a directory.

To archive and compress a directory, use the tar command. Here’s a script:

#!/bin/bash

DIRECTORY_TO_ARCHIVE="my_directory"
ARCHIVE_NAME="my_directory_archive.tar.gz"

tar -czvf $ARCHIVE_NAME $DIRECTORY_TO_ARCHIVE

echo "Directory $DIRECTORY_TO_ARCHIVE has been archived and compressed into $ARCHIVE_NAME"

7. Write a command to find all files larger than 100MB in a directory and its subdirectories.

To find all files larger than 100MB in a directory and its subdirectories, use:

find /path/to/directory -type f -size +100M

This command filters files larger than 100MB.

8. Explain Unix file permissions and how to modify them.

Unix file permissions determine who can read, write, or execute a file. Each file and directory has three types of permissions for the owner, group, and others. Permissions are represented as a string of 10 characters, such as -rwxr-xr--.

To modify permissions, use the chmod command. Permissions can be changed using symbolic or numeric representation.

Symbolic example:

chmod u+x file.txt

Numeric example:

chmod 755 file.txt

9. Explain piping and redirection in Unix with examples.

Piping in Unix uses the pipe symbol (|) to allow the output of one command to be used as the input for another.

Example:

ls -l | grep ".txt"

Redirection changes the standard input/output of commands. The greater-than symbol (>) redirects output to a file, while the less-than symbol (<) redirects input from a file. Examples:

echo "Hello, world!" > output.txt
sort < unsorted.txt > sorted.txt

10. Write a script to monitor disk usage and send an alert if usage exceeds 90%.

To monitor disk usage and send an alert if usage exceeds 90%, use this script:

#!/bin/bash

THRESHOLD=90
USAGE=$(df / | grep / | awk '{ print $5 }' | sed 's/%//g')

if [ $USAGE -gt $THRESHOLD ]; then
  echo "Disk usage is above $THRESHOLD%. Current usage is $USAGE%." | mail -s "Disk Usage Alert" [email protected]
fi
Previous

15 Linux Networking Interview Questions and Answers

Back to Interview
Next

10 Oracle Database Performance Tuning Interview Questions and Answers