[Introduction to Linux]---Linux instructions ②

Common commands for Linux systems

1.man command

LinuxThe command has many parameters, and it is impossible for us to remember them all. We can get help by checking the online manual. The command to access Linuxthe manual page is
the syntax: man [选项] 命令
Common options
-kSearch online help based on keywords Search
numonly in chapters Display all chapters. For example, it starts searching from the first chapter by default and stops when it is known. Use options. When pressed q exits, he will continue to search later until all chapters are searched. To explain, the above manual is divided into 8 chapters. They are ordinary commands, system calls, etc. (Through this , you can at least easily find out what header files need to be added to call this function). They are library functions, such as special ones. Files, that is, the various device files under it, refer to the format of the file. For example , it will explain the meaning of each field in this file. It is reserved for games and is defined by each game. It is an attachment and has some variables, such as this Global variables are explained here. They are commands for system management. These commands can only be used by. For example, we can use the man command to find the commands we have learned before. When we need to find the use of the printf function in C language, we need to specify the first Manual No. 3, because Linux also has instructions, the command starts searching from manual No. 3 by default. If found, the usage of the search instruction will be directly displayed without continuing to search! Command search results:num
-aman printfa

1
2openwrite
3printf,fread
4/dev
5passwd
6
7environ
8rootifconfig
Insert image description here
ls\mkdir\rm
Insert image description here
Insert image description here
Insert image description here
printfman1
man printf
Insert image description here
Insert image description here
man 3 printfInstruction search results:
Insert image description here
XshellIf some instructions on the machine cannot be queried, the classic version of the man instruction that may be used can be installed using the following instructions.

yum install -y man-pages

2.echo

The echo command is used to print the value of a shell variable in the shell, or directly output a specified string. In layman's terms, echo 字符串the string will be output to the specified file, and the echo command is enabled by default \n.
Syntax: echo(选项)(参数)
Options
-e: Activate escape characters.
When using -eoptions, if the following characters appear in the string, they will be specially processed and will not be output as ordinary text:

\aSound a warning;
\bdelete the previous character;
\cproduce no further output (characters after \c will not be output);
\fwrap to a new line but the cursor remains at the original position;
\nwrap to a new line and move the cursor to the beginning of the line;
\rmove the cursor to the beginning of the line, but not Line break;
\tinsert tab;
\vsame \fas;
\\insert \character;
\nnninsert the character nnnrepresented by (octal) ASCII;
Insert image description here
echo 字符串won't the string be output (written) to the file? So if we don't specify the output (write) file, shouldn't it be output to the automatically generated file by default? Why is it output to the monitor?

在Linux下,一切皆文件。Our monitor can only write but not read files, the keyboard can only read but not write files, and other peripherals such as network cards are also files; so when we use the echo command without specifying a file to write a string, it will The string is written to the monitor file, that is, the echo command will write the string to the monitor file by default.

>For output redirection, you can use it with the echo command to write the string to the specified file. If the specified file does not exist, the file will be created and the string will be written to the file; if the specified file exists, it will be cleared. Then write the string content after the file.

echo 字符串 > 指定文件名

The result of the demonstration is:
Insert image description here

catThe command will check the contents of the file (explained later)

>Common usage of output redirection : >文件名if the file does not exist, the file will be created; if the file exists, the file will be cleared.
>>For append redirection, echowhen used with the command, echo 字符串 >> 文件名the string will be appended to the end of the file.
Insert image description here
Common usage of append redirection >>: >>文件名If the file does not exist, the file will be created; if it exists, nothing will be done!

3.cp command (important)

