48 Basic Linux commands and questions

Linux is not just an OS instead of a whole computing world powering millions of servers, so if you learning Linux, then here a few common questions and their answers you should know related to Linux.

The below given 50 Linux questions that could be asked in an interview or just have a look at them if you want to increase your basic knowledge.

Contents

Common Linux questions and commands

 1. What is Linux?

It is an open-source Unix-like operating system that is meant to use, develop, and distribute freely without any restrictions. Its kernel was first released by Linas Benardict Torvalds in 1991. It was mainly inspired by the ideas of Minix and Unix. A multi-user, multi-tasking, multi-thread, and multi-CPU operating system based on POSIX and Unix. It can run major Unix tool software, applications, and network protocols. It supports 32-bit and 64-bit hardware. Right now, there are hundreds of Linux operating systems with a different package manager, Interface, and working, however, the kernel will be from the same source. Debian, RedHat, CentOS, Ubuntu are some common are popular examples of Linux distros.

Note: Know more about it on Wikipedia

2. What are the main functions of the Linux kernel

  • System memory management
  • Software program management
  • Hardware equipment management
  • File system management

3. Interactive mode

Console terminal, graphical terminal

4. Start the shell

The GNU bash shell can provide interactive access to the Linux system. As a normal program, it is usually started when the user logs in to the terminal. The shell started by the system upon login depends on the configuration of the user account.

5. bash manual

Most Linux distributions come with online manuals for finding shell commands and other GNU tool information. The man command is used to access the manual page on the Linux system. When viewing the manual with the man command, use a paging program to be realistic.

6. Where are you after logging in?

After logging in, your location is in your home directory.

7. Absolute file path? Relative file path? A shortcut?

Absolute file path: It describes the exact location of the directory in the virtual directory structure, starting with the virtual directory and the directory, which is equivalent to the full name of the directory.

Start with a forward slash (/), such as /usr/local.

Relative file path: It allows the user to execute a target file path based on the current location.

For example: currently in /usr/local

➜ ls
Frameworks bin        go         lib        sbin       var
etc        include    opt        share
➜ cd go

Shortcut (used in the relative path):

For example to get back to parent directory type cd ..

8. Get lost, where is my current location?

pwd the command displays the current directory.

[root@h2s]# pwd

/usr/local

9. How to switch directories?

Syntax: cd destination

destination: relative file path or absolute file path

For example:

cd /etc/perl

The above command takes you directly into Perl directory resides inside the /etc directory.

You can jump to any directory that exists.

10. How to view the files in the directory?

The ls command displays the files and directories available in the current directory in the most basic form:

➜ls
Frameworks bin go lib sbin var
Cellar etc include opt share

It can be seen that the default is displayed in alphabetical order…

Distinguish which files are directories? Recursive search?

Generally speaking, the ls command displays different colors to distinguish different file types such as folders, files, and script. If no color plug-in is installed, ls -F can be used to distinguish which are directories (directory with /) and which are files (file without /)

ls -R recursively displays all files resides in the directory and subdirectories, the more directories, the more output but full information.

How to view the files in the directory

11. Create a file? Create a directory? Bulk creation?

Syntax to create a file: touch filename

To create files in batches: touch filename1 filename2

Example:

➜ touch a
➜ ls
a
➜ touch b c
➜ test ls
a b c

Create directory: mkdir directory name

Create directories in batches: mkdir directory name1 directory name2…

➜ mkdir aa
➜ mkdir bb cc
➜ ls
aa bb  cc
➜ ls -F
aa/ bb/ cc/

12. Delete file, Force delete, and Recursive deletion?

Syntax: rm filename 

Flags to be used with the rm command.

-i asks whether to delete

-r deletes recursively (whole directory or folder)

-f forcibly deletes.

Note: rm alone cannot delete a directory with files, it needs to be deleted recursively.

➜ rm jdk
rm: jdk: is a directory

In the above example JDK is directory thus to remove it we need to use -r flag with it.

