Linux_ basic knowledge and common commands

1. Components of the Linux system

Components of the Linux operating system

  • Linux kernel
  • Shell
  • File system
  • utilities

image.png
Linux kernel : The kernel (kernel space) is the lowest layer of the Linux system, providing the core functions of the system and allowing processes to access hardware in an orderly manner. Used to control processes, input and output devices, file system operations, and manage memory. These do not require user participation, the system completes itself.
Shell : A shell is a command line interpreter that enables users to interact with the operating system.
File system : The files in the Linux file system are a collection of data. The file system not only contains the data in the file but also the structure of the file system, files, directories, soft links, and file protection information seen by all Linux users and programs. are stored in it.
Utilities : A standard Linux system has a set of utilities, which are programs that serve users to operate the system, such as editors (for editing files), filters (for receiving data and filtering data), interactive programs , perform standard calculation operations, etc.

2. Basic commands commonly used in Linux

pwd (print work directory): display the absolute path of the current directory
cd (change directory): switch directory
common directory: (./current directory) (.../upper directory) (- path of the last operation) (~ current user username path)
ls (list show): list the contents of the current directory
mkdir (make directory): create a new folder
touch : create a file
rm : delete a file
rm -rf : delete a folder
clear : clear the screen
cp : copy a file

  • cp hello.c world.c, make a copy of hello.c and name it world.c
  • cp hello.c /home/linux/Cbase, copy hello.c to the Cbase folder, /home/linux/Cbase is the path

cp -a : copy folder

  • cp old_dir new_dir -a, copy the old_dir folder and name it new_dir
  • cp Cbase …/ -a , copy the Cbase folder to the upper directory

mv

  • Move the file (mv hello.c /home/linux/Cbase, move hello.c to the Cbase folder)
  • Rename the source file to the target file ( mv hello.c world.c, hello.c is renamed to world.c )
  • Move the source file to the target folder ( mv Cbase …/ , move the Cbase folder to the upper directory)

find : Search for the specified file, format: find path-name file name

  • find /home/linux -name hello.c, search for hello.c in the /home/linux directory

grep : Search for specified content, format: grep "string" file

  • grep "hello" log.c, search for "hello" string in log.c file
  • grep "main" * -nR, search for main in all files in the current directory and subdirectories

Pipeline : The output of one command is used as the input of another command, and multiple commands are connected by |. That is: the operation of the latter command is performed on the basis of the completion of the operation of the previous command.


cat : display all the content of the specified file
cut : specify the split character, split the specified content, format: cut -d "separation character" -f field
head : display the first few lines at the beginning of the file (generally used in conjunction with the pipeline), head -Line number file
tail : Display the last few lines at the end of the file (usually used in conjunction with pipelines), tail -line number file
give some examples:

  • cat /etc/passwd | grep "linux", find the "linux" string in the passwd file
  • ls /usr/include | grep "stdio.h", look for the "stdio.h" file in the include folder
  • head -10 /etc/passwd, display the first 10 lines at the beginning of the /etc/passwd file
  • tail -1 /etc/passwd, display the information of the last line of the /etc/passwd file
  • tail -1 /etc/passwd | cut -d ":" -f 1,3,4 , divide the information in the last line of the passwd file into pieces with colons as separators, and display the first 1, 3, 4.


wc : Count the number of lines/words/bytes of a file

  • -l displays the number of lines in a file
  • -w displays the number of words in a file
  • -c displays the number of bytes in a file

Three, Linux commonly used special symbols