Syntax: cp [选项] 源文件或目录 目标文件或目录
Function: Copy files or directories
Description: The cp command is used to copy files or directories. If more than two files or directories are specified at the same time, and the final destination is an existing directory, it will copy all previously specified files or directories. Files or directories are copied into this directory. If multiple files or directories are specified at the same time, and the final destination is not an existing directory, an error message will appear.
Common options:
-f 或 --force Forcibly copy the file or directory, regardless of whether the destination file or directory already exists. Ask the user to recurse
-i 或 --interactivebefore overwriting the file.
-rProcess, process the files and subdirectories in the specified directory together. If the form of the source file or directory does not belong to a directory or symbolic link, it will be treated as an ordinary file for recursive processing,
-R 或 --recursiveand the files and subdirectories in the specified directory will be processed together
cp. Common operation instructions:
Insert image description here
cp -R 拷贝的多级目录名 源目录名Recursive copy:

Insert image description here

4.mv command (important):

mvCommand is movethe abbreviation of . It can be used to move files or rename files (move (rename) files). It is a commonly used command in Linux systems and is often used to back up files or directories.
Syntax: mv [选项] 源文件或目录 目标文件或目录
Function:

  1. Depending mv命令on the type of the second argument (destination file or destination directory), mv命令the file is renamed or moved to a new directory.
  2. When the second parameter type is a file, mv命令complete the file renaming. At this time, there can only be one source file (it can also be the source directory name). It will rename the given source file or directory to the given target file. name.
  3. When the second parameter is the name of an existing directory, there can be multiple source files or directory parameters, and the mv命令source files specified by each parameter will be moved to the target directory.

Commonly used options:
-f : forceMandatory meaning, if the target file already exists, it will be overwritten directly without asking
-i: If the target file ( destination) already exists, it will be asked whether to overwrite it!
mv 文件名 ..(上级路径)Cut file demo:
Insert image description here
mv 文件名 新的文件名Perform file renaming demo Demo:
Insert image description here

mv 文件名 目录\新文件名Demonstration of cutting and renaming files:
Insert image description here
Question: Why can the mv command rename files for a while, cut files for a while, and cut and rename for a while?

mv 旧文件名 新的文件名In fact, it is also a cut and rename function, but the path is not specified, and the default is to cut to the .current path, that is mv 旧文件名 ./新的文件名; mv 旧文件名 路径it is also a cut and rename function, but a new file name is not specified, and the original file name is used by default, that is mv 旧文件名 路径/旧文件名.

5.alias command

Usage: Used to set the alias of the command.
Syntax: alias(选项)(参数)
Options
-p : Print the command alias that has been set.
Parameters: 命令别名设置:定义命令别名,格式为“命令别名=‘实际命令’” .
Supplementary instructions:
alias命令Used to set the alias of the command. We can use this command to simplify some long commands. When using alias, the user must use single quotes '' to surround the original command to prevent special characters from causing errors.
②The alias命令function is limited to the login operation. If you want to be able to use these command aliases every time you log in, you can alias命令store the corresponding ones in bashthe initialization file /etc/bashrc.

alias -pInstructions View renaming instructions on Linux machines:
Insert image description here

Use nano指令the edit file as follows:

#include<stdio.h>
int main()
{
    
     
    int	cnt=10;
    while(cnt--)
    {
    
    
        printf("hello Linux:%d\n",cnt);
    }
    return 0; 
}

The result of the xshell machine demonstration is:
Insert image description here
Insert image description here
Use which指令to view the storage location of other instructions:
Insert image description here
Use lsto view the file name under the Linuxspecific path of the system/usr/bin
Insert image description here

Use the command to rename aliasthe executable filea.out
Insert image description here
: In the Linux system, install the executable program: put the executable file we wrote in the Linux specific path /usr/bin, and you can use the program when you log in again; **Uninstall the executable program: ** /usr/binDelete the Linux specific path of the executable program file.

Summary:
① Both instructions and executable programs can be executed, so instructions are executable programs; ② When we enter instructions, we will first search for the corresponding instructions in the system; ③ The essence of executing instructions is to find them in a specific path of the system The executable file corresponding to the instruction. If found, the instruction exists and will be executed. If the instruction is found and does not exist, it will be displayed that the instruction does not exist.

