Pure dry goods | Linux must-have interview questions for entry and common use

Follow, star public account, direct access to exciting content

ID: Technology makes dreams greater

Source: web material

Finishing: Li Xiaoyao

I believe everyone understands the importance of learning Linux without me saying more. The following are the basic knowledge of Linux and the Linux commands frequently asked in interviews. I hope to help you understand and use it in a more standardized way~

How to switch between graphic mode and text mode


   Linux presets provide six command window terminals for us to log in.

  • By default, we log in to the first window, which is tty1. The six windows are tty1, tty2… tty6. You can press Ctrl + Alt + F1 ~ F6 to switch them.

  • If you have installed the graphical interface, it will enter the graphical interface by default. At this time, you can press Ctrl + Alt + F1 ~ F6 to enter one of the command window interfaces.

  • When you enter the command window interface and then return to the graphical interface, just press Ctrl + Alt + F7 to return.

  • If you use a vmware virtual machine, the shortcut keys for switching the command window are Alt + Space + F1~F6.

  • If you are in the graphical interface, press Alt + Shift + Ctrl + F1~F6 to switch to the command window.

Learn to use shortcut keys

  • Ctrl + C: This is a shortcut key used to terminate the current command. Of course, you can also enter a large string of characters. If you don't want it to run directly Ctrl + C, the cursor will jump to the next line.

  • Tab: This key is the most useful key, and it is also the key with the highest probability of being hit by many people. Because when you hit half of a command, it will help you complete it. Not only commands, but when you type a directory, you can also complete it. If you don't believe it, try it.

  • Ctrl + D: Exit the current terminal, you can also enter exit.

  • Ctrl + Z: Pause the current process. For example, if you are running a command and suddenly feel something wrong and want to pause it, you can use this shortcut. After pausing, you can use fg to resume it.

  • man + command: input man ls in fact, the format is man + command, you will see the related help documents. From the introduction of the command to the introduction of the parameters and usage of the command are very detailed, not bad.

  • Ctrl + L: Clear the screen and move the cursor to the first line.

Linux system directory structure

After logging in to the system, enter ls / in the current command window and you will see


Learn to query help files — man

This man is usually used to look at the help documentation of a command. E.g:


The following is an explanation of these directories:

  • /bin: bin is the abbreviation of Binary, this directory stores the most frequently used commands.

  • /usr: This is a very important directory. Many applications and files of users are placed in this directory, similar to the program files directory under windows.

  • /usr/bin: Application programs used by system users.

  • /usr/sbin: The more advanced management programs and system daemons used by super users.

  • /usr/src: The default storage directory of the kernel source code.

  • /boot: Stored here are some core files used when starting Linux, including some connection files and mirror files.

  • /dev: dev is the abbreviation of Device. Linux's external devices are stored in this directory, and the way of accessing devices and files in Linux are the same.

  • /etc: This directory is used to store all the configuration files and subdirectories needed for system management.

  • /home: The user's home directory. In Linux, each user has its own directory. Generally, the directory name is named after the user's account.

  • /lib: This directory stores the most basic dynamic link shared libraries of the system, and its function is similar to the DLL files in Windows. Almost all applications need to use these shared libraries.

  • /mnt: This directory is provided by the system to allow users to temporarily mount other file systems. We can mount the CD-ROM drive on /mnt/, and then enter the directory to view the contents of the CD-ROM drive.

  • /opt: This is the directory where additional software is installed on the host. For example, if you install an ORACLE database, you can put it in this directory. The default is empty.

  • /proc: This directory is a virtual directory, which is a mapping of system memory. We can directly access this directory to obtain system information. The content of this directory is not on the hard disk but in the memory. We can also directly modify some files in it. For example, we can use the following command to block the host's ping command so that others cannot ping your machine: echo 1> /proc /sys/net/ipv4/icmp_echo_ignore_all.

  • /root: This directory is the user home directory of the system administrator, also known as the super authority.

  • /sbin s: It means Super User. The system management program used by the system administrator is stored here.

  • /selinux: This directory is unique to Redhat/CentOS. Selinux is a security mechanism, similar to a windows firewall, but this mechanism is more complicated. This directory is for storing selinux-related files.

  • /srv: This directory stores some data that needs to be extracted after the service is started.

  • /sys: This is a big change in the linux2.6 kernel. A new file system sysfs in the 2.6 kernel is installed in this directory. The sysfs file system integrates the information of the following three file systems: the proc file system for process information, the devfs file system for devices, and the devpts file for pseudo-terminals. system. The file system is an intuitive reflection of the kernel device tree. When a kernel object is created, the corresponding files and directories are also created in the kernel object subsystem.

  • /tmp: This directory is used to store some temporary files.

  • /var: This directory contains things that are constantly expanding. We are used to putting those frequently modified directories in this directory. Including various log files.

  • /lost+found: This directory is usually empty. When the system is shut down illegally, some files are stored here.

  • /media: Linux system will automatically recognize some devices, such as U disk, CD-ROM, etc., after recognition, linux will mount the recognized device to this directory.