➜ rm -r jdk

rm -i Ask about deletion, suggest that you usually delete multi-i, and make sure to delete.

➜ touch demo
➜ rm -i demo
remove demo? n

rm -rf will be directly deleted, there is no warning message, use must be careful.

13. Automatic tab completion?

Sometimes the name of the file is very long, and it is easy to misspell it. Even if the spelling is correct, it is a waste of time. Thus, type the initial letter of a file or folder and press the TAB key and it will autocomplete the match file name. If there are multiple files with the same initials then it will print them all on the screen.

➜ls java

javaxyzabc

For example, in the above command we have written java and then pressed the TAB key rest of the file name will be completed automatically, isn’t it much more convenient.

14. Copy files

Syntax: cp source target

If the target does not exist, it will be created directly. If it exists, it will not remind you whether you need to overwrite by default. If you need a reminder, you can add -i, you will be asked if you want to overwrite, n or y.

➜ cp a c
➜ cp -i a c
overwrite c? (y/n [n]) y

In the above example, we are copying a file a to c, this means if there is no file with name ‘c’, ti will be created with all content of ‘a’. Whereas, if there is some content in the file ‘c’, then the command will ask to overwrite.

You can use the above command to copy files from one folder to another by giving a full absolute path.

15. Rename the file? Move files?

Syntax: mv source target

Rename:

➜ touch java
➜ ls
java

To remain java

➜ mv java java-new
➜ ls
java-new

Move files:

Create a new JDK directory and move the java-new file to the JDK directory.

➜ mkdir jdk
➜ mv java-new jdk
➜ ls -R
jdk

16. What is a linked file?

If you need to maintain two or more copies of the same file on the system, in addition to keeping multiple separate copies of the physical file. You can also save a copy of a physical file and multiple virtual copies, this virtual copy is called a link.

17. View file type? Character encoding?

Syntax: file destination

Example:

➜ file tomcat
tomcat: ASCII text

It can be seen that the file command can display the file type text and character encoding ASCII.

18. View the entire file?

Also, know how to show line number according to the text? Show line number without text?

Syntax: cat destination

-n displays the line number

-b displays the line number with text.

Note: (The line number is not displayed by default)

➜ cat -n hosts
1 text
2 text
 
➜ cat -b hosts
1 text 123445...
2 text xyzsee ...

command to view all file content

19. View file content without editing or opening it

Syntax: tail destination

By default, the last 10 lines of the file will be displayed.

-n Line number, display the last n lines.

➜ tail -n 2 hosts

Syntax: head destination

By default, the first 10 lines of the file will be displayed.

-n line number, display the first n lines.

➜ head -n 2 hosts

20. Sort data by numbers or months

By default, the data display of the file is displayed in the original order. The sort command can sort the data in the text file. Sort will treat data as characters by default.

Syntax: sort destination

sort -n  So when sorting numbers, you need to use -n, which means that the current sort is the number.

sort -M For example, Jan, Feb, Mar, if you want it to be sorted by month, adding -M will sort by month size.

21. Find matching data? Reverse search?

Syntax: grep [options] pattern [file]

This command will find the line of the string that matches the execution pattern and output it.

➜ grep start xyz

-v reverse search

➜ grep -v start xyz

-n display line number

-c displays the number of matching lines

22. What are the compression tools?

Tools or software used to compress files and folders using the command line or GUI. Such tools help in reducing the file size and keep them intact in one place. ZIP, Gzip, bzip2 & compress.

23. How to compress files? How to unzip the file?

Let’s assume that we have a file in the .gz format, therefore to compress it:

Compression syntax: gzip filename

➜ gzip xyz
➜ ls
xyz.gz

Decompression syntax: gunzip destination

➜ apache gunzip xyz.gz
➜ apache ls
xyz

24. The widely used archive data method of Linux?

Although the zip command can compress and decompress a single file, the tar command is more commonly used for archiving.

Syntax: tar  [options] dir1 dir2

Create an archive

