Interview

15 Unix Commands Interview Questions and Answers

Prepare for your interview with our guide on Unix commands, featuring common questions and answers to boost your confidence and knowledge.

Unix commands form the backbone of many critical systems and applications, making proficiency in them a valuable skill in the tech industry. From system administration to software development, Unix commands are essential for managing files, processes, and system performance. Their versatility and power make them indispensable tools for anyone working in a Unix or Linux environment.

This article offers a curated selection of Unix command questions and answers to help you prepare for your upcoming interview. By familiarizing yourself with these questions, you will gain the confidence and knowledge needed to demonstrate your expertise and problem-solving abilities in Unix-based systems.

Unix Commands Interview Questions and Answers

1. Explain the use of grep and provide an example command to search for a specific string in a file.

The grep command in Unix searches for specific patterns within files. It stands for “Global Regular Expression Print” and is useful for filtering lines in a file that match a given pattern.

Example command:

grep "specific_string" filename.txt

This searches for “specific_string” in filename.txt and prints matching lines.

2. How can you display the first 10 lines of a file?

To display the first 10 lines of a file, use the head command. By default, it shows the first 10 lines, but you can specify a different number with the -n option.

Example:

head filename.txt

To display a different number of lines:

head -n 5 filename.txt

3. Write a command to recursively copy a directory and its contents to another location.

To recursively copy a directory and its contents, use the cp command with the -r option.

cp -r /source_directory /destination_directory

/source_directory is the directory to copy, and /destination_directory is the target location.

4. How do you change the permissions of a file to be readable and writable only by the owner?

File permissions in Unix determine who can read, write, or execute a file. Use the chmod command to change permissions. To make a file readable and writable only by the owner, set permissions to 600.

Example:

chmod 600 filename

This means the owner has read and write permissions, while the group and others have none.

5. Provide a command to find the disk usage of a specific directory.

To find the disk usage of a directory, use the du command.

Example:

du -sh /path/to/directory

-s provides a summary, and -h makes the output human-readable.

6. How can you display the current environment variables?

To display current environment variables, use the printenv or env command.

Example:

printenv

To display a specific variable:

echo $HOME

7. How would you schedule a job to run at a specific time using Unix commands?

To schedule a job at a specific time, use the cron utility. The crontab command edits the cron table, which contains scheduled jobs.

Example to run a job at 3:30 PM daily:

30 15 * * * /path/to/your/script.sh

Edit the crontab file with:

crontab -e

8. How can you monitor real-time system performance metrics like CPU and memory usage?

To monitor real-time system performance metrics like CPU and memory usage, use commands like top, htop, vmstat, iostat, and free.

Examples:

top
htop
vmstat 1
iostat 1
free -m

Each provides different views of system performance.

9. Write a command to sort the contents of a file alphabetically.

To sort a file’s contents alphabetically, use the sort command.

Example:

sort filename.txt

This sorts the contents of filename.txt alphabetically.

10. Write a script to check if a service is running and restart it if it is not.

To check if a service is running and restart it if not, use a shell script with systemctl.

Example:

#!/bin/bash

SERVICE_NAME="your_service_name"

if systemctl is-active --quiet $SERVICE_NAME; then
    echo "$SERVICE_NAME is running."
else
    echo "$SERVICE_NAME is not running. Restarting..."
    systemctl restart $SERVICE_NAME
fi

11. Provide an example of using awk to process text data.

awk is a text processing tool for pattern scanning and processing.

Example:

echo "Name Age\nAlice 30\nBob 25\nCharlie 35" | awk '{if ($2 > 30) print $1}'

This prints names of individuals older than 30.

12. Describe how to use pipes and redirection.

Pipes and redirection manage data flow between commands and files. Pipes connect command outputs to inputs, while redirection changes standard input, output, and error.

Examples:

ls -l | grep "txt"
echo "Hello, world!" > output.txt
echo "Hello, again!" >> output.txt
sort < input.txt
ls non_existent_file 2> error.log
command &> output_and_error.log

13. Provide an example of using sed for stream editing.

sed is a stream editor for parsing and transforming text.

Example:

echo "Hello World" | sed 's/World/Unix/'

This substitutes “World” with “Unix”.

14. How can you search for a process by name and kill it?

To search for a process by name and kill it, use ps, grep, and kill.

Example:

ps aux | grep process_name
kill -9 PID

ps aux lists processes, grep filters by name, and kill -9 PID terminates the process.

15. Write a command to archive and compress a directory.

To archive and compress a directory, use the tar command with options for compression.

Example:

tar -czvf archive_name.tar.gz /path/to/directory

-c creates an archive, -z compresses it, -v shows progress, and -f specifies the archive name.

Previous

15 Binary Tree Interview Questions and Answers

Back to Interview
Next

10 SQL vs NoSQL Interview Questions and Answers