In the Linux system, there are several directories that are more important, and you need to be careful not to delete or change internal files by mistake. /etc: As mentioned above, this is a configuration file in the system. If you change a file in this directory, the system may not start. /bin, /sbin, /usr/bin, /usr/sbin: This is the default storage directory of the executable file. For example, ls is in the /bin/ls directory.

It is worth mentioning that /bin, /usr/bin are instructions for system users (pass users except root), and /sbin, /usr/sbin are instructions for root. /var: This is a very important directory. There are many programs running on the system, then each program will have a corresponding log generated, and these logs are recorded in this directory, specifically in the /var/log directory, in addition The default placement of mail is also here.

How to shut down properly

In fact, it is mostly used on servers in the Linux field, and it rarely encounters shutdown operations. After all, there is no end to running a service on the server, unless under special circumstances, it will be shut down as a last resort.

Linux is different from windows. Under Linux, because every program (or service) is executed in the background, there may actually be quite a lot of people on your host at the same time behind the screen you can’t see. Work, such as browsing the web, sending letters, transferring files by FTP, etc. If you directly press the power switch to shut down, other people's data may be interrupted. In addition, the biggest problem is that if it is shut down abnormally, the file system may be damaged (because it is too late to write the data back to the file, so some service files may have problems).

If you want to shut down, you must ensure that no other users are online in the current system. The who command can be issued, and if you want to see the online status of the network, you can issue the netstat -a command, and depending on the background execution program, you can execute the ps -aux command. Using these commands can give you a little understanding of the current status of the host.

  • sync: Synchronize data from memory to hard disk.

  • shutdown: shutdown command, you can take a look at the help file with man shutdown, for example, you can run the following command

  • shutdown -h 10:'This server will shutdown after 10 mins' This command tells everyone that the computer will shut down in 10 minutes and will be displayed on the current screen of the logged in user.

  • Shutdown -h now: Shut down immediately

  • Shutdown -h 20:25: The system will shut down at 20:25 today

  • Shutdown -h +10: Shut down after ten minutes

  • Shutdown -r now: The system restarts immediately

  • Shutdown -r +10: The system restarts in ten minutes

  • reboot: reboot, equivalent to shutdown -r now

  • halt: Shut down the system, equivalent to shutdown -h now and poweroff

Finally, to summarize, whether you are restarting the system or shutting down the system, you must first run the sync command to write the data in the memory to the disk. The commands for shutdown are shutdown -h now halt poweroff and init 0, and the commands for restarting the system are shutdown -r now reboot init 6.

Absolute path and relative path


Absolute path


The location path of the complete file starting with a forward slash
can be used whenever you want to put a file name on top

relative path

Do not start with a slash to the
top relative to the current working directory or a directory position
can be used as a short form to specify a file name

-Base name: basename is the name of the file itself

-Directory name: dirname is the name of the folder where it is located

Change directory

cd change directory

Use absolute or relative path
cd/home/wang
cd home/wang to
switch to the parent directory: cd to
switch to the previous working directory: cd-

Option: -P

Related environment variables:

PWD: current directory path
OLDPWD: last directory path

File wildcard

*Match zero or more characters

? Matches any single character

~ Current user's home directory

~mage user mage home directory

~+ Current working directory  

~- the previous working directory

[0-9] match number range

[az]: letters

[wang] matches any character in the list
[^wang] matches all characters in the list except for characters

Create empty file and refresh time

touch command

Format: touch[OPTION]…FILE…
-a only change atime and ctime
-m only change mtime and ctime
-t [[CC]YY]MMDDhhmm[.ss]
specify the timestamp of atime and mtime
-c If the file does not exist, Will not be created

Copy files and directories cp

cp common options

-i: Prompt before overwriting, -n: not overwrite, pay attention to the order of the two
-r, -R: recursively copy the directory and all contents inside
-a: archive, equivalent to -dR –preserv=all
-d:--no -dereference—preserv=links do not copy the original file, only copy the link name
-preserv[=ATTR_LIST]
mode: permission
ownership: owner group

cp option

-p: equivalent—preserv=mode,ownership,timestamp
-v:--verbose
-f:--force
-u:--update only copy source target update file or target non-existent file
-b: target exists, before overwriting Back up first
--backup=numbered target exists, back up and add number suffix before overwriting