6.cat command

Syntax: cat [选项][文件]
Function: View the contents of the target file.
Common options:
-b Number non-empty output lines. Number
-nall output lines. You can view the contents of the file
-s without outputting multiple blank lines . The demonstration results are as follows: Demonstration results: Demonstration results: Demonstration results: By default, the cat command reads data from the keyboard file, and prints it on the display screen as input redirection. Used in conjunction with cat, the content of the file is read and printed to the display screen.
cat 文件名
Insert image description here
cat -b 文件名
Insert image description here
cat -n 文件名
Insert image description here
cat -s 文件名
Insert image description here

Insert image description here
<
Insert image description here

7.more command

Syntax: more [选项][文件]
Function: more Command, function is similar to cat
common options:
-n number all lines of output
qExitmore

# cnt=0;while [ $cnt -le 10000 ];do echo "hello linux $cnt";let cnt++;done>log.txt
//向log.txt文件中打印10000条‘hello linux’

The more command checks the contents of the log.txt file:
Insert image description here

Press enter回车键to scroll down to view the contents of the file. You cannot use the up and down keys to scroll up and down to view the contents of the file. Press to qexit the more command!

8.less directive (important)

  • lessThe tool is also a tool for paging display of files or other output. It should be said that it is an orthodox tool for viewing file contents in Linux and is extremely powerful.
  • lessThe usage of moreis more flexible than that of . When more, we have no way to flip forward, we can only look backward. But if less is used, we can use the [pageup][pagedown]function of waiting keys to flip forward and backward through the file, which is easier to view the contents of a file!
  • In addition, lessyou can have more search functions in , not only you can search down, but you can also search up.
    Syntax: less [parameter] File
    function:
    less Similar moreto, but you lesscan use the up and down keys to browse the file at will, and moreyou can only move forward, but not backward, and lessthe entire file will not be loaded before viewing.
    Options:
    -i Ignore case when searching
    -NDisplay the line number of each line
    /字符串: Search down for "string" functionality
    ?字符串: Search up for "string" functionality
    n: Repeat the previous search (related to / or ?)
    N: Repeat the previous search in reverse A search (related to / or ?) using
    q the :quit lesscommand results in the following:
    less -N log.txt/999
    Insert image description here

cat指令Suitable for viewing small files, not suitable for viewing large files; less指令Bi more指令is more friendly and more powerful, but both are suitable for querying large files

9.head command

headAs taileasy to understand as its name, it is used to display a certain number of text blocks at the beginning or end. It is used headto display the beginning of the file to the standard output, and tailof course it is to see the end of the file.
Syntax: head [参数] [文件]
Function: head Used to display the beginning of the file to the standard output. The default headcommand prints the first 10 lines of the corresponding file.
Option:
-n<行数> Display n lines of content in the header.
head 文件名The display results are as follows:
Insert image description here
head -16 文件名Search display results are as follows:
Insert image description here

10.tail command

tailThe command writes the file to the standard output starting from the specified point. Use the -f option of the tail command to conveniently check the changing log file. The last content tail -f filenamewill be displayed on the screen, and it will not only be refreshed, so that you can see the latest filenameThe file content.
Syntax: tail[必要参数][选择参数][文件]
Function: Used to display the content at the end of the specified file. When no file is specified, it is processed as input information. Commonly used to view log files.
Options:
-f Read in a loop and
-n<行数>display the last n lines.
Insert image description here
If you want to read log.txtthe 1000th to 1020th lines in the file, what should we do based on what we have learned so far?
Step ①: head -1020 log.txt >tmp.txtOutput log.txtthe first 1020 lines of the file into tmp.txtthe file.
Use cat指令View tmp.txtFile.
Insert image description here
Step ②: tail -21 tmp.txtView the 21 lines at the end of the file.
Insert image description here
Method 2: Enter the following command.

