Windows Commmand Line Tips

Lately I have been spending a lot of time at the command line so I thought I’d share some useful commands, shortcuts, tips and maybe some lesser known features and write it down for future reference.

In no particular order …

Ctrl + C - Terminate currently executing command

When a command is taking unexpectedly long time or you accidentally enter something like C:\>dir /s then this is the solution.

tree

Shows nicely formatted folder structure. Below is a sample of how it looks like showing new aspnetcore app.

C:\Users\Thomas\Desktop\aspnetcoreapp>tree

C:.
├───Controllers
├───Models
├───obj
├───Properties
├───Views
│   ├───Home
│   └───Shared
└───wwwroot
    ├───css
    ├───images
    ├───js
    └───lib
        ├───bootstrap
        │   └───dist
        │       ├───css
        │       ├───fonts
        │       └───js
        ├───jquery
        │   └───dist
        ├───jquery-validation
        │   └───dist
        └───jquery-validation-unobtrusive

|

Pipe - send output from one command to another. tree | clip

clip

Send output of a command to the Windows clipboard. After executing the previous sample dir | clip I can now Ctrl + V the tree ouput anywhere I need to.

Ctrl + Shift + Scroll on your mouse

Changes transparency of the Console window.

title

Set the window title. Makes navigation between multiple command line windows much easier. Example use:

title logging

F11 or Alt Enter

Toggle fullscreen mode. Good if you have multiple monitors and can dedicate one the command line window.

type

Type file. Analogous to the cat command on unix. Displays contents of the specified file. Example use:

type readme.txt

> and >>

Redirection. Usually used when outputing to file. Difference between > and >> is that the former overwrites the specified file and the latter appends to it. Example use:

echo test1 > test.txt
echo test2 > test.txt
echo test3 >> test.txt
type test.txt

which outputs …

test2
test3

& and && and ||

Chain commands. Example use:

echo foo & echo bar

which outputs …

foo
bar

Why 3? Each one works slightly differently

%1 (%2, %3, …)

Accessing arguments passed to a batch file. Example use inside batch file:

set arg1=%~1

The extra ~ removes enclosing quotation marks.

if exist

Check if file exists. Example use:

if exist "some-file.txt" (
    echo file is there
) else (
    echo oh no, file is missing

    REM set errorlevel and exit
    exit /b 1
)

%errorlevel%

Special variable holding error level of the last executed command. By convention anything not 0 is considered an error. Example use:

if %errorlevel% neq 0 exit /b

For more comprehensive list of commands and options check out this wiki book

Comments

comments powered by Disqus