Introduction to the use of CMD command line pipeline command

Time:2020-10-18

1、 What are pipeline commands

The pipeline command can filter the execution result of a command and keep only the information we need.
If the dir command will display all the folders and files in the directory, you can use the pipeline command | findstr “” to filter the results of dir and keep only the information needed

|Command pipeline

Format: first order | second order [| third order…]
Use the result of the first command as an argument to the second command. Remember that this is common in UNIX.

For example:

dir c:\|find “txt”

The above command is: find all C: \, and find the txt string.

Please check the function of find by yourself with find /

This is how I automatically format a disk when I don’t make format’s auto format parameters
echo y|format a: /s /q /v:system
All of you who have used format know that you need to enter y to confirm whether it is a grid. This command is preceded by echo y and uses the character |
Is passed to the format command
So as to achieve the purpose of inputting y automatically
(this command is dangerous, please be careful when testing)

The most commonly used output y to the following statement

echo y|delete *.txt /s

2、 Use of pipe command

1. Windows netstat view port and process usage

//If we need to determine who's using our port 9050

C:/>netstat -aon|findstr "9050"
TCP 127.0.0.1:9050 0.0.0.0:0 LISTENING 2016
//See? The port is occupied by a process with process number 2016. Continue to execute the following command:
C:/>tasklist|findstr "2016"
tor.exe 2016 Console 0 16,064 K
//It's clear, tor.exe  It's taking up your port

2. View folder directory structure

tree > list.txt

tree /f >list.txt

3. I / O redirection

Echo – C / / – C: does not wrap, Linux is – n

I / O redirection:
For each naming, there are three related files. Standard input file, standard output file, error output file
Because under UNIX / Linux, any device is treated as a file. In fact, all standard input files are keyboards,
Standard output and error output files are screens
>Output redirection
< input redirection
< < input append
>>Input append
File descriptor:
Each file can be described by a file descriptor. The system provides 12, and 3-9 can be defined arbitrarily. 0-2 system definition is as follows:
0 standard input keyboard
1. Standard output display
2. Error output display
That is, 0-2 can be used to represent the above three files
For example, the output of CMD > file / / CMD is redirected to the file file
The output of CMD > > file / / CMD is appended to the file, otherwise it is created
CMD 1 > file / / redirect the standard output of the shell running CMD to file, and 1 represents the standard output
CMD > file 2 > & 1 / / redirect the output of CMD to file and 2 (error output) to 1 (standard output)
If 2 > 1, 1 will be regarded as a file, and it is necessary to use & 1 to direct errors to 1
CMD 2 > file / / direct the error output to file
CMD > > file 2 > & 1 / / append error and CMD output to file, CMD > > file, append; 2 > & 1, error directed to
Standard output and standard input are appended to the file, so both error and CMD output are appended to the file
CMD 1 > > file 2 > & 1 / / this command can be equivalent to the previous command
The input of cmdfile2 / / CMD is file1, and the result is output to File2
CMD < < end / / CMD uses standard input until end is encountered. End can be any character
Understanding: CMD uses the default standard input, run, when encountering end, it will be appended, but the standard input and output
Unable to append, so error exit
Note: according to the statement in Chapter 25 of Linux shell programming,
This is the syntax defined by the shell. It can be directly interpreted as end as the separator
CMD < & M / / take the file descriptor m as the input of CMD
CMD > & M / / take the file descriptor m as the output of CMD
CMD < & – / / turn off standard input
!! Note:
1. Do not add a space before the input and output directional character, otherwise unexpected problems may occur
2. 0 1 2 cannot use > > or < < directional character, for example: 2 > > & 1 error

Use the pipe character to enter Y / N automatically

Sometimes, when calling the CMD command, the prompt message will appear. For example, when using PSCP, the prompt message will appear as follows:
Store key in cache?(y/n)
You need to enter y or n every time. If you call the CMD command in Java or python, you will not be able to execute it. What can I do? You can use the pipe symbol to do this, such as the command PSCP. You can fill in Y automatically by using the following method:

echo y |pscp ……

The ellipsis at the end is the operation command;
Other language calls can also run normally

This article on the use of the CMD command line pipeline command to introduce this, more related content of the CMD pipeline, please search the previous article of developeppaer or continue to browse the related articles below, I hope you can support developeppaer more in the future!