# head -1020 log.txt|tail -21

Insert image description here

It is |a temporary pipeline file. The content on the left will flow to the pipeline file, and the data that matches the program on the right will be read into the pipeline file. Advantages: There is no need to create a temporary file, which is convenient and fast.

11. Time-related instructions

Date display:
Insert image description here
date Display time in specified format: date +%Y:%m:%d
date Usage: date [OPTION]... [+FORMAT]

1. In terms of display

Users can set the format to be displayed. The format is set to a plus sign followed by several marks. The commonly used marks are listed below.

%H : 小时(00..23)
%M : 分钟(00..59)
%S :(00..61)
%X : 相当于 %H:%M:%S
%d :(01..31)
%m : 月份 (01..12)
%Y : 完整年份 (0000..9999)
%F : 相当于 %Y-%m-%d

Insert image description here

2. In terms of setting time

date -s //设置当前时间,只有root权限才能设置,其他只能查看。
date -s 20080523 //设置成20080523,这样会把具体时间设置成空00:00:00
date -s 01:01:01 //设置具体时间,不会对日期做更改
date -s “01:01:01 2008-05-23//这样可以设置全部时间
date -s “01:01:01 20080523//这样可以设置全部时间
date -s “2008-05-23 01:01:01//这样可以设置全部时间
date -s “20080523 01:01:01//这样可以设置全部时间

3. Timestamp

Time -> Timestamp: date +%s
Timestamp -> Time: date -d@1508749502
Unix timestamp (Unix epoch, Unix time, POSIX time or Unix timestamp in English) is the number of seconds that have elapsed since January 1, 1970 (midnight UTC/GMT) Number, leap seconds are not taken into account.
Insert image description here

12.Cal instructions

cal命令Can be used to display the Gregorian (Gregorian) calendar. The Gregorian calendar is the calendar currently used internationally, also known as the Gregorian calendar and commonly known as the Gregorian calendar. The "Gregorian calendar", also known as the "solar calendar", is based on the earth's orbit around the sun as one year. It is common in Western countries, so it is also called the "Western calendar".
Command format: cal [参数][月份][年份]
Function: Used to view calendar and other time information. If there is only one parameter, it represents the year (1-9999). If there are two parameters, it represents the month and year. Common options: display the
previous
-3 month and current month of the system, The monthly calendar of the next month
-jdisplays the number of days in the year (the date of the year is calculated by days, starting from January 1st, and the number of days of the current month in the year is displayed by default). The calendar of the current year is displayed. The xshell demonstration result
-yis
:
Insert image description here

13.find command

  • The find command under Linux searches for files in the directory structure and performs the specified operation.
  • The find command under Linux provides quite a few search conditions and is very powerful. Because find has powerful functions, it also has many options, most of which are worth our time to understand.
  • Even if the system contains a Network File System (NFS), the find command is also valid in the file system, as long as you have the corresponding permissions.
  • When running a very resource-consuming find command, many people tend to execute it in the background, because traversing a large file system may take a long time (here refers to a file system of more than 30G bytes).
    Syntax: find pathname -options
    Function: Used to search for files in the file tree and perform corresponding processing (possibly accessing the disk)
    Common options:
    -name Search for files by file name. Search for files ending
    find ~ -name *,cwith in the home directory. We know that commands and commands can also be searched. These two What is the difference between an instruction and a directive? Command: You can search for files under the specified path, such as generally searching in disk files. ②Command : You can search for commands in the system default path /usr/bin/. ③Instruction : Search in the system-specific path, you can find not only executable program files (instructions), but also manuals, installation packages, compressed packages, etc..c
    Insert image description here
    whichwhereisfind
    find
    which
    whereis

14. grep command

