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.
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.
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.
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
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.
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.
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.
To display current environment variables, use the printenv
or env
command.
Example:
printenv
To display a specific variable:
echo $HOME
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
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.
To sort a file’s contents alphabetically, use the sort
command.
Example:
sort filename.txt
This sorts the contents of filename.txt
alphabetically.
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
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.
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
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”.
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.
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.