➜ tar -cvf zyz.tar

To unarchive

➜ tar zxvf zyz.tar

25. How to view the command history?

The history command can display the history of the commands you use.

Just type:

history

26. Check the existing alias? Create your own alias?

Alias are shortcuts to any existing command to increase productivity. You can also create a custom alias.

To check the existing alias on your system type:

alisal -p

To create a new one, for example, you want to switch to web directory i.e /var/www/html but without typing the path, thus we can create shortcut it.

alias k="cd /var/www/html"

The above command will create a shortcut with ‘K’ for web directory means, just type k and you will be switched to HTML directory.

But the above declaration of shortcut or alias will be a temporary one, the moment you end the session this will gone.

To make a permanent one, we need to declare it in Shell configuration files that are:

  • bash ~/.bashrc
  • ZSH – ~/.zshrc
  • Fish – ~/.config/fish/config.fish

For example for bash:

vi ~/.bashrc

And at the end enter the alias you want to create, if we take the above example then it will be alias k="cd /var/www/html"

Save and exit the file.

reload the bash:

source ~/.bashrc

Now, the created alias will be a permanent one.

27. What are the environmental variables?

The bash shell uses a feature called environment variables to store information about the shell session and working environment. This feature allows you to store data in memory so that scripts running in programs or shells can easily access them. This is also a convenient way to store persistent data.

In the bash shell, environment variables are divided into two categories:

Global variables: visible to the shell session and all generated subshells.

Local variables: Only visible to the shell that created them.

28. View group information? How to create a group? Delete group?

To view all groups

cat /etc/group

To add

groupadd xyz

For deleting

groupdel xyz

29. File descriptors? What does each descriptor mean?

# ls -l  

1. File type:

-Representative documents
d stands for directory
l stands for link
c stands for character equipment
b stands for block device
n stands for network equipment

2. Access permission symbol:

r means the object is readable
w means the object is writable
x means the object is executable

If there is no permission, a single dashed line will appear at the permission level.

3. These three sets of permissions correspond to the three security levels of the object:

Owner of the object
Object’s Genre
Other users of the system

30. Modify permissions?

Syntax: chmod options mode file

For example, to give executable permissions to the file:

# chmod +x filename

 31. How to execute executable files?

# sh executable filename

sh file.sh

or simply use

./file.sh

Replace file.sh with the real filename.

32. List installed packages? install software? Update software? Uninstall?

List installed packages:

# For Redhat, CentOS or other similar OS

yum list installed
or 
dnf list installed

Install the software:

yum install package_name
or 
dnf install package_name

Update software:

yum update package_name
or 
dnf update package_name

Uninstall the software:

yum remove package_name 
or
dnf remove package_name

If you do not want to keep data files and configuration files

Can execute:

yum erase package_name

#For Ubuntu or Debian systems just replace the yum with apt

 

33. The usual way to install source code?

tar -zxvf xx.gz // 
cd xx
./configure
make
make install

Note: replace xx.gz with the archive file of the source code you want to compile and install.

34. How many operation modes of vim editor? Basic operation?

Operating mode:

  • Normal mode: read and delete text but can’t insert any character.
  • Insert mode – can be activated by pressing the INSERT key on the keyboard this will you to insert text to file.

Basic operation:

  • h: Move one character to the left.
  • j: Move down one line (the next line in the text).
  • k: Move up one line (the previous line in the text).
  • l: Move one character to the right.

vim provides some commands that can increase the speed of movement:

  • PageDown (or Ctrl+F): turn down one screen
  • PageUp (or Ctrl+B): Turn up one screen.
  • G: Move to the last line of the buffer.
  • num G: Move to the num line in the buffer.
  • gg: Move to the first line of the buffer.

Exit vim:

Press Esc key before using any of the following Vim editor’s Exit command:

  • :q     //If the buffer data is not modified, exit.
  • :q!   //Cancel all modifications to the buffer data and exit.
  • :w   //filename: Save the file to another file.
  • :wq  //Save the buffer data to a file and exit.