symbol meaning example
Asterisk * matches a string of any length Input: ls file_*.txt
Output: file_1.txt file_2.txt file_3.txt
question mark ? matches a string of length Input: ls file_?.txt
Output: file_1.txt file_a.txt file.x.txt
Square brackets[ … ] matches one of the characters specified in Input: ls file_[otr].txt
Output: file_o.txt file_r.txt file_t.txt
Square brackets[ - ] Match a specified range of characters Input: ls file_[az].txt
Output: file_a.txt file_b.txt file_z.txt
square brackets [ ^… ] Matches everything except the characters specified in it Input: ls file_[^obt].txt
Output: other files except file_o.txt, file_b.txt, file_t.txt
output redirection> Modify and output the correct data that should have been output to the screen to other places (such as files). echo "hello world" > log.txt
writes hello world into the log.txt file, the content of the log.txt file will be cleared before writing, and there is no file to create a file
echo "happy" >> log.txt
append method write, do not clear the file content
input redirection< Change the default input source, and get the information that should be input from the keyboard from other locations (such as files). cat < /etc/passwd > a.txt
redirects /etc/passwd as the input device, and redirects the output to a.txt, and finally realizes copying the content in the /etc/passwd file to a.txt
error redirect 2> Change the error message that should have been output to the screen to output to the file bababa123 2> log.txt
As a wrong command, bababa should have output command not found to the screen, but this command will now be output to the log.txt file.
command substitution Use the output of one command as an argument to another command command1 `command2` The output of command2 is used as the parameter of command1.
Such as find `pwd` -name hello.c

Four, vim editor and gcc compiler

vim editor: a tool for editing code

vim + hello.c Open the hello.c file, at this time the code cannot be edited, only command operations can be performed.

model Instructions
edit mode Click the "i" key, the keyword insert will appear in the lower left corner of the terminal, and enter the edit mode
command mode After editing the code, press the esc key to exit the editing mode.
Press the ":" key, after a colon appears at the bottom of the window, you will successfully enter the command mode

Commonly used commands in command mode:
image.png
image.png

gcc compiler: a tool for compiling code

gcc command:

  • gcc hello.c compiles the code, and the system will generate a file called a.out in the current directory by default [all out]
    . ./a.out executes the a.out file and outputs the corresponding result.
  • gcc hello.c -o exec Compile the code, user-defined name of the generated executable file.
    ./exec Execute the ./exec file and output the corresponding result.

gcc compilation process: preprocessing --> compilation --> assembly --> link

  • Preprocessing ----> generate preprocessed C code xx.i
  • Compile ----> generate our assembly code xx.s from our preprocessed code
  • Assembly ----> Generate assembly code into our target file xx.o
  • Link ----> Generate our target file into our executable file

Parameters can be added when the gcc command is entered:

  • -E causes the compiler to stop at the end of preprocessing
  • -S causes the compiler to stop at the end of compilation
  • -c causes the compiler to stop at the end of assembly
  • -o Output the result of gcc compilation

Five, sed command and awk command

sed command

Introduction:

  • Vim uses an interactive text editing mode, and you can use keyboard commands to interactively insert, delete, or replace text in data. But the sed command is different. It uses the stream editing mode. The most obvious feature is that before sed processes the data, it needs to provide a set of rules in advance, and sed will edit the data according to these rules.

Usage scenario:

  • Oversized file processing;
  • Add and replace files in batches.
  • Regular text, such as log files separated by semicolons, spaces, etc.;

sed will process the data in the text file according to the script commands. These commands are either input from the command line or stored in a text file. The order in which this command executes the data is as follows:

  • Only read one line at a time;
  • Matches and modifies data according to the provided rule command. Note that sed will not directly modify the source file data by default, but will copy the data to the buffer, and the modification is limited to the data in the buffer;
  • Output the execution result.

When a line of data is matched, it will continue to read the next line of data and repeat this process until all the data in the file has been processed.

Syntax format: sed [options] '{command}[flags]' [filename]
[options]:

  • -e (script command) This option adds the script command that follows it to the existing command.
  • -f (script file) This option adds the script commands in its file to the existing commands.
  • -n show only matching lines
  • -i Operate the original file directly, and the content of the original file will be modified. The sed command does not modify the file by default

