46 Common Linux Interview Questions for You

46 Common Linux Interview Questions for YouQuestion 1:What symbol represents the absolute path? How to represent the current directory and the parent directory? How to represent the home directory? What command is used to change directories?Answer:Absolute path: e.g., /etc/init.dCurrent directory and parent directory: ./ ../Home directory: ~/Change directory: cdQuestion 2:How to view the current processes? How to exit? How to view the current path?Answer:View current processes: psExecute exit: exitView current path: pwdQuestion 3:How to clear the screen? How to exit the current command? How to execute sleep? How to view the current user ID? What command is used to view specific help?Answer:Clear screen: clearExit current command: ctrl+c to completely exitExecute sleep: ctrl+z to suspend the current process, fg to resume in the backgroundView current user ID: “id”: displays the current logged-in user’s uid, gid, and group membershipView specific help: e.g., man adduser (very comprehensive and includes examples); adduser –help (shows some common parameters); info adduser;Question 4:What function does the ls command perform? What parameters can it take, and what are the differences?Answer:Function of ls: lists directories and files in the specified directoryParameters and differences: -a for all files, -l for detailed information including size, permissions, etc.Question 5:Commands to create symbolic links (shortcuts) and hard links.Answer:Symbolic link: ln -s slink sourceHard link: ln link sourceQuestion 6:What command is used to create a directory? What command is used to create a file? What command is used to copy a file?Answer:Create directory: mkdirCreate file: typically touch, vi can also create files; any output to a non-existent file will create itCopy file: cpQuestion 7:What command is used to modify file permissions? What is the format?Answer:Modify file permissions: chmodFormat as follows:

$ chmod u+x file gives the owner of file execute permissions$ chmod 751 file gives the owner read, write, and execute (7) permissions, gives the group read and execute (5) permissions, and gives others execute (1) permissions$ chmod u=rwx,g=rx,o=x file another form of the above$ chmod =r file gives read permissions to all users$ chmod 444 file same as above$ chmod a-wx,a+r file same as above$ chmod -R u+r directory recursively gives read permissions to the owner of all files and subdirectories in directory

Question 8:What commands can be used to view file contents?Answer:vi filename # view in edit mode, can modifycat filename # display all file contentsmore filename # display file contents page by pageless filename # similar to more, but allows backward navigationtail filename # view only the end, can specify line numbershead filename # view only the beginning, can specify line numbersQuestion 9:What command is used to write files? How to output a string with spaces to the screen, such as “hello world”?Answer:Write file command: viOutput a string with spaces: echo hello worldQuestion 10:Which folder is the terminal in? What file is the black hole file located in?Answer:Terminal: /dev/ttyBlack hole file: /dev/nullQuestion 11:What command is used to move files? What command is used to rename?Answer:mv mvQuestion 12:What command is used to copy files? If you need to copy the folder as well, what command is used? If you need a prompt, what command is used?Answer:cp cp -r ????Question 13:What command is used to delete files? If you need to delete the directory and its files as well, what command is used? What command is used to delete empty folders?Answer:rm rm -r rmdirQuestion 14:What wildcard characters can be used in Linux commands? What do they represent?Answer:“?” can replace a single character.“*” can replace any number of characters.Square brackets “[charset]” can replace any single character from the charset, e.g., [a-z], [abABC]Question 15:What command is used to count the contents of a file? (line number, word count, byte count)Answer:wc command -c counts bytes -l counts lines -w counts words.Question 16:What is the use of the grep command? How to ignore case? How to find lines that do not contain a string?Answer:It is a powerful text search tool that can use regular expressions to search text and print matching lines.grep [stringSTRING] filename grep [^string] filenameQuestion 17:What are the states of processes in Linux? What symbols are used to represent them in the information displayed by ps?Answer:(1) Uninterruptible state: The process is in a sleep state, but it is uninterruptible. Uninterruptible means the process does not respond to asynchronous signals.(2) Stopped/Tracing state: When a SIGSTOP signal is sent to the process, it enters the TASK_STOPPED state; when the process is being traced, it is in the TASK_TRACED state.“Being traced” means the process is paused, waiting for the process tracing it to operate on it.(3) Ready state: The state in the run_queue(4) Running state: The state in the run_queue(5) Interruptible sleep state: A process in this state is suspended waiting for some event to occur (e.g., waiting for a socket connection, waiting for a semaphore).(6) Zombie state: If the parent does not release the child’s corpse (task_struct) through the wait series of system calls.(7) Exit state

