Introduction
Linux is an open-source Unix-like operating system-based family on the Linux kernel, and the OS kernel was first published on 17 September 1991 by Linus Torvalds. Typically, Linux is packaged as the Linux distribution, which contains the supporting libraries and system software and kernel, several of which are offered by the GNU Project. Several Linux distributions use the term “Linux” in the title, but the Free Software Foundation uses the “GNU/Linux” title to focus on the necessity of GNU software, causing a few controversies.
Famous Linux distributions are Ubuntu, Fedora Linux, and Debian, the latter of which is composed of several different modifications and distributions, including Xubuntu and Lubuntu. Commercial distributions are SUSE Linux Enterprise and Red Hat Enterprise Linux. Desktop distributions of Linux are windowing systems like Wayland or X11 and desktop environments like KDE Plasma and GNOME.
Dive into a comprehensive compilation of Linux Commands Interview Questions & Answers, designed to sharpen your command-line proficiency. Whether you’re a Linux enthusiast, a system administrator, or preparing for a technical interview, this resource covers a diverse array of command-line topics. Explore essential commands, shell scripting, file manipulation, and system administration tasks through detailed answers that provide in-depth insights.
Linux Commands Interview Questions
1. Explain about chown command ?
Chown command: The command “chown” stands for change file owner and Group. This command is used to change the ownership of one or more files or folders for a specified user or group.
syntax:
$ chown [OPTION]… [OWNER][:[GROUP]] FILE…
Or
$ chown [OPTION]… –reference=RFILE FILE…
2. What is the cp command in Linux ?
cp command: cp command is used to copy files and directories. It is also used to backup files or directories.
Syntax:
$ cp filename
3. How to remove a file or directory from the system in Linux ?
rm command: The rm command is used to remove the directory or file specified on the command line. You need to be careful while removing any file or directory.
Syntax:
rm filename—
Command | Description |
rm filename | Removes single file. |
rm filename1, filename2, filename 3 | Removes multiple files. |
rm * .pdf | Removes all pdf files in the current directory. |
rm -i filename(s) | -i mean to confirm before deleting the file |
rm -f filename(s) | Removes files without prompting |
rm -fv *.txt | Remove all .txt files in the current directory without prompting |
4. What is mkdir in Linux ?
mkdir, command allows users to create directories in Linux. Users can create multiple directories at once and can set the permissions to the directories.
Syntax:
mkdir [options...] [directories ...]
Option | Description |
Directory | name of the directory to be created |
-m=mode, –mode=mode | to set a file mode (permissions, etc.) for the created directories |
-p, –parents | create parent directories |
–v, –verbose | Verbose output. Print a message for the created directory. |
–Z= context, –context=context | If you are using SELinux, this option sets the security context of each created directory to context. |
–help | shows help message and exit |
–version | It shows version information and exit |
5. Explain rmdir command in Linux ?
The rmdir is used to remove each directory specified on the command line.
Syntax:
rmdir [-p] [-v | –verbose] [–ignore-fail-on-non-empty] directories
6. How to exit from vi editors ?
We can use two commands to exit from the vi editor. They are
- Wq: wq command saves the current work and exits from the vi editor.
- q!: q! The command does not save the current work, but it exits from the vi editor.
7. How to delete information from a file in vi ?
The following commands are used to delete information from a file in vi editors.
- Command x deletes the current character.
- Command dd deletes the current line.
8. Enlist some Linux to file content commands ?
File content commands
- head: Display top lines of the file.
- tail: Display the last lines of the file.
- cat: Concatenate more than 2 files.
- more: Displays the content in pager form to view in the terminal.
9. What is the ‘ls’ command and what it does ?
It is one of the basic commands in Linux. It is used to list all the files and directories in the file system.zom
Syntax:
ls [OPTIONS] [FILES]
We can use it without passing the arguments, then it will list all the files in the current working directory. Files will be displayed in alphabetical order.
Is
To list the specific directory files use the directory name in the syntax, follow as shown in the below syntax.
ls /etc
We can also pass multiple directories names in the syntax, but separated by space.
ls /etc /var /etc/passwd
10. What is the tail command in Linux ?
The Tail command print the last N number of lines of the given data, it prints 10 lines by default.
Syntax:
tail [OPTION]... [FILE]
Example:
$ cat names.txt
Tableau
sql server
linux administration
Now, check the syntax without giving any option or input.
$ tail state.txt
11. What is grep command in Linux ?
grep command is a filter that is used to the global search for regular expressions.
Syntax:
grep [options] pattern [files]
12. What is ps command in Linux ?
The ps command displays the current process status of the system. And it displays the processes id’s with some other related data also.
Syntax:
Ps [options]
Output:
[root@rhe17~]# ps
PID TTY TIME CMD
12330 pts/0 00:00:00 bash
21621 pts/0 00:00:00 ps
13. What is the ping command ?
Linux ping (Packet Internet Groper) command is a command that is used to check connection status between source and destination. In simple words, this command is used to check whether a network is available and if the host is reachable. It can also be used to troubleshoot different connectivity issues, verify connectivity at an IP -level to a second TCP/IP device, and name resolution. One can use this command to test both the computer name and IP address of the computer.
14. Which command is used to check the default run level ?
The command used to check the default run level is “/etc/inittab”.
15. Which command is used to check the size of file or directory ?
The command used to check the size of the file or directory is “du”. Here “du” stands for disk usage that is used to check information of disk usage of files and directories on a machine. It is also used to display files and directory sizes in a recursive manner.
Example:
$ du -sh /var/log/*
1.8M /var/log/anaconda
384K /var/log/audit
4.0K /var/log/boot.log
0 /var/log/chrony
4.0K /var/log/cron
4.0K /var/log/maillog
64K /var/log/messages
16. Which command is used to count the number of characters in a file ?
The command that is used to count the number of characters in a file in Linux is “wc”. Here “wc” stands for word count. It is used to count the number of lines, words, and characters in a text file.
17. What is the function of grep command ?
Grep (Global regular expression print) is a command that is used to the global search for a string of characters in a specified file. The text search pattern is generally known as a regular expression. It simply makes use of pattern-based searching.
Syntax: grep [options] pattern [files] Example: $ grep -c “linux” interview.txt
The above command will usually print the total count of the word “Linux” in the file “interview.txt”.
18. Explain working of env command ?
“env” command is basically a shell command that is used to print a list of current environmental variables. Here, “env” stands for the environment. It can also run another process in another environment without any modification of the current environment. It allows you to run programs in a modified environment. It is generally used by shell scripts to launch the correct interpreter. It can also be useful for checking if wrong environment variables prevent the application from starting during troubleshooting.
Example:
$env
PHYTHON_PIP_VERSION=9.0L1
HOME=/root
DB_NAME=test
PATH=/usr/local/bin:/usr/local/sbin
LAND=C.UTF=8
PYTHON_VERSION=3.4.6
PWD=/
DB_URI=mongodb://database:27017/test
19. What is the pwd command ?
“pwd” command is basically a command that is used to print the complete path of the current working directory starting from the root (/). Here, “pwd” stands for Print Working Directory. It is considered one of the most basic and most used commands in Linux. This command is usually a built-in shell command and is also available in different shells such as bash, ksh, zsh, bourne shell, etc.
20. Name the command that is used to check all the listening ports and services of your machine ?
# netstat -ntlp
21. Which command is used to check the memory status ?
The command used mostly to check memory status in Linux is “free”. Other commands that can be used are given below:
- “cat” command:It can be used to show or display Linux memory information. (cat/proc/meminfo)
- “vmstat” command:It can be used to report statistics of virtual memory.
- “top” command:It can be used to check the usage of memory.
- “htop” command:It can be used to find the memory load of each process.
22. What is pipe
In Linux, a pipe is basically a form of redirection that is used to send the output of one command to another command for further processing. It simply takes the output from one command and uses it as an input for another. It provides asynchronous execution of commands with help of buffered I/O routines.
23. What are Linux directory commands ?
There are basically five Linux directory commands that are used to work with files and directories as given below:
- pwd: It stands for “print working directory”. This command is generally used to display the path of the present or current working directory.
Syntax:$ pwd - cd: It stands for “change directory”. This command is generally used to change the present working directory to the directory that we want to work on.
Syntax:$ cd <path to new directory> - Is: It stands for “list”. This command is generally used to show the full list of content of files and directories in the present working directory.
Syntax:$ ls - mkdir:It stands for “make directory”. This command generally allows users to create directories in Linux.
Syntax: $ mkdir <name (and path if required) of new directory> - rmdir:It stands for “remove directory”. This command is used to remove/delete each directory that is specified on the command line.
Syntax: $ rmdir <name (and path if required) of directory>
24. What do you mean by unmask ?
Umask, also known as user file-creation mask, is a Linux command that allows you to set up default permissions for new files and folders that you create. In Linux OS, umask command is used to set default file and folder permission. It is also used by other commands in Linux like mkdir, tee, touch, etc. that create files and directories.
Syntax: umask [-p] [-S] [mask]
Where,
[mask]: It represents the permission masks that you are applying.
[-S]: It displays the current mask as a symbolic value.
[-p]: It displays the current mask along with umask command thus allowing it to be copied and pasted as a future input.
25. Name the command used to review boot messages ?
The command that is used to review boot messages is the “dmesg” command.
26. What is the env command in Linux ?
env is a shell command is used to print a list of current environmental variables and it can run another process in another environment without any modification of the current environment.
env [OPTION]... [-] [NAME=VALUE]... [COMMAND [ARG]...]
Options
Tag | Description |
-i, –ignore-environment | Start with an empty environment. |
-0, –null | output line End with a 0 (null) byte rather than a newline. |
-u, –unset=NAME | removes variable NAME from the environment |
–help | Display a help message and exit. |
27. What is the top Command in Linux ?
The top command is used to show the system process and it displays and updates the sorted process information.
28. What is netstat command in Linux ?
netstat command gives various information about the network and routing tables, interface statics and more about the system.
29. What is lsof command in Linux ?
lsof means List of files, we can know which file is opened by which process.
#lsof
30. Explain about chmod command ?
This command is used to change the permission of files and directories. It’s an important command so, I’ll explain it briefly. Therefore, on the whole, there are three ty0pe of permission, read, write and execute and are represented by numbers as shown below.
- 4 – read permission
- 2 – write permission
- 1- execute permission
syntax:
$ chmod options permissions file name
$ chmod [OPTION]... MODE[,MODE]... FILE...
$ chmod [OPTION]... OCTAL-MODE FILE...
$ chmod [OPTION]... --reference=RFILE FILE…
31. What is the cat command in Linux ?
In Linux cat command concatenates and prints files. Users might issue cat to check the contents of your dependencies file or to confirm the version of the application that you have already built locally.
cat Example:
$ cat requirements.txt
flask
flask_pymongo
32. What is the df command in Linux ?
Users can use the df command to troubleshoot disk space issues. Here df stands for display free disk space.
df Command Example:
df -h
33. What is a du command in Linux ?
du command in Linux is used to retrieve more detailed information about which files use the disk space in a directory.
du Command Example:
$ du -sh /var/log/*
1.8M /var/log/anaconda
384K /var/log/audit
4.0K /var/log/boot.log
0 /var/log/chrony
4.0K /var/log/cron
4.0K /var/log/maillog
64K /var/log/messages