Syntax: grep [选项] 搜寻字符串 文件
Function: Search for a string in the file, print out the found lines, and use it as a line text filtering tool.
Common options:
-i: Ignore the difference in case, so the case is treated as the same
-n: By the way, output the line number
-v: Reverse selection, also That is, the xshell demonstration result of the line without "search string'" content is shown as follows:

Insert image description here

15.zip/unzip instructions:

WindowsThe system also has the function of compressing and decompressing files. Why does this function exist?
Advantages: ① Packing and compressing a whole file composed of multiple files, it is not easy to cause the loss of files; ② The size of the packed and compressed files becomes smaller, the storage space occupied becomes smaller, and the download time becomes shorter!
Syntax: zip 压缩文件.zip 目录或文件 , can only compress a single directory/file. To process multi-directory files, you need to bring ‘r’options
: compress directories or files into zip format.
Common options:
-r recursive processing, process all files and subdirectories in the specified directory together.
Insert image description here
You can see After compressing d1multi-level directories and test.txtfiles d1.zip, the size is significantly reduced, making it easier for us to perform operations such as copying and cutting.
unzip指令Syntax: unzip 压缩文件.zipUse -doptions to specify decompression to a specified folder. If you do not specify a decompression folder, it will be decompressed to the current folder.
Insert image description here
Insert image description here
If you friends are still unable to use zip\unzipthe instructions, there may be no installation zip\unzipinstructions. You can use the following instructions to install:

# yum install -y zip unzip//使用root账户安装

16.tar command (important)

Syntax: tar [-cxtzjvf] 文件与目录 ....
Parameters:
-c : Parameter instructions for creating a compressed file (meaning create);
-x: Parameter instructions for decompressing a compressed file!
-t: View tarfilethe files inside!
-z: Does it also have gzipthe attributes of ? That is, do I need to use gzipcompression?
-j: Does it also have bzip2the attributes of ? That is, do I need to use bzip2compression?
-v: Display files during compression! This is commonly used, but it is not recommended to be used in background execution processes!
-f: When using a file name, please note that fthe file name must be entered immediately after ! Don't add any more parameters! : Commonly used combination options for
-C decompressing to a specified directory : : Pack and compress the specified files and directories into a compressed package : Preview the contents of the compressed package. : Decompress the contents of the compressed package to the current folder . Note: is the packaging suffix, is the compression suffix, and the packaging and compression suffix is , which can be abbreviated as

tar -czf xxx.tgz 指定文件 指定文件(目录)...xxx.tgz
tar -tzf xxx.tgzxxx.tgz
tar -xzf xxx.tgzxxx.tgz
targz.tar.gztgz
Insert image description here

17.bc instruction

bcThe command can easily perform floating point calculations
Insert image description here

18.uname -r command

Syntax : uname [选项]
Function: uname Used to obtain information about the computer and operating system.
Supplementary instructions: Basic information such as the version of the operating system used by the host and the name of the hardware unamecan be displayed . Common options: Output all information in detail, in order: kernel name, host name, kernel version number, kernel version, hardware name, processor type, hardware platform type, operating system name: display the release number of the operating system;linux

-a或–all
-r或--release
Insert image description here

19. Shut down

Syntax: shutdown [选项]
Common options:
-h : Immediately shut down the system after stopping its services.
-r: Restart the system after stopping the service
-t sec: -tAdd the number of seconds after it, which means "shut down after a few seconds"

20. Several important hot keys

[Tab]按键- It has the functions of "Command Completion" and "File Completion"
[Ctrl]-c按键-- "Stop" the current program. When the program is abnormal and we are unable to enter commands, we can press ctrl+c without thinking to terminate the current program. program.
[Ctrl]-d按键- usually means: "End Of File, EOF or End OfInput"; in addition, it can also be used to replace exit
[ctrl]-r按键- history search command. Entering the key letters of the command will automatically match the commands that have been entered. historyCommands can find the 1000 most recently used commands.

Guess you like

Origin blog.csdn.net/m0_74288306/article/details/132916906