[Linux]——Basic Operation Instructions (2)

 =========================================================================

Homepage

code repository

C language column

Elementary data structure column

Linux column

LeetCode brush questions

Algorithm column 

=========================================================================

Table of contents

Preface

man command

cp command

mv command 

echo command

cat command

more command

less command

head and tail instructions

head command

tail command


Preface

The last article explained some basic operation instructions in the Linux environment to give you a little understanding of the instructions. Today I will bring you a few instructions to make you more familiar with using instructions.


man command

Linux commands have many parameters, and it is impossible for us to remember them all. We can get help by checking the online manual. Visit the Linux man page.
Syntax: man [options] command

If you cannot use the man command, you can enter the command

yum install -y man-pages

Install the man executable program.

 For example: we enter man pwd to view the manual for the pwd command.

In this way, we can find the usage instructions of pwd. In the man command, we press the q key to exit the search.

We found that there is a serial number 1 after pwd. There are a total of nine manuals in man. We will traverse the search starting from the first one and display it when found. We can use the man man command to view it.

Manual No. 1: Display the basic usage of viewing executable programs or instructions

Manual No. 2 will explain it to you later.

Manual No. 3: Usage of library functions, mainly C language.

You will basically not need the following manuals, so I won’t explain them in detail here.

In Linux printf is an executable program

Then we can also use the man 3 printf command to query the usage of printf in C language


cp command

Syntax: cp [option] Source file or directory Target file or directory
Function: Copy file or directory
Description: The cp command is used to copy files or directories. If two or more files or directories are specified at the same time, and the final destination is an already directory exists, it will copy all previously specified files or directories to 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.


Commonly used options:
-f or --force forcefully copies a file or directory, regardless of whether the destination file or directory already exists
-i or --interactive asks the user before overwriting the file
-r recursively processes the files in the specified directory with the subdirectory and processed. 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 processing
-R or --recursive recursive processing, and the files and subdirectories in the specified directory will be processed together.

We know that files or directories with the same name cannot exist in the same directory , so we cannot copy a file in the same directory. We can only copy it in the same directory by adding a suffix when copying the file. 

Note: The copy command does not copy hidden files in the directory.


mv command 

The mv command is the abbreviation of move . 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 [option] Source file or directory Target file or directory
Function:
1. Depending on the type of the second parameter in the mv command (whether it is a target file or a target directory), the mv command renames the file or moves it to a in the new directory.
2. When the second parameter type is a file, the mv command completes 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 one. specified 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 command will move all the source files specified by each parameter to the target directory.

After moving, the original directory will be deleted.

 

When the moved target directory does not exist, the directory name will be modified.


echo command

Syntax: echo XXX

Function: Output XXX

Syntax: echo filename

Function: Write to the file (when the file exists, write directly to the file, if it does not exist, create the file and write)

As shown above: the file test_echo does not exist, it will be automatically created and written.

The > here is output redirection .

When we want to write more content, we find that each input will not be saved. Here we need to use >> to append the redirection symbol.


cat command

Syntax: cat [option][file]
Function: View the contents of the target file
Common options:
-b Number non-empty output lines
-n Number all output lines
-s Do not output multiple blank lines

Here we append a few sets of spaces to the previous file, and finally enter a string of characters.

The tac command here is just the opposite of cat, inputting the contents of the file in reverse.


more command

Syntax: more [option][file]
Function: more command, similar to cat in function 

Here we first use a loop to create a large file.

After using the more command, the file will be read and the file will continue to be read as the Enter key is pressed. Press the q key to exit.

Only the first 10 lines are read.


less command

The less 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.
The usage of less is more flexible than that of more. When using more, we have no way to turn forward and can only look back. But when using less, we can use the [pageup][pagedown] and other key functions to flip back and forth through the file, which is easier to use for viewing. The contents of a file!
In addition, you can have more search functions in less, not only you can search down, but you can also search up.
Syntax: less [parameter] File
function:
less is similar to more, but you can use less to browse the file at will, while more can only move forward, but not backward, and less will not load the entire file before viewing.
Options:
-i ignore case when searching
-N display line number for each line
/ string: search down for "string" function
? string: search up for "string" function
n: repeat previous search ( related to / or ?)
N: Repeat the previous search in reverse (related to / or ?)
q:quit 

When using the less command to read the price, you can use the up and down keys to view the file forward or backward.


head and tail instructions

head command

Head and tail are as easy to understand as their names. They are used to display a certain number of text blocks at the beginning or end. Head is used to display the beginning of the file to the standard output, while tail is used to read the file. end.
Syntax: head [parameter]... [file]...
Function: head is 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.

Options: -n<number of lines> Number of lines to display

tail command

The tail 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. tail - f filename will display the last content of filename on the screen, and not only refresh , so that you can see the latest file content.
Syntax: tail [required parameters] [select parameters] [file]
Function: used to display the content at the end of the specified file. When no file is specified, it will be processed as input information. Commonly used to view log files.

Options:

-f loop reading
-n<number of lines> display the number of lines


Thinking: We want the first 10 of the 500 numbers among these 1,000 numbers. How should we do this?

Method 1: Take out the first 510 numbers and put them into a new file, and then take out the last 10 numbers from the new file.

Method 2: Use pipes 

This | symbol is a command line pipe, in this way we can solve the problem. Later in the study, we will talk about pipelines.

Guess you like

Origin blog.csdn.net/qq_55119554/article/details/133442851