Collection of 21 Most Commonly Tested Linux Commands in Interviews

Click the image below to search for the secret code【001】, claim your154-page Linux study notes.

Collection of 21 Most Commonly Tested Linux Commands in Interviews

1. Files and Directories

1. cd command

(Used to switch the current directory; its parameter is the path of the directory to switch to, which can be either an absolute or relative path)

cd /home Enter the ‘/home’ directory

cd .. Return to the previous directory

cd ../.. Return to the two levels up directory

cd Enter the user’s home directory

cd ~user1 Enter the home directory of user1

cd – Return to the last directory

2. pwd command

pwd Displays the working path

3. ls command

(Command to view files and directories, meaning list)

ls View files in the directory

ls -l Display detailed information about files and directories

ls -a List all files, including hidden ones

ls -R List contents of directories recursively (all files under that directory will be displayed)

ls [0-9] Display filenames and directory names that contain numbers

4. cp command

(Used to copy files, meaning copy; it can also copy multiple files to a directory at once)

-a : Copy file attributes along with the file

-p : Copy file attributes, not using the default method, similar to -a, commonly used for backups

-i : If the target file already exists, it will ask for confirmation before overwriting

-r : Recursively copy, used for directory copying

-u : Copy only if the target file differs from the source file

5. mv command

(Used to move files, directories, or rename, meaning move)

-f : Force, if the target file exists, it will overwrite without asking

-i : If the target file exists, it will ask for confirmation before overwriting

-u : Only update if the target file exists and is older than the source file

6. rm command

(Used to delete files or directories, meaning remove)

-f : Force, ignores non-existent files, no warning message will appear

-i : Interactive mode, will ask the user for confirmation before deleting

-r : Recursively delete, commonly used for directory deletion; this is a very dangerous parameter

Collection of 21 Most Commonly Tested Linux Commands in Interviews

2. Viewing File Content

7. cat command

(Used to view the content of text files, followed by the filename to view; usually can be used with pipes and more or less)

cat file1 View the content of the file from the first byte

tac file1 View the content of a file from the last line

cat -n file1 Number the lines of the file

more file1 View the content of a long file

head -n 2 file1 View the first two lines of a file

tail -n 2 file1 View the last two lines of a file

tail -n +1000 file1 Display from line 1000 onward

cat filename | head -n 3000 | tail -n +1000 Display lines 1000 to 3000

cat filename | tail -n +3000 | head -n 1000 Display 1000 lines starting from line 3000 (i.e., displaying lines 3000 to 3999)

Collection of 21 Most Commonly Tested Linux Commands in Interviews

3. File Search

8. find command

find / -name file1 Search for files and directories starting from ‘/’ in the root filesystem

find / -user user1 Search for files and directories belonging to user ‘user1’

find /usr/bin -type f -atime +100 Search for executable files that have not been used in the past 100 days

find /usr/bin -type f -mtime -10 Search for files created or modified in the last 10 days

whereis halt Show the location of a binary file, source code, or man page

which halt Show the full path of a binary or executable file

Delete files larger than 50M:

find /var/mail/ -size +50M -exec rm {} \;

4. File Permissions – Use ‘+’ to set permissions, use ‘-‘ to remove

9. chmod command

ls -lh Display permissions

chmod ugo+rwx directory1 Set read (r, 4), write (w, 2), and execute (x, 1) permissions for the owner (u), group (g), and others (o) on the directory

chmod go-rwx directory1 Remove read, write, and execute permissions for group (g) and others (o) on the directory

10. chown command

(Change the owner of a file)

chown user1 file1 Change the owner attribute of a file

chown -R user1 directory1 Change the owner attribute of a directory and all files within it

chown user1:group1 file1 Change both the owner and group attributes of a file

11. chgrp command

(Change the group ownership of a file)

chgrp group1 file1 Change the group of a file

5. Text Processing

12. grep command

(Analyzes a line of information, if it contains the information we need, it displays that line; this command is usually used with pipe commands to filter and process the output of some commands)

grep Aug /var/log/messages Search for the keyword “Aug” in the file ‘/var/log/messages’

grep ^Aug /var/log/messages Search for words starting with “Aug” in the file ‘/var/log/messages’

grep [0-9] /var/log/messages Select all lines in the ‘/var/log/messages’ file that contain numbers

