Unix Interview Questions and Answers for freshers
-
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.
-
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.
-
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.
-
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.
-
Name some popular Unix shells.
- Answer: Bash (Bourne Again Shell), Zsh (Z Shell), Ksh (Korn Shell), Csh (C Shell).
-
What is the command to list files and directories?
- Answer:
ls
- Answer:
-
How do you list hidden files using ls?
- Answer:
ls -a
- Answer:
-
What does the command `pwd` do?
- Answer:
pwd
(print working directory) displays the current working directory.
- Answer:
-
How do you create a new directory?
- Answer:
mkdir [directory_name]
- Answer:
-
How do you remove a directory?
- Answer:
rmdir [directory_name]
(for empty directories) orrm -r [directory_name]
(for non-empty directories, use with caution).
- Answer:
-
What is the command to change directories?
- Answer:
cd [directory_name]
- Answer:
-
How do you copy a file?
- Answer:
cp [source_file] [destination_file]
- Answer:
-
How do you move or rename a file?
- Answer:
mv [source_file] [destination_file]
- Answer:
-
How do you remove a file?
- Answer:
rm [file_name]
- Answer:
-
What is the command to display the contents of a file?
- Answer:
cat [file_name]
orless [file_name]
(for large files).
- Answer:
-
What does `grep` do?
- Answer:
grep
searches for patterns within files.
- Answer:
-
How would you search for the word "example" in a file named "my_file.txt"?
- Answer:
grep "example" my_file.txt
- Answer:
-
What is the purpose of the `find` command?
- Answer:
find
searches for files and directories based on specified criteria.
- Answer:
-
How would you find all files ending with ".txt" in the current directory?
- Answer:
find . -name "*.txt"
- Answer:
-
What does `chmod` do?
- Answer:
chmod
changes the permissions of a file or directory.
- Answer:
-
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).
- Answer:
-
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.
-
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").
- Answer:
-
What is input/output redirection?
- Answer: It redirects the standard input, output, or error streams of a command to a file or another command.
-
How would you redirect the output of a command to a file?
- Answer:
command > output.txt
- Answer:
-
How would you append the output of a command to a file?
- Answer:
command >> output.txt
- Answer:
-
What is the command to display the current date and time?
- Answer:
date
- Answer:
-
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).
- Answer:
-
What is `wc` command used for?
- Answer:
wc
counts lines, words, and characters in a file.
- Answer:
-
What is `sort` command used for?
- Answer:
sort
sorts lines of text files.
- Answer:
-
What is `uniq` command used for?
- Answer:
uniq
filters out repeated consecutive lines from a sorted file.
- Answer:
-
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.
-
How do you create a symbolic link?
- Answer:
ln -s [original_file] [link_name]
- Answer:
-
What is the purpose of the `chown` command?
- Answer:
chown
changes the owner and/or group of a file or directory.
- Answer:
-
What is the purpose of the `chgrp` command?
- Answer:
chgrp
changes the group of a file or directory.
- Answer:
-
What is a regular expression?
- Answer: A regular expression (regex or regexp) is a sequence of characters that define a search pattern.
-
Give an example of a simple regular expression.
- Answer:
a.*b
(matches "a" followed by any characters, followed by "b").
- Answer:
-
What is the `tar` command used for?
- Answer:
tar
is used to create and manipulate archive files (e.g., .tar, .tar.gz, .tar.bz2).
- Answer:
-
How would you create a tar archive of a directory?
- Answer:
tar -czvf myarchive.tar.gz mydirectory
- Answer:
-
How would you extract a tar archive?
- Answer:
tar -xzvf myarchive.tar.gz
- Answer:
-
What is the `gzip` command used for?
- Answer:
gzip
compresses and decompresses files using the gzip algorithm.
- Answer:
-
How would you compress a file using gzip?
- Answer:
gzip myfile.txt
- Answer:
-
How would you decompress a gzip file?
- Answer:
gunzip myfile.txt.gz
- Answer:
-
What is the `bzip2` command used for?
- Answer:
bzip2
compresses and decompresses files using the bzip2 algorithm (generally offers higher compression than gzip).
- Answer:
-
What are environment variables?
- Answer: Environment variables are dynamic named values that can affect the behavior of programs and the shell.
-
How do you view environment variables?
- Answer:
env
orprintenv
- Answer:
-
How do you set an environment variable?
- Answer:
export MY_VARIABLE="value"
- Answer:
-
What is the `echo` command used for?
- Answer:
echo
displays text on the terminal.
- Answer:
-
What is a process ID (PID)?
- Answer: A unique numerical identifier assigned to each running process by the operating system.
-
How do you find the PID of a running process?
- Answer:
ps aux | grep [process_name]
- Answer:
-
What is the `kill` command used for?
- Answer:
kill
sends signals to processes, often used to terminate them.
- Answer:
-
How would you kill a process with PID 1234?
- Answer:
kill 1234
- Answer:
-
What is the difference between `kill` and `kill -9`?
- Answer:
kill
sends the TERM signal (allowing the process to clean up), whilekill -9
sends the SIGKILL signal (forcing immediate termination without cleanup).
- Answer:
-
What is the `top` command used for?
- Answer:
top
displays dynamic real-time information about running processes.
- Answer:
-
What is the `ps` command used for?
- Answer:
ps
displays information about running processes (a snapshot).
- Answer:
-
What is the `head` command used for?
- Answer:
head
displays the first few lines of a file (default is 10).
- Answer:
-
What is the `tail` command used for?
- Answer:
tail
displays the last few lines of a file (default is 10).
- Answer:
-
How would you display the last 20 lines of a log file?
- Answer:
tail -n 20 logfile.txt
- Answer:
-
What is the `diff` command used for?
- Answer:
diff
compares two files and shows the differences between them.
- Answer:
-
What is the `cut` command used for?
- Answer:
cut
extracts sections from each line of files.
- Answer:
-
What is the `awk` command used for?
- Answer:
awk
is a powerful text processing tool that can perform pattern scanning and text manipulation.
- Answer:
-
What is the `sed` command used for?
- Answer:
sed
is a stream editor for filtering and transforming text.
- Answer:
-
What is a shell script?
- Answer: A shell script is a program written in a shell scripting language (like Bash) that automates tasks.
-
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.
- Answer: The shebang line (e.g.,
-
How do you make a shell script executable?
- Answer:
chmod +x myscript.sh
- Answer:
-
What is the `cron` utility used for?
- Answer:
cron
schedules tasks to run at specific times or intervals.
- Answer:
-
What is a crontab?
- Answer: A crontab is a file containing a list of scheduled tasks for a user.
-
How do you edit your crontab?
- Answer:
crontab -e
- Answer:
-
What are some common crontab entries? (Give one example)
- Answer:
0 0 * * * /path/to/myscript.sh
(runs myscript.sh at midnight every day).
- Answer:
-
What is the `umask` command used for?
- Answer:
umask
sets the default permissions for newly created files and directories.
- Answer:
-
What is the `whoami` command used for?
- Answer:
whoami
displays the current user's username.
- Answer:
-
What is the `finger` command used for?
- Answer:
finger
displays information about users on the system (information available depends on system configuration).
- Answer:
-
What is the `hostname` command used for?
- Answer:
hostname
displays the system's hostname.
- Answer:
-
What is the `uname` command used for?
- Answer:
uname
prints system information (kernel name, node name, etc.).
- Answer:
-
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.
-
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.
-
What is the difference between `>` and `>>` in redirection?
- Answer: `>` overwrites the file, `>>` appends to the file.
-
How do you redirect standard error to a file?
- Answer:
command 2> error.txt
- Answer:
-
How do you redirect both standard output and standard error to a file?
- Answer:
command &> output.txt
orcommand > output.txt 2>&1
- Answer:
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!