D Uninterruptible (usually IO)R Running, or in the queueS SleepingT Stopped or being tracedZ Zombie processW Swapped out (ineffective since kernel 2.6)X Dead process

Question 18:How to run a command in the background?Answer:Generally, use & at the end of the command to let the program run automatically. (No need to add a space after the command)Question 19:How to display all processes using ps? How to use ps to view information about a specific process?Answer:ps -ef (system v output) ps -aux bsd format outputps -ef | grep pidQuestion 20:Which command is used to view background tasks? Answer:job -lQuestion 21:What command is used to bring a background task to the foreground? What command is used to resume a stopped background task in the background?Answer:Bring a background task to the foreground: fgResume a stopped background task in the background: bgQuestion 22:What command is used to terminate a process? What parameters are needed?Answer:kill [-s ][program] or kill [-l ] kill -9 pidQuestion 23:How to view all signals supported by the system?Answer:kill -lQuestion 24:What command is used to search for files? What is the format?Answer:find whereis with parameters and filenamelocate only with filenamefind directly searches the disk, slower.find / -name “string*”Question 25:What command is used to see who is currently using the host? What command is used to find information about the terminal you are in?Answer:Find out the terminal you are in: who am iSee who is currently using the host: whoQuestion 26:What command is used to view the list of commands used?Answer:historyQuestion 27:What command is used to view disk usage? What about free space?Answer:df -hlFile system Capacity Used Available Used% Mounted on /dev/hda2 45G 19G 24G 44% //dev/hda1 494M 19M 450M 4% /bootQuestion 28:What command is used to check network connectivity?Answer:netstatQuestion 29:What command is used to view IP address and interface information?Answer:ifconfigQuestion 30:What command is used to view various environment variables?Answer:View all envView a specific one, e.g., home: env $HOMEQuestion 31:What command is used to specify the command prompt?Answer:

\u: displays the current user account\h: displays the current hostname\W: only displays the last directory of the current path\w: displays the current absolute path (the current user directory is replaced with ~)$PWD: displays the current full path\$: displays the command line ‘$’ or ‘#’ symbol\#: the number of the command issued\d: represents the date, formatted as weekday month date, e.g., “Mon Aug 1”\t: displays time in 24-hour format, e.g., HH:MM:SS\T: displays time in 12-hour format\A: displays time in 24-hour format: HH:MM\v: BASH version information e.g., export PS1='[\u@\h\w\#]\$’

Question 32:How to find where the executable files for commands are searched? How to set and add them?Answer:whereis [-bfmsu][-B …][-M …][-S …][file…]Note: The whereis command searches for files that meet conditions in specific directories. These files should belong to original code, binary files, or help files.

-b only searches for binary files.-B only searches for binary files in the specified directory. -f does not display the path name before the file name.-m only searches for help files.-M only searches for help files in the specified directory. -s only searches for original code files.-S only searches for original code files in the specified directory. -u searches for files that do not contain the specified type.which command searches for the location of a system command in the path specified by the PATH variable and returns the first search result.-n specifies the length of the file name, which must be greater than or equal to the longest file name among all files.-p is similar to the -n parameter, but includes the file path.-w specifies the width of the output column.-V displays version information

Question 33:What command is used to find executable commands?Answer:which only finds executable fileswhereis only finds binary files, documentation, source files, etc.Question 34:How to create an alias for a command?Answer:alias la=’ls -a’Question 35:What are the definitions of du and df, and what are the differences?Answer:du shows the size of directories or filesdf shows information about the file system where each resides, by default it shows all file systems.(The file system allocates some disk blocks to record its own data, such as i-nodes, disk distribution maps, indirect blocks, super blocks, etc. This data is usually invisible to most user-level programs and is commonly referred to as Meta Data.) The du command is a user-level program that does not consider Meta Data, while the df command looks at the disk allocation map of the file system and considers Meta Data.The df command obtains real file system data, while the du command only looks at part of the file system.Question 36:Detailed explanation of awk.Answer:

