Article directory
- Common commands for Linux systems
-
- 1.man command
- 2.echo
- 3.cp command (important)
- 4.mv command (important):
- 5.alias command
- 6.cat command
- 7.more command
- 8.less directive (important)
- 9.head command
- 10.tail command
- 11. Time-related instructions
- 12.Cal instructions
- 13.find command
- 14. grep command
- 15.zip/unzip instructions:
- 16.tar command (important)
- 17.bc instruction
- 18.uname -r command
- 19. Shut down
- 20. Several important hot keys
Common commands for Linux systems
1.man command
Linux
The 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 Linux
the manual page is
the syntax: man [选项] 命令
Common options
-k
Search online help based on keywords Search
num
only 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
-a
man
printf
a
1
2
open
write
3
printf,fread
4
/dev
5
passwd
6
7
environ
8
root
ifconfig
ls\mkdir\rm
printf
man
1
man printf
man 3 printf
Instruction search results:
Xshell
If 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 -e
options, if the following characters appear in the string, they will be specially processed and will not be output as ordinary text:
\a
Sound a warning;
\b
delete the previous character;
\c
produce no further output (characters after \c will not be output);
\f
wrap to a new line but the cursor remains at the original position;
\n
wrap to a new line and move the cursor to the beginning of the line;
\r
move the cursor to the beginning of the line, but not Line break;
\t
insert tab
;
\v
same \f
as;
\\
insert \
character;
\nnn
insert the character nnn
represented by (octal) ASCII
;
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:
cat
The 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, echo
when used with the command, echo 字符串 >> 文件名
the string will be appended to the end of the file.
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 或 --interactive
before overwriting the file.
-r
Process, 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 或 --recursive
and the files and subdirectories in the specified directory will be processed together
cp
. Common operation instructions:
cp -R 拷贝的多级目录名 源目录名
Recursive copy:
4.mv command (important):
mv
Command is move
the 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:
- 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.- 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.- 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
: force
Mandatory 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:
mv 文件名 新的文件名
Perform file renaming demo Demo:
mv 文件名 目录\新文件名
Demonstration of cutting and renaming files:
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 ismv 旧文件名 ./新的文件名
;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 ismv 旧文件名 路径/旧文件名
.
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 bash
the initialization file /etc/bashrc
.
alias -p
Instructions View renaming instructions on Linux machines:
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:
Use which指令
to view the storage location of other instructions:
Use ls
to view the file name under the Linux
specific path of the system/usr/bin
Use the command to rename alias
the executable filea.out
: 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/bin
Delete 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
-n
all 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 文件名
cat -b 文件名
cat -n 文件名
cat -s 文件名
<
7.more command
Syntax: more [选项][文件]
Function: more
Command, function is similar to cat
common options:
-n
number all lines of output
q
Exitmore
# 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:
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 toq
exit the more command!
8.less directive (important)
less
The 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.less
The usage ofmore
is more flexible than that of . Whenmore
, 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,
less
you can have more search functions in , not only you can search down, but you can also search up.
Syntax: less [parameter] File
function:
less
Similarmore
to, but youless
can use the up and down keys to browse the file at will, andmore
you can only move forward, but not backward, andless
the entire file will not be loaded before viewing.
Options:
-i
Ignore case when searching
-N
Display 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 :quitless
command results in the following:
less -N log.txt
/999
cat指令
Suitable for viewing small files, not suitable for viewing large files;less指令
Bimore指令
is more friendly and more powerful, but both are suitable for querying large files
9.head command
head
As tail
easy to understand as its name, it is used to display a certain number of text blocks at the beginning or end. It is used head
to display the beginning of the file to the standard output, and tail
of 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 head
command 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:
head -16 文件名
Search display results are as follows:
10.tail command
tail
The 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 filename
will be displayed on the screen, and it will not only be refreshed, so that you can see the latest filename
The 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.
If you want to read log.txt
the 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.txt
Output log.txt
the first 1020 lines of the file into tmp.txt
the file.
Use cat指令
View tmp.txt
File.
Step ②: tail -21 tmp.txt
View the 21 lines at the end of the file.
Method 2: Enter the following command.
# head -1020 log.txt|tail -21
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:
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
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.
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
-j
displays 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
-y
is
:
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 *,c
with 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
which
whereis
find
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:
15.zip/unzip instructions:
Windows
The 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.
You can see After compressing d1
multi-level directories and test.txt
files d1.zip
, the size is significantly reduced, making it easier for us to perform operations such as copying and cutting.
unzip指令
Syntax: unzip 压缩文件.zip
Use -d
options to specify decompression to a specified folder. If you do not specify a decompression folder, it will be decompressed to the current folder.
If you friends are still unable to use zip\unzip
the instructions, there may be no installation zip\unzip
instructions. 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 tarfile
the files inside!
-z
: Does it also have gzip
the attributes of ? That is, do I need to use gzip
compression?
-j
: Does it also have bzip2
the attributes of ? That is, do I need to use bzip2
compression?
-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 f
the 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.tgz
xxx.tgz
tar -xzf xxx.tgz
xxx.tgz
tar
gz
.tar.gz
tgz
17.bc instruction
bc
The command can easily perform floating point calculations
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 uname
can 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
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
: -t
Add 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. history
Commands can find the 1000 most recently used commands.