Linux Mock Interview questions – Part 1

1. Interviewer: Let’s start with some basics. Could you name a few essential Linux commands and explain their purposes?

Candidate: Sure! Some fundamental Linux commands are:

  1. `ls`: It lists the contents of a directory, allowing you to see files and subdirectories.
  2. `cd`: It stands for “change directory” and is used to navigate between directories.
  3. `pwd`: It prints the current working directory, showing you where you are in the file system.
  4. `mkdir`: It creates a new directory.
  5. `rm`: It removes files or directories (with the `-r` option for recursive deletion).
  6. `cp`: It copies files or directories.
  7. `mv`: It moves or renames files or directories.
  8. `touch`: It creates an empty file or updates the timestamp of an existing file.

 

2. Interviewer: How would you navigate to the root directory from your current location?

Candidate: To navigate to the root directory from anywhere in the file system, I would use the `cd` command followed by a forward slash (/). So, the command would be `cd /`

 

3. Interviewer: Let’s say you need to move to your home directory from the current location. How would you do that?

Candidate: To move to my home directory from anywhere in the file system, I can use the tilde (~) character, which represents the home directory. So, the command would be `cd ~`.

 

4. Interviewer: How would you go back one directory level (up) from your current location?

Candidate: To go back one directory level, I would use the command `cd ..`.

 

5. Interviewer:  What do file permissions consist of, and how would you change them?

Candidate: File permissions in Linux consist of three sets of permissions: read (r), write (w), and execute (x). Each set applies to three categories of users: the file owner, the group the file belongs to, and other users.

To change file permissions, I would use the `chmod` command followed by the appropriate numeric code or symbolic representation. For example, to give read and write permissions to the owner of a file, I would use `chmod u+rw filename`.

 

6. Interviewer: How you would find files with a specific name using the command line?

Candidate: To find files with a specific name, I would use the `find` command along with the name and location I want to search. For example, to find all files named “example.txt” in the current directory and its subdirectories, I would use `find . -name “example.txt”`.

 

7. Interviewer: How would you view the content of a text file without opening it in an editor?

Candidate: To view the content of a text file without opening it in an editor, I would use the `cat` command. For example, `cat filename.txt` will display the contents of “filename.txt” directly in the terminal.

 

8. Interviewer: What if you wanted to display the first few lines of a text file instead of the entire content? How would you do that?

Candidate: If I want to display the first few lines of a text file, I can use the `head` command. By default, `head` displays the first 10 lines of a file, but I can specify a different number of lines with the `-n` option. For example, `head -n 5 filename.txt` will display the first 5 lines of “filename.txt.”

 

9. Interviewer: How would you display the last few lines of a text file?

Candidate: To display the last few lines of a text file, I would use the `tail` command. Similar to `head`, `tail` also displays the last 10 lines of a file by default, but I can use the `-n` option to specify a different number of lines. For example, `tail -n 8 filename.txt` will display the last 8 lines of “filename.txt.”

 

10. Interviewer: How would you create a copy of a directory and all its contents?

Candidate: To create a copy of a directory and all its contents, I would use the `cp` command with the `-r` option, which stands for “recursive.” For example, `cp -r sourcedir/ destinationdir/` will copy “sourcedir” and all its contents to “destinationdir.”

 

11. Interviewer: Let’s say you need to delete a directory, along with all the files and subdirectories it contains. How would you achieve this?

Candidate: To delete a directory and all its contents, I would use the `rm` command with the `-r` and `-f` options. The `-r` option ensures that the removal is done recursively, and the `-f` option suppresses any confirmation prompts. For example, `rm -rf directoryname/` will delete “directoryname” and everything inside it.

 

12. Interviewer: How would you check your current user’s privileges or display which user you are logged in as?

Candidate: To check my current user’s privileges or display the user I’m logged in as, I would use the `whoami` command. It simply returns the name of the currently logged-in user.

 

13. Interviewer: Can you explain what the `grep` command does in Linux and how you would use it?

Candidate: The `grep` command is used to search for patterns or specific strings within files or streams of text. It allows us to filter and extract relevant information from large amounts of text data. To use `grep`, I would specify the pattern I want to search for, followed by the file(s) or input stream to search within. For example, `grep “error” logfile.txt` would search for the word “error” in the file “logfile.txt.”

 

14. Interviewer: Let’s discuss file compression. How would you create a compressed archive file (tarball) of a directory, and how would you extract its contents?