35. Check how much disk space is left on the device?

df    //the function can be used to view all the usage of the disks.

Flags:

df -m  to see detail in megabytes

df -G instead of g bytes

To know more about it type:

df –help

By default, du displays the disk usage of all files, directories, and subdirectories in the current directory.

36. The default process information display?

ps  //It can output a lot of information about all programs running on the system.

By default, the ps value displays the current user’s process running under the current console.

37. Real-time monitoring process

Compared with ps, the top can monitor process information in real-time.

Type:

top

See all real time process on Linux with top command

The average load has 3 values: the average load for the last 1 minute, the last 5 minutes, and the last 15 minutes. The larger the value, the higher the system load. Due to the short-term sudden activity of the process, it is also common to see a high load value at the last minute, but if the average load in the past 15 minutes is very high, it may indicate a problem with the system.

38. How to interrupt a process?

In a terminal,

Ctrl + c

Through this command many (not all) command-line programs can be interrupted. for example, if you are installing something then that can be stopped using this.

39. How to put a process to run in the background?

./demo.sh &

In the above command demo.sh is an example of some file name.

At this time, the process cannot be interrupted by Ctrl + c.

40. How to kill or stop a process?

The kill command is used to send signals to the program. If no signal is specified, the TERM (termination) signal is sent by default.

Syntax: kill [-signal] PID…

For example, we want to kill a process with PID 4567, thus the command will be:

sudo kill -9 4567

41. What is the command to verify network linkability?

ping        //This ping command sends a special network packet (called IMCP ECHO REQUEST) to a designated host. Most network devices that receive this packet will reply to it to allow network connection verification.

Example: ping google.com

Once started, ping will continue to send packets at a specific time (default 1 second).

42. Check if a certain port is occupied?

netstat -ntulp|grep 8080

replace 8080 with the port that needs to be checked.

Parameter Description:

-t (tcp) Display only tcp related options
-u (udp) Display only udp related options
-n refuse to display the alias, can display all the numbers converted into numbers
-l List only the service status in Listen
-p displays the name of the program that established the relevant link

43. How to find matching files? Based on file attributes?

The find program can search a given directory (and its subdirectories) based on various attributes to find files.

The simplest use of the find command is to search one or more directories.

Ordinary search, search by name:

find -name Xyz

Find out the total number of files in a directory.

find |wc -l

You can also mention the path of some particular directory to find out the total number of files inside it.

Example: To see the total number of files under /etc/perl

find /etc/perl|wc -l

According to the file type:

find -type d | wc -l

or

find /etc/perl -type d | wc -l

You can replace the d flag that represents directory with others such as

b   //block device file

c  //character device file

f  //ordinary file

l // symbolic link

44. How to check the current hostname? How to modify?

hostname   //to check

To change:

1. Command

 hostnamectl set-hostname xyz

2. Second, modify the configuration file: /etc/hostname

 vim /etc/hostname

45. ​​How to write a rule to deny an IP to access port 8080 of this machine?

In the below command we are disabling the access of outside work to port number 8080. You can change the port number to others that you want to reject.

iptables -I INPUT -s ip -p tcp —dport 8080 -j REJECT

To Allow the same port instead of type REJECT use the ACCEPT function.

iptables -I INPUT -s ip -p tcp —dport 8080 -j REJECT

47. Which file contains the mapping between hostname and IP?

/etc/hosts

48. How to print only the 5th line with sed in a file?  Replace string?

Example: Only print line 5:

➜ sed -n "5p" demo

Delete the first line:

cat demo

Replace string:
➜  cat demo

The lion said to the bear, “I caught this kid first, and so this is mine.”

So, we want to replace e this with that.

➜ sed 's#this#that#g' demo

The lion said to the bear, “I caught that kid first, and so that is mine.”

49. How to use awk to view the penultimate third field of the second line?

➜ awk 'NR==3{print $(NF-2)}' demo  // demo is the file name

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.