Move and rename files

mv[OPTION]…[-T]SOURCE DEST
mv[OPTION]…SOURCE…DIRECTORY
mv[option]…-t DIRECTORY SOURCE…

Common options:

-i: interactive
-f: mandatory
-b: target exists, back up before overwriting

delete

rm[OPTION]…FILI…
(It is recommended that you do not use the rm command during work)

Common options:

-i interactive
-f force delete
-r recursive
--no-preserve-root delete/
example:
rm -rf /*

Directory operations

tree display directory tree

-d: only display the directory
-L level: specify the number of levels displayed
-p pattern: only display the path matched by the specified pattern

mkdir create directory

-p: Exist in the directory without reporting an error, and can automatically create the required directories
-v: Display detailed information
-m MODE: Specify permissions directly when creating a directory

rmdir delete empty directories

-p: delete the parent empty directory recursively
-v: display detailed information

rm -r delete directory tree recursively

Index node

**The inode (index node) table contains a list of all files in the file system
inode table structure**


A node (index node) is a table entry that contains information (metadata) about the file, including:
file type, permission, UID, GID
link number (the number of path names pointing to the file name)
and the size and difference of the file The timestamp
points to the data block pointer
of the file on the disk and other data about the file

Hard link and soft link (symbolic link)

Hard link

-Creating a hard link will add additional record items to refer to files
-Corresponding to a physical file on the same file system
-Each directory references the same inode number
-When creating a
file, the number of links increases -When deleting a file:
rm command to decrement the linked
file To exist, there is at least one link number.
When the link number is zero, the file is deleted
-cannot span drives or partitions
-syntax:
In filename [linkname]

Symbolic link (soft link)


-A symbolic link points to another file- IS-l displays the name of the link and the referenced file
-the content of a symbolic link is the name of the file it refers to
-can be targeted
-can cross partitions
-points to another file Path; its size is the length of the path string pointed to, without increasing or decreasing the reference count of the target file inode; -Syntax
:
In-s filename [linkname]

Hard link : Give multiple names to a file.
Soft link : The original file generally uses a relative path, and the relative path must be relative to the path of the soft link file

The difference between hard link and soft link:

(1) Essence: A hard link is a file with multiple names, and a soft link corresponds to multiple shortcuts.
(2) Cross partition: Hard links cannot cross partitions, soft links can cross partitions.
(3) Number of links: Hard links will increase the number of links, but soft links will not.
(4) Node number: the hard link is the same, but the soft link is different.
(5) Will deleting the original file affect the linked file? When the files are hard-linked, each file has a level relationship, and the access can still be accessed, while the soft link is a dependency relationship, and the link will become invalid after the original file is deleted.
(6) Directory: hard link does not support directory, soft link supports directory.

Determine the content of the file

-File content can contain multiple types of data
-Check the type of file, and then determine the appropriate open command or application using
-file[options]...-
Common options:

-b does not display the file name when listing the file identification results
-f filelist lists the file types of the file names in the filelist
-F replaces the default ":" separator after the output file with the specified separator
-L view the corresponding soft link correspondence File type of the file
--help displays online help for the command

Standard input and output

Program: instruction + data
read in data: input
output data: output
open file has a fd: file descriptor (file descriptor)
Linux provides three types of I/O devices for the program
-standard input (STDIN)-0 default acceptance from Keyboard input
-standard output (STDOUT) -1 default output to the terminal window
-standard error (STDERR) -2 default output to the terminal window
I/O redirection: change the default location

Redirect output and errors to file

STDOUT and STDERR can be redirected to file
commands. Operation symbols Operation symbols
supported by file names include:
>把STDOUT重新定向到文件
2>把STDERR重新定向到文件
&>把所有输出重定到文件

>File content will be overwritten
set-C 禁止将内容覆盖已有文件,但可追加
>file强制覆盖
Set+C允许覆盖
>>Based on the original content, add content
2> Overwrite redirection error output data stream
2>>Add redirection error output data stream
Standard output and error output are directed to different locations
COMMAND>/path/ to/file.out2>/path/to/error.out
merges standard output and error output into the same data stream for redirection
-&>overwrite redirection
-&>>additional redirection-
COMMAND>/path/to/file .out2>&1 (The order is important)
-COMMAND>>/path/to/file.out2>&1
(): Combine STDOUT
(cal2007;cal2008)>all.txt of multiple programs

tr command

  • tr conversion and deletion of characters

  • tr[OPTION]…SET1[SET2]

  • Options

    • -c -C --complement: take the complement of the character set

    • -d --delete: delete all characters belonging to the first character set

    • -s --squeeze-repeats: represent consecutively repeated characters as a single character

    • -t --truncate-set1: Convert the characters corresponding to the first character set into the characters corresponding to the second character set

  • [:alnum:]:letters and numbers[:alpha:]:letters[:cntrl:]:control (non-printing) characters

  • [:digit:]:digit[:graph:]:graphic characters[:lower:]:lowercase letters[:print:]:printable characters

  • [:punct:]:Punctuation marks[:space:]:White space characters[:upper:]:Uppercase letters

  • [:xdigit:]: Hexadecimal characters

Send multiple lines to STDIN

Use the "<<termination word" command to redirect multiple lines from the keyboard to STDIN
-all text up to the termination word position is sent to STDIN
-sometimes called heretext

Mail -s“Please Call”[email protected]<<END
>Hi Wang
>
>Please give me a call when you get in.We may need
>to do some maintenance on server1.
>
>Details when you are on -site
>Zhang
>END


pipeline

The pipe (represented by the symbol "|") is used to connect command
1| command2|command3... -Send
STDOUT of command 1 to STDIN of command 2, and STDOUT of command 2 to STDIN of command 3
-STDERR cannot pass by default Pipeline forwarding, can be implemented by 2>&1 or |&-the
last command will be executed in the subshell process of the current shell process-
the function of combining multiple tools
Is | tr'a-z''A-Z'    
less: one page one Page view input
ls -l/etc|less
mail: send input
echo "test email"|mail -s "test" [email protected]
lpr: send input to printer
echo "test print"| lpr- P printer_name
pipeline-Symbol
example:
Pack the file in /home, but the packed data is not recorded to the file, but transmitted to stdout. After the pipeline, tar -cvf- /home is transmitted to the following tar -xvf- , The latter-is to take the stdout of the previous command, so there is no need to use a temporary file
tar -cvf-/home|tar-xvf-

Redirect to multiple targets

Command 1|tee[-a] file name|Command 2
save the STDOUT of command 1 in a file, as the input of command 2-a
additional
use
-save the output of different stages
-troubleshooting of complex pipelines
-view and record at the same time Output

File search

Find eligible files on the file system
File search: locate, find
Non-real-time search (database search): locate
real-time search: find

Locate command


Useful options for locate KEYWORD
-i Case-insensitive search
-n N Only list the first N matching items
-r Use regular expression
examples to
search for files with "conf" in the name or path
locate conf
Use Regex to search for " Files ending with .conf"
locate -r'.conf$'

find command

Real-time search tool to complete file search by traversing the specified path
. Features:
-The search speed is slightly slower
-Precise search
-Real-time search
-It may only search for directories with read and execute permissions.
Syntax:
find[OPTION]...find path [processing actions ]
Search path: specify the specific target path; the default is the current directory
search condition: the specified search criteria can be carried out by file name, size, type, permission and other criteria; the default is to find all file
processing actions under the specified path : File operations, output to the screen by default

Search condition

Refers to the search level

-maxdepth level Maximum search directory depth, specify the directory as
level 1 -mindepth level Minimum search directory depth

Process the files in the directory first, then process the directory

-depth

Find based on file name and inode:

-name "file name": support the use of glob
*,?,[],[^]
-iname "file name": case insensitive
-inum n search by inode number
-samefile name file with the same inode number
-links n Files with n number of links-
regex "PATTERN": match the entire file path with PATTERN instead of the file name

Search by owner and group:

-user USERNAME: find files
whose owner is a specified user (UID) -group GRPNAME: find files whose belonging group is a specified group (GID)
-uid UserID: find files whose owner is a specified UID number
-gid GroupID: find their own group For the file with the specified GID number-
nouser: find files without an owner-nogroup: find files
without a group

Find by file type"

type TYPE:
f: ordinary file
d: directory file
l: symbolic link file
s: socket file
b: block device file
c: character device file
p: pipe file

Empty file or directory

-empty
Find/app -type d -empty

Combination conditions:

And: -a
or: -o
non: -not,!

De Morgan's Law

(Not A) or (not B)=not (A and B)
(not A) and (not B)=not (A or B)
Example
:! A -a!B=!(A -o B)
! A -0!B=!(A -a B)

Find based on file size

Link : https://blog.csdn.net/alitech2017/article/details/105729350         

Recommended reading:

嵌入式编程专辑
Linux 学习专辑
C/C++编程专辑

关注微信公众号『技术让梦想更伟大』,后台回复“m”查看更多内容,回复“加群”加入技术交流群。
长按前往图中包含的公众号关注

Guess you like

Origin blog.csdn.net/u012846795/article/details/108138794