{command}:

  • i : insert, add a new line in front of the specified matched line as string
  • a : append, append a new line after the specified or matched line, the content is string
  • d : delete, delete the lines that meet the address delimitation conditions
  • p : print, by default, sed will output the output result to the standard output after processing the pattern space. After adding the p command, it is equivalent to outputting both the original text and the content after pattern matching processing.
  • s : Find and replace, by default only replace the string matched by the pattern for the first time in each line, if the modifier is g, it will replace all.

[flags]:

  • n : A number between 1 and 512 indicates the number of occurrences of the specified character string to be replaced.
    For example, there are 3 A's in a row, but the user only wants to replace the second A, so this tag is used;
  • g : Replace all matched content in the data. If there is no g, the replacement operation will only be performed when the first match is successful.
    For example, if there are 3 A in a row of data, only the first A will be replaced;
  • p : will print lines matching the pattern specified in the substitution command. This flag is typically used with the -n option.


Some examples:
display information on the third line of /etc/passwd

  • sed -n '3p' /etc/passwd where 3 represents the number of lines and p represents the output result

Add a line before the first line in /etc/passwd as "Good Good Study"

  • sed -e '1iGood Good Study' /etc/passwd where 1 stands for the first line and i stands for insert. Indicates that a new character string is inserted before line 1.

Change all strings named root in /etc/passwd to class

  • sed -e ‘s/root/class/g’ /etc/passwd
  • sed -n 's/root/class/2p' /etc/passwd
    The -n option will disable sed output, but the p flag will output the modified line, the effect of matching the two is to only output the modified by the replacement command Passed.

Delete the content in /etc/passwd and list the line numbers, and delete lines 2~5

  • cat -n /etc/passwd | sed '2,5d' , cat -n will display the line number, and the pipeline is used to display the content here.

Redirect the information of the root user in the /etc/passwd file to the log.txt file in the form of line numbers. It is required to replace the root in the log.txt file with linux, and save the replacement in log.txt content of the file.

  • cat /etc/passwd | grep -n “root” > log.txt
    sed -i ‘s/root/linux/g’ log.txt
    cat log.txt

awk command

The basic function of the AWK language is to browse and extract information based on specified rules in files or strings. After awk extracts information, it can operate on other text. It is a powerful text analysis tool. To put it simply, awk reads the file line by line, slices each line with a space as the default delimiter, and then performs various analysis and processing on the cut parts.
The sed command is often used to process entire lines. Awk, on the other hand, prefers to divide a line into multiple "fields" and then process it.
Applicable scene

  • Oversized file processing;
  • Output a formatted text report;
  • perform arithmetic operations;
  • Perform string manipulation, etc.

Syntax format: awk [options] 'pattern {action}' filename
[options]:

  • -F : Indicates the field delimiter used for input, the default delimiter is space or tab key
  • -v (var=VALUE) : ​​custom variable

pattern: Matching rules, generally use relational expressions as conditions, and execute corresponding actions if the conditions are met.
{action}: some calculation operation/formatted data/flow control statement

filename: file name

For example:
Create a new student.txt with the following content:

ID NAME   PHP Linux MySQL Avereage
1  Liming 82  95    86    87.66
2  Sc     74  96    87    85.66
3  Gao    99  83    93    91.66
  • Output file content: awk '{print}' student.txt
  • Output the content of column 2: awk '{print$2}' student.txt
NAME
Liming
Sc
Gao
  • Format output: awk '{print $2 $6}' student.txt , do not adjust the format output, concatenate
NAMEAvereage
Liming87.66
Sc85.66
Gao91.66
  • awk ‘{print $2"\t"$6}’ student.txt
NAME Avereage
Liming 87.66
Sc 85.66
Gao 91.66

Awk output disk information: df -Th | grep tmpfs | awk '{print $1"\t"$5}'

udev    921M
tmpfs   186M
tmpfs   945M
tmpfs   5.0M
tmpfs   945M
tmpfs   189M

By default, awk will read a line of text from the input, and then execute the program script against the data of this line, but sometimes it may be necessary to run some script commands before processing the data, which requires the use of the BEGIN key Character.

