⬆️ ⬇️

Windows command line subtleties

Recently, I grew up from a fierce enikee in a very large company, to a modest sysadmin who supervises a 10-pc network. And, as a very lazy sysadmin, faced with the tasks of automating their activities. Six months ago, I did not know that there are pipelines in the Windows command line. This was the first shocking discovery. And I went further, and it turned out that where I used to write utilities in C #, Delphi or cumbersome scripts with nested loops, it was possible to get forfiles couple of forfiles or robocopy .

I will not talk about banalities, such as the transfer of files and folders by pressing Tab . Under habrakat I will tell that can be useful to beginning administrators and enikeyam.



Hotkeys


Let's start with hotkeys, because you first need to learn what the working environment can give us.



F1 - In the console, this key works exactly like the right arrow, i.e. displays one character from the last command entered (or selected in the history).

F2 + <character> - Displays the last command entered before the specified character. For example, if the last command you entered looked like this:

 ping 192.168.25.1 


then after pressing the F2 + 5 key combination you will receive:

 ping 192.168.2 


F3 - Displays the last, and only the last, command in the entire history.

F5 - Displays the last commands entered in order, as well as the up arrow.

F6 - Inserts the EOF character at the current command line position, which is similar to pressing Ctrl + Z.

F7 - Dialog box containing command history.

image

Alt + F7 - Clears command history.

<character (s)> + F8 - Enumerates commands that begin with the characters already entered in the command line.

If you do not enter anything before pressing F8 , then this key will work like the up arrow, but with a slight difference - the lines will be searched in cycles, i.e. after the first command from the list, the last one will be displayed.

F9 + <number> - Inserts a command from the history under the corresponding number. For example, in the situation shown in the screenshot above, when you press the F9 + 4 combination, the console will display:

 ipconfig 


')

Command line operators


I, long ago, when I was small, did not even know how to work in the console without a graphical interface. After all, the output of commands sometimes takes dozens of pages, and if you need to select some data from there, then the paginal output will not save. But once I put the old FreeBSD comp on, opened the handbook, and just went around my head from the possibilities that had opened up. There you can redirect the output of the command to the input of another command and this is called a pipeline.



The operator of the pipeline in * nix and cmd, is the vertical bar symbol.

 | 


For example, the output of all text files in the current folder will be shown by the command

 dir | find ".txt" 




Command unification operator


 & 


Example: Team1 & Team2 - first, Team1 will be executed, and only then Command2



Operator and


 && 


Example: Command1 && Command2 - Command2 will be executed only if the successful execution of Command1



Operator OR


 || 


Example: Team1 || Team2 - Team2 will be executed only if Command1 could not be executed.



For grouping commands, parentheses are used, examples:





Thanks for attention! I look forward to criticism and suggestions ...



UPD1


For those who are not in the subject, circumflex (this is the “^” sign) means pressing the key with Ctrl (^C = Ctrl +C ).



^C - Interrupts the command, well, everyone knows that.

^S - Suspends the execution of a command, and then starts it.

^I - Analog Tab, iterates through folders and files.

^M - Analogue of Enter.

^H - Analog Backspace.

^G - Having written the echo ^G command in a batch file, you can spike with the system speaker (speaker).

(The commands ^I and ^H , are received by me by the method of "scientific spear", there are still ^J but I don’t know what she is doing)



PS Other subtleties of the Windows command line have already been repeatedly covered on Habré. And I see no reason to save and paste.

PPS Links to interesting posts and articles on other Windows command line features:

I / O, cycles, variables

Work with arrays

Interesting topic on the topic

Source: https://habr.com/ru/post/218759/



All Articles