Linux common tools
1. Linux package manager yum
1. Software package
- When installing software under Linux, a common method is to download the source code of the program and compile it to obtain an executable program.
- But this is too troublesome, so some people compile some commonly used software in advance and make it into a software package (which can be understood as an installation program on Windows) and put it on a server. This compilation can be easily obtained through the package manager. Good software package, install it directly.
- Software packages and software package managers are like the relationship between "App" and "App Store".
2. View the software package
The command can be used yum list
to list the current software packages. Since the number of packages may be very large, here we need to use the grep command to filter out only the packages we are concerned about, for example:
- Software package name: major version number. minor version number. source program release number - release number of the software package. host platform. cpu architecture.
- The "x86_64" suffix indicates the installation package for 64-bit systems, and the "i686" suffix indicates the installation package for 32-bit systems. When selecting the package, it must match the system.
- "el7" represents the version of the operating system distribution. "el7" represents centos7/redhat7. "el6" represents centos6/redhat6.
- The last column, updates represents the name of the "software source", similar to concepts such as "Xiaomi App Store" and "Huawei App Store"
3. Install/uninstall software
Through yum , we can complete the installation of the sl command with a very simple command sudo yum install sl
; the sl command is an interesting command. After entering the sl command and press Enter , a train will pass by on the screen; we install the sl command first, because the installation requires root permissions, so we need to add sudo in front of it during installation , which means using root permissions to execute this command. How to use sudo will be introduced later; as shown below:
After executing this command, you may be asked to enter the password of the current user. We can just enter it. After entering it, you will not be able to enter the password again if you use sudo again within a short period of time;
You will then enter the installation process, and you may be asked to confirm whether to install, as shown below:
It means how much memory is needed and whether to confirm the installation. We can enter y (confirm). After installation, it is as shown below:
Then we execute the sl command as follows:
If we need to uninstall the slsudo yum remove sl
command, just enter on the command line , as shown below:
As shown in the picture above, the uninstallation has been successful. When we execute the sl command again, it is as follows:
It means the uninstallation was successful.
At the same time, when we perform installation and uninstallation operations, we can add the -y option at the end , that is sudo yum install sl -y
, the installation will be confirmed by default, and the system will not ask us again whether to confirm the installation.
4. Functions of other yum commands
yum makecache
The function of the command is to cache the server’s software package information locally.yum search
The command can search all software packages for packages containing the specified keyword.yum clean all
Command can clear old header files and packages in the cacheyum clean all
Command can clear old header files and packages in the cacheyum -y upgrade
Only upgrade all packages, do not upgrade the software and system kernel, the software and kernel remain unchanged
2. Linux editor-vim use
1. Basic concepts of vim
vim is a multi-mode text editor.
To put it simply, the difference between vi/vim is that they are both multi-mode editors. The difference is that vim is an upgraded version of vi . It is not only compatible with all instructions of vi , but also has some new features in it. For example, syntax highlighting and visual operations can be run not only on the terminal, but also on x window, mac os, and windows.
Let’s first familiarize ourselves with the three common modes of vim , namely command mode , insert mode and last line mode .
The conversion diagram between them is as follows:
- Normal/Normal/Command mode (Normal mode) controls the movement of the screen cursor, deletion of characters, words or lines, moving and copying a section and entering Insert mode, or to last line mode
- Insert mode (Insert mode) Text input can only be done in Insert mode. Press the "ESC" key to return to the command line mode. This mode is the most frequently used editing mode we will use later.
- Save or exit the file in last line mode . You can also perform file replacement, find strings, list line numbers, etc.
shift+:
This mode can be entered in command mode . To see all your modes: Open vim and enter the mode on the bottom line directly: help vim-modes
For example, we first touch a test.c file:
Then use vim to enter test.c :
Among them, after we enter test.c , the default is the command mode . As you can see from the conversion diagram, we can enter the insert mode by pressing i (or a, o), and the word insert will be displayed in the lower left corner, as shown below :
At this point we can edit the test.c file. For example, we write a simple .c file:
At this time, if we want to enter the bottom row mode , as can be seen from the conversion diagram, we must first press Esc to enter the command mode , and then press shift + ; to enter the bottom row mode ; after entering the bottom row mode , a message will appear in the lower left corner A : As shown below:
At this point, just enter the instructions we need.
2. Basic operations of vim
We already know above that the default mode for vim to open is the command mode , so we first become familiar with the operation in the command mode.
(1) Cursor movement (command mode)
First of all, in command mode, our cursor can only be moved using keyboard operations; we can use the keyboard's ↑ ↓ ← → to move the cursor, but we do not commonly use arrow operations to move the cursor, but use h, j, k, l move.
-
h: Move the cursor one unit to the left
-
j: Move the cursor down one unit
-
k: Move the cursor up one unit
-
l: Move the cursor one unit to the right
(2) Cursor positioning (command mode)
In addition to moving the cursor up, down, left, and right, we can also perform specific operations on the text, as follows:
- gg: locate the first line of code
- shift + g: locate the last line of code (n + shift + g: locate the nth line of code)
- shift + 6(^): Position to the beginning of a specific line
- shift + 4($): Position to the end of a specific line
- w: Move backward in units of words, supporting cross-line
- b: Move forward in word units, supporting cross-line
(3) Copy and paste undo (command mode)
We can also copy and paste text:
- yy: Copy the line where the cursor is (n + yy: Copy n lines)
- p: paste the content after the current line (n + p: paste n lines)
- dd: Cut/delete the line where the cursor is (n + dd)
- u: Undo the operation just now (ie ctrl+z under windows)
- ctrl + r: Undo (ctrl + y under windows)
(4) Other operations (command mode)
- shift + `(~): Convert text to upper and lower case
- r: Replace the character where the cursor is (n + r: Replace n characters)
- x: Delete one character backward (n + x: Delete n characters backward)
- shift + x: delete one character forward (n + shift + x: delete n characters forward)
- shift + 3(#): Find words with the same name, press n to jump once
After we briefly learn the operations in the command mode, we will learn the common operations in the bottom line mode .
(5) Save and exit (bottom row mode)
We press Esc in command mode and then press shift + ; to switch to bottom line mode. We can perform the following operations in bottom line mode:
- w: save text (w!: force save text)
- q: Exit the current text (q!: Force exit the text)
- wq: save and exit (wq!: force save and exit)
- set nu/nonu: Set line number for text
In addition to the three modes we have learned, we will simply learn two more modes, namely replacement mode and view mode . The mode conversion diagram between them is as follows:
(6) Replacement mode
First, let’s introduce the replacement mode. After we press shift + r in the command mode , we enter the replacement mode. At this time, the word REPLACE will appear in the lower left corner , as shown below:
At this time, we can directly replace the word, and the word where the cursor is replaced will be replaced every time it is pressed.
(7) View mode
View mode is used to help us complete batch annotations . For example, if we need to annotate multiple lines of code, we can use view mode. First we need to press ctrl + v in command mode to enter view mode . After entering view mode , the lower left corner will display:
For example, our text is as follows, and all printf functions need to be commented:
Then we need to batch annotate , press the j/k keys to select the area up and down:
Then press shift + i and the cursor will return to its original position:
Then we add the comment // at this position and finally press Esc :
As shown in the picture above, batch annotation is completed.
Summary: The steps required to complete batch annotation are:
- In command mode: ctrl+v enters view mode;
- Use the j/k buttons to select the area that needs to be annotated;
- Press shift + i ;
- Add comments to the current line;
- Press Esc
Next, we delete comments in batches; similarly, we first enter the view mode , and then hjkl selects the area. Taking the above commented code as an example, we delete all the commented code. After selecting the area, the following is as follows:
Then press d , as shown below:
Summary: Steps to delete batch comments:
- ctrl + v enter view mode
- hjkl selected area
- Just press d
We have a little trick when selecting an area. You can use n + shift + g in command mode to quickly select the area you need.
(8)Multiple file editing
vim also supports multi-file editing. We need to use vim to open a text first, enter the bottom line mode, and enter in the bottom line mode vs + 需要打开的文本名字
, such as the following figure:
As shown below, you can edit multiple files:
It should be noted that which text our cursor is on is the text we are operating on. We can press in command modectrl + ww
to switch the cursor to the next text.
Tip: When we use vim , after opening the text, the place where the cursor stays is the position where we last saved and exited. We can use to vim test.c +n
open test.c and directly position the cursor to the nth line ; this is for convenience When we compile the file, we can directly locate that line after an error occurs.
3. Linux compiler - gcc/g++ use
1. Program translation process
Before learning gcc/g++ , we first recall the program translation process, preprocessing and program environment ; the program translation process includes preprocessing, compilation, assembly, and linking.
- preprocessing
Preprocessing is to expand header files, remove comments, conditional compilation, macro replacement and other operations. We can use gcc to verify this process. Suppose we have the following code in the test.c file:
We only need to execute gcc -E test.c -o test.i
to generate the preprocessed file, ending with the .i suffix, as shown below:
We use vim to enter the test.i file to view, as shown below:
As can be seen from the figure, our code has reached more than 800 lines, indicating that the previous header file has been expanded, and comments and macro replacement have also been carried out.
In gcc -E test.c -o test.i
this instruction, the meaning is to use gcc to translate the program, stop after the preprocessing is completed, and generate the preprocessed program into a file with the suffix .i-E
; which is to preprocess the file ; -o + 文件名
Is to generate the specified file.
- compile
The compilation process is to convert C language into assembly language . We can directly convert from source files to assembly language, or we can convert from preprocessed files to assembly language;
If converted directly from source files to assembly language, the instructions are gcc -S test.c -o test.s
;
But we already have preprocessed files above. If we start directly from the source file, the work will be repeated, so we directly convert from the test.i file to assembly language. The instructions are gcc -S test.i -o test.s
as follows:
Among them gcc -S test.i -o test.s
, this instruction -S
means starting the program translation work and stopping after the compilation is completed; among them, the compiled file should end with the .s suffix.
- compilation
The process of assembly is to convert assembly language into relocatable binary files. Same as above, we can also convert directly from source files to binary files, but we have already converted the files into assembly files, so we can start from the assembly files and convert them into binary files. file, its instructions are as gcc -c test.s -o test.o
shown below:
The -c
option means to stop as soon as the assembly work is completed . Relocatable binary files are files ending with .o suffix, and test.o is a non-executable file.
- Link
Linking is the process of adding the test.o file to the system library to generate an executable program. The instructions are as follows gcc test.o -o 可执行文件名
:
Mycode in the picture above is an executable program file. To execute it, we only need to enter instructions ./mycode
, as shown below:
As shown in the figure above, the execution of the program is completed.
The above four steps are the translation process of our program, but when we usually want to execute a program, we do not need to perform the above steps separately. For example, there is only one .c file in my current directory, as shown in the figure:
We can currently directly generate an executable file from the test.c file. We only need to execute the instructions gcc test.c
, as shown below:
The name of the executable file generated by the system by default is a.out . When we want to execute it, we only need to find it in the current directory and execute it directly. The instruction is ./a.out
, as shown in the figure:
This completes the execution of our program; in addition, if we want the test.c file to generate an executable file with a specified name, we can bring -o
the option, that is, execute gcc test.c -o 文件名
.
The above is the use of gcc . In fact, the use of g++ is similar to the use of gcc . You can refer to gcc completely . Just change gcc to g++ when using it . Among them, gcc is the compiler that compiles C language , and g++ can compile C language and C++ .
2. Understanding of dynamic and static libraries
In our C program above , the function implementation of "printf" is not defined , and the "stdio.h" included in the precompilation only has the declaration of the function, but does not define the implementation of the function. So, where is it implemented? What about the "printf" function?
The answer is that the system has implemented these functions into a library file named libc.so.6 . If not specified, gcc will go to the system's default search path /usr/lib64/libc.so.6 . Search, that is, link to the libc.so.6 library function, so that the function "printf" can be implemented, and this is the role of the link; we can see that this file does exist:
- static library
Static library refers to adding all the code of the library file to the executable file when compiling and linking. Therefore, the generated file is relatively large, but the library file is no longer needed at runtime. Its suffix is generally .a
Among them, the static library is a collection of all methods provided by C/C++ or other third parties. It is copied by all programs to copy the required code into their own executable programs;
The advantages of static linking: it ignores the library and can run independently;
the disadvantages of static linking: it is too large and wastes resources;
By default, our server does not have the C static library installed, only the dynamic library. If you need to install the C static library , you only need to execute the instruction sudo yum install glibc-static
; if you need to install the C++ static library , sudo yum install -y libstdc++-static
just execute the instruction.
sudo yum install glibc-static // c 静态库
sudo yum install -y libstdc++-static // c++ 静态库
- dynamic library
Dynamic libraries , on the contrary, do not add the code of the library file to the executable file when compiling and linking. Instead, the library is loaded by the runtime link file when the program is executed, which can save system overhead. Dynamic libraries generally have a suffix of .so . As mentioned earlier, libc.so.6 is a dynamic
library .
gcc uses dynamic libraries by default when compiling . After completing the linking, gcc can generate the executable file.
The advantages of dynamic linking: the executable program formed is smaller in size and saves resources; the
disadvantages of dynamic linking: it is slightly slower and relies heavily on dynamic libraries. If the dynamic library is gone, all programs that rely on this library will not be able to run;