Candidate: To create a compressed archive file of a directory, I would use the `tar` command with the `-czvf` options. The `-c` option stands for “create,” the `-z` option is for compressing with gzip, the `-v` option enables verbose mode (optional, for displaying progress), and the `-f` option specifies the filename of the resulting tarball. For example, to create a tarball named “archive.tar.gz” of a directory called “myfiles,” I would use: `tar -czvf archive.tar.gz myfiles/`.

To extract the contents of a tarball, I would use the `tar` command with the `-x` option. For example, to extract the contents of “archive.tar.gz” into the current directory, I would use: `tar -xzvf archive.tar.gz`.

 

15. Interviewer: Let’s move on to managing processes in Linux. How would you list all the running processes on your system, and how would you terminate a specific process?

Candidate: To list all the running processes on the system, I would use the `ps` command with the `aux` options. The `a` option shows processes for all users, the `u` option displays detailed information, and the `x` option includes processes without a controlling terminal. The command would be: `ps aux`.

To terminate a specific process, I would first identify its Process ID (PID) from the `ps` output. Then, I would use the `kill` command followed by the PID. For example, to terminate a process with PID 12345, I would use: `kill 12345`.

 

16. Interviewer: Now, let’s touch on file ownership and permissions. How would you change the owner of a file, and how would you change the permissions to make a file executable?

Candidate: To change the owner of a file, I would use the `chown` command followed by the new owner’s username and the filename. For example, to change the owner of “file.txt” to the user “newuser,” I would use: `chown newuser file.txt`.

To make a file executable, I would use the `chmod` command with the `+x` option. This adds the execute permission to the file. For example, `chmod +x script.sh` would make “script.sh” executable.

 

17. Interviewer: Let’s move on to networking. How would you check the network connectivity between your Linux system and a remote server?

Candidate: To check network connectivity between my Linux system and a remote server, I would use the `ping` command followed by the IP address or domain name of the remote server. For example, to ping a server with IP address “192.168.1.100,” I would use: `ping 192.168.1.100`. This command sends ICMP packets to the remote server and shows the response time and status.

 

18. Interviewer: Let’s discuss how you would transfer files between your local machine and a remote server securely. What tools or commands would you use?

Candidate: To securely transfer files between my local machine and a remote server, I would use the `scp` (secure copy) command. It uses SSH for encryption and secure data transfer. The basic syntax for using `scp` is: `scp [source] [destination]`. For example, to copy a file named “data.txt” from my local machine to the remote server, I would use: `scp data.txt username@remote_server_ip:/path/to/destination/`.

 

19.Interviewer: Let’s explore disk usage and how to check the free space on a Linux system. How would you do that?

Candidate: To check disk usage and the free space on a Linux system, I would use the `df` command. By default, it shows the information for all mounted filesystems. To display human-readable sizes and get a more concise output, I would use the `-h` option. The command would be: `df -h`.

 

20. Interviewer: Let’s talk about system updates and package management. How would you update the packages on a Debian-based Linux distribution, such as Ubuntu?

Candidate: To update packages on a Debian-based Linux distribution, I would use the `apt` package manager. The process usually involves two steps. First, I would update the package lists to get the latest information about available updates using the command: `sudo apt update`. Then, I would perform the actual upgrade of the installed packages using: `sudo apt upgrade`. This command may prompt for confirmation before proceeding.

 

21. Interviewer: Let’s delve into process management a bit more. How would you run a command in the background and also bring it back to the foreground if needed?

Candidate: To run a command in the background, I would add an ampersand (&) at the end of the command. For example, to run a script named “myscript.sh” in the background, I would use: `./myscript.sh &`. This way, the command will execute in the background, and I can continue to use the terminal.

To bring a background process to the foreground, I would use the `fg` command followed by the job number. The job number can be found using the `jobs` command. For instance, if I have only one background job running, I can bring it to the foreground with: `fg %1`.

 

22. Interviewer: Now, let’s discuss package installation and removal. How would you install a package on a Debian-based Linux distribution using the `apt` package manager?

Candidate: To install a package on a Debian-based Linux distribution, such as Ubuntu, I would use the `apt` package manager. The command to install a package is: `sudo apt install package_name`. For example, to install the “git” package, I would use: `sudo apt install git`. This command will download and install the specified package and its dependencies.

 

23. Interviewer:  What about removing a package using the `apt` package manager?

Candidate: To remove a package using the `apt` package manager, I would use the `sudo apt remove package_name` command. For example, to remove the “vim” package, I would use: `sudo apt remove vim`. This command will remove the package from the system, but it will not remove configuration files associated with the package.

If I want to completely remove a package, including its configuration files, I can use the `–purge` option. The command would be: `sudo apt purge package_name`.

Leave a Comment

Your email address will not be published. Required fields are marked *

15 − 9 =