grep Aug -R /var/log/* Search for the string “Aug” in the ‘/var/log’ directory and its subsequent directories

sed ‘s/stringa1/stringa2/g’ example.txt Replace “string1” with “string2” in the example.txt file

sed ‘/^$/d’ example.txt Delete all blank lines from the example.txt file (search public account: “Internet Architect”, reply “2T”, get an architect’s treasure)

13. paste command

paste file1 file2 Merge the contents of two files or columns

paste -d ‘+’ file1 file2 Merge the contents of two files or columns, separated by “+”

14. sort command

sort file1 file2 Sort the contents of two files

sort file1 file2 | uniq Get the union of two files (keep only one of the duplicate lines)

sort file1 file2 | uniq -u Delete the intersection, leaving the other lines

sort file1 file2 | uniq -d Get the intersection of two files (keep only the lines that exist in both files)

15. comm command

comm -1 file1 file2 Compare the contents of two files and delete the content contained only in ‘file1’

comm -2 file1 file2 Compare the contents of two files and delete the content contained only in ‘file2’

comm -3 file1 file2 Compare the contents of two files and delete the common parts of both files

6. Packing and Compressing Files

16. tar command

(Packages files; by default, it does not compress; if specified parameters are set, it will also call the corresponding compression program (such as gzip and bzip, etc.) to compress and decompress)

-c : Create a new archive file

-t : View the contents of the archive file and the filenames it contains

-x : Decompress or unpack; can be used with -C (uppercase) to specify the directory for extraction; note that -c, -t, -x cannot appear in the same command

-j : Compress/decompress with bzip2 support

-z : Compress/decompress with gzip support

-v : Display the filename being processed during compression/decompression

-f filename : filename is the file to be processed

-C dir : Specify the directory dir for compression/decompression

Compress: tar -jcv -f filename.tar.bz2 The name of the file or directory to be processed

Query: tar -jtv -f filename.tar.bz2

Decompress: tar -jxv -f filename.tar.bz2 -C The directory to decompress to

bunzip2 file1.bz2 Decompress a file named ‘file1.bz2’

bzip2 file1 Compress a file named ‘file1’

gunzip file1.gz Decompress a file named ‘file1.gz’

gzip file1 Compress a file named ‘file1’

gzip -9 file1 Maximum compression

rar a file1.rar test_file Create a package named ‘file1.rar’

rar a file1.rar file1 file2 dir1 Compress ‘file1’, ‘file2’, and directory ‘dir1’ at the same time

rar x file1.rar Decompress the rar package

zip file1.zip file1 Create a zip format package

unzip file1.zip Decompress a zip format package

zip -r file1.zip file1 file2 dir1 Compress several files and directories into one zip format package

7. System and Shutdown (System Shutdown, Restart, and Logout)

shutdown -h now Shutdown the system (1)

init 0 Shutdown the system (2)

telinit 0 Shutdown the system (3)

shutdown -h hours:minutes & Shutdown the system at a scheduled time

shutdown -c Cancel the scheduled shutdown

shutdown -r now Restart (1)

reboot Restart (2)

logout Logout

time Measure the execution time of a command (i.e., program)

8. Process Related Commands

17. jps command

(Displays the Java process status of the current system, along with its id number)

jps (Java Virtual Machine Process Status Tool) is a command provided by JDK 1.5 that displays the pid of all current Java processes, simple and practical, very suitable for quickly viewing the status of current Java processes on Linux/Unix platforms.

18. ps command

(Used to select and output the process status at a certain point in time, meaning process)

-A : Display all processes

-a : All processes not related to the terminal

-u : Related processes of effective users

-x : Generally used with the a parameter, can list more complete information

-l : List PID information in a longer and more detailed way

ps aux # View all process data in the system

ps ax # View all processes not related to the terminal

ps -lA # View all process data in the system

ps axjf # View part of the process tree status

19. kill command

(Used to send a signal to a job (%jobnumber) or a PID (number), usually used with ps and jobs commands)

20. killall command

(Send a signal to a process started by a command)

21. top command

Commonly used performance analysis tool in Linux, can display the resource usage status of various processes in real-time, similar to the Windows task manager.

How to kill a process:

Graphical interface method

kill -9 pid (-9 means force close)

killall -9 program name

pkill program name

View process port number:

netstat -tunlp | grep port number

Recent Courses Available:

Linux System Administration | Linux Service Management | MySql Database Management | Shell Scripting | Python Introduction | Ansible Automation Operations | Enterprise KVM Virtualization | Kubernetes Container Orchestration | Docker | Large Website Clusters | ELK Log Center | Zabbix

Collection of 21 Most Commonly Tested Linux Commands in Interviews

Leave a Comment