[linux#linux] awk 'BEGIN{print "test start!"}{print $2 "\t" $5}' student.txt 
test start!
NAME MySQL
Liming 86
Sc 87
Gao 93

Corresponding to the BEGIN keyword, the END keyword allows us to specify some script commands, and awk will execute them after reading the data.

[linux#linux] awk '{print $2 "\t" $5}END{print "test END!"}' student.txt 
NAME MySQL
Liming 86
Sc 87
Gao 93
test END!

6. File information and compression commands

1. File information

In Linux, it can be said that all devices are files.
For example, a hard disk is a file, a partition on the hard disk is also a file, and hardware devices such as sound cards and network cards are also files in the eyes of the operating system.

We can view the detailed information of the file through the ls -ld command, here is the detailed information of all files in the bin directory.
image.png
Take -rwxr-xr-x 1 root root 1113504 Apr 18 2022 /bin/bash as an example.

(1). File type information:

First of all, the identifier on the first digit indicates the file type information:

  • b : block device file (block) such as hard disk
  • c : character device file character such as mouse
  • d : directory file directroy
  • - : regular file
  • l : soft link file (windows shortcut) link
  • p : pipe file (often used for process communication) pipe
  • s : Unix domain socket file (often used for process communication)

(2). File permission information:

The next nine digits "rwxr-xr-x" are file permission information, and each three digits represent different roles, namely: file owner permission, group member permission of the file owner, and other person permission.

Among them: r indicates that the file is readable, w indicates that the file is writable, and x indicates that the file is executable.

We usually also use octal numbers to combine file permissions for easy memory:
different permissions are represented by octal numbers: r: 4, w: 2, x: 1. When expressing the permissions of a role, use addition to express.

For example, the file owner permission of the current file is: rwx, expressed as "7" after addition by using octal numbers; the group member permission of the file owner's group is: rx, then it is expressed as "5"; other people's permissions are the same Represented as "5".

In this way, we can use "755" to represent all the permission information of this file.

(3). Other information

  • The third part: 1 -> number of hard links
  • Part 4: root -> file owner name
  • Part V: root -> the group the file belongs to
  • Part 6: 1113504 -> file size (default unit byte)
  • Part VII: Apr 18 2022 -> File Last Modified Time
  • Part 8: /bin/bash -> file name

(4). File permission change

Format: chmod + permission + file name
such as:

  • Change the permissions of the main.c file to be readable, writable and executable by all users: chmod 777 main.c
  • Only the file owner can read and write, and others cannot read, write and execute: chmod 400 main.c
  • Everyone has no permissions on this file: chmod 000 main.c

2. Compression command

Linux supports two commonly used compression formats

  • gzip: one of the compression formats supported by linux, the advantage is that the compression speed is faster
  • bzip2: One of the compression formats supported by linux. The advantage is that the compressed storage space takes up less space.

Use the zip command to compress files and the unzip command to decompress files.

  • zip test.zip hello.c log.c
  • unzip test.zip

Use the tar command for file compression and related operations:

  • c -> create
  • x -> release
  • z -> gzip operation
  • j -> bzip2 operation
  • v -> show progress
  • f -> Specify the file name (must be placed at the end)

Example:

  • sudo tar -czvf work.tar.gz work : Compress the work file through the gzip operation, and specify the name of the compressed package as work.tar.gz, and request to display the compression process.
  • sudo tar -cjvf work.tar.bz2 work: The difference from the above example is to use bzip2 operation for compression.
  • sudo tar -xzvf work.tar.gz extract
  • sudo tar -xjvf work.tar.bz2 decompression
  • sudo tar -xvf work.tar.gz/work.tar.bz2 (both compressed packages can be released)
  • sudo tar -xvf work.tar.gz -C Specify the directory, extract it to the specified location.

Guess you like

Origin blog.csdn.net/jiang1126/article/details/132048255