awk ‘{pattern + action}’ {filenames}#cat /etc/passwd |awk -F ‘:’ ‘{print $1″\t”$7}’ //-F means separated by ‘:’ root /bin/bashdaemon /bin/sh searches for all lines in /etc/passwd that have the root keyword

#awk -F: ‘/root/’ /etc/passwd root:x:0:0:root:/root:/bin/bash

Question 37:When you need to bind a macro or key to a command, what should you do?Answer:You can use the bind command, which makes it easy to implement macros or key bindings in the shell.When binding keys, we need to first obtain the character sequence corresponding to the bound key.For example, to get the character sequence for F12, you can do the following: press Ctrl+V, then press F12. We can get the character sequence for F12 as ^[[24~.Then use bind to perform the binding.[root@localhost ~]# bind ‘”\e[24]:”date”‘Note: The same key may produce different character sequences on different terminals or terminal emulators.[Note] You can also use the showkey -a command to view the character sequences corresponding to keys.Question 38:If a Linux novice wants to know the list of all commands supported by the current system, what should he do?Answer:Use the command compgen -c to print the list of all supported commands.

[root@localhost ~]$ compgen -cl.lllswhichifthenelseelifficaseesacforselectwhileuntildodone

Question 39:If your assistant wants to print the current directory stack, what would you suggest?Answer:Use the Linux command dirs to print the current directory stack.

[root@localhost ~]# dirs

/usr/share/X11

[Note]: The directory stack is operated using pushd and popd.Question 40:Currently, your system has many running tasks. What method can be used to remove all running processes without restarting the machine?Answer:Use the Linux command ‘disown -r’ to remove all running processes.Question 41:What is the function of the hash command in bash shell?Answer:The Linux command ‘hash’ manages a built-in hash table that records the full paths of commands that have been executed, and it can print the commands you have used and the number of times they have been executed.

[root@localhost ~]# hashhits command2 /bin/ls2 /bin/su

Question 42:Which bash built-in command can perform mathematical operations?Answer:The built-in command let in bash shell can perform integer arithmetic operations.

#! /bin/bashlet c=a+b

Question 43:How to view the contents of a large file page by page?Answer:By piping the command “cat file_name.txt” and ‘more’ together, this requirement can be achieved.[root@localhost ~]# cat file_name.txt | moreQuestion 44:Which user does the data dictionary belong to?Answer:The data dictionary belongs to the ‘SYS’ user, and the users ‘SYS’ and ‘SYSEM’ are created automatically by the system.Question 45:How to view an overview and usage of a Linux command? Suppose you accidentally see a command you have never seen before in the /bin directory, how can you know its function and usage?Answer:Use the command whatis to display a brief usage of this command, for example, you can use whatis zcat to see the introduction and brief usage of ‘zcat’.

[root@localhost ~]# whatis zcatzcat [gzip] (1) – compress or expand files

Question 46:Which command can be used to view the disk space quota of your file system?Answer:Use the command repquota to display the quota information of a file system.[Note] Only the root user can view the quotas of other users.

————

Editor ∑Gemini

Source: Programmer

☞Interesting facts about Taylor’s theorem

☞Qiu Chengdong: Talking about differential geometry

☞How Leibniz Came Up with Calculus? (Part 1)

☞The physical significance of linear correlation and rank

☞What do you think is the ugliest formula in the history of mathematics?

☞Terence Tao talks about what good mathematics is

☞Tian Yuandong: The use of mathematics (Part 2)

☞You definitely didn’t think mathematicians could be so rogue, they conduct violent proofs if they don’t agree

☞The five most powerful doctoral theses in the world

☞What coincidences in mathematics are eye-opening?

☞Algorithm success! A professor from Tsinghua University was robbed of his car in the US, and the police were helpless, he used a “greedy algorithm” to retrieve it

☞A strange article in academic history: How to catch a lion with mathematics

☞Reflections from a professor at National Taiwan University: The hardest lesson we didn’t teach students

☞MIT graduate student study guide – how to be a graduate student

☞Sharing mathematics, common sense, and luck – a lecture by investment master James Simons at MIT in 2010

Welcome to contribute to the WeChat public account of the beauty of algorithm mathematics

The articles involve mathematics, physics, algorithms, computer science, programming, and other related fields. Upon adoption, we will provide remuneration.

Submission email: [email protected]

Leave a Comment