DEV Community

Sibusiso Dlamini
Sibusiso Dlamini

Posted on • Updated on

10 CMD tips and tricks

I think I'm a little obsessed with the windows command line. It all started when I began learning Python. I would learn a command to do something and then just kept picking up commands here and there. Now I almost live in the terminal. It is the first application that I open everyday. I believe I have reached a level terminal wizardry where I almost never have to reach for my mouse and or touch pad.

There are plenty commands that I know of and even more that I am yet to discover but for now I only want to discuss the commands that I personally found useful and use on a regular basis.

Basics 👶

If you are just starting learn the command line yourself you should know the commands

  • cd
  • dir
  • mkdir
  • del and
  • rmdir

These are basic commands for navigating, creating and removing files and folders. I'm going to assume that you know these and what they do as well as what flags are.

Now that that is out of the way. Let's get into my terminal tips and tricks.

My Goto Commands 🙃

1. Help ❓

First up is the help command. This is just about as useful as a command gets. Simply enter help in the terminal and you should see a list of basic commands and a brief description of what they do. Here's an example of what the output would look like.

help command

Whenever you stumble across a new command you and want to know more about it and how to use it, you can try help followed by that new command or use the /? flag with the command. This should give you a more in depth explanation of that new command, and examples of how to use of it.

help assoc command

2. Cls

This command clears the terminal. If you ever just get annoyed with a long list of output and just want to get rid of without having to start a new terminal session, you can instead use cls and this way you keep the history of commands that you entered. Here is what that would look like!

clearing console gif

I can't emphasis the cls command enough. It is so simple but effective. I don't know why but I can't go 2 seconds without clearing my console. Every now and then I just find myself entering cls into the google search bar out of habit😂.

When I started out using the command line no one really told me about this command. I think it is so basic that they might have forgotten about. So from me to you, have fun with this one. Honestly, if you know cd and dir and cls you're already halfway to terminal wizardry.

3. Tab

Everyone knows that if you press the up and down arrow keys you can revisit your command history, but no one really speaks about the power of the Tab key. The tab key cycles through your files and folders within you current directory. It also predicts what file you are trying to refer to. So if you have long file or folder names and you want to reference it in a command all you need to do is type in the first 1 or 2 letters and hit Tab and CMD will predict which file you want to reference in your command.

gif of tab

Note📝 If you enter a file as a command into the prompt, windows will open the file with its default application. For example, if I were to type index.html into the terminal, windows will open sublime text and load the HTML file. This is one of the reasons why command prompt is the first application I start up everyday.

4. Start

The command, start, by itself will simply start a new terminal session. But if you pass it in a parameter it will start that specific program. Enter start+ nameofprogram.exe and you the program will startup. And this is another reason why I always start the day by opening the terminal. From there I just navigate to the files I want to open and open the programs I need to work with 🙃.

5. Tasklist

This command displays a list of all current running processes or tasks. I like to think of it as a task manager within the command prompt. I find it useful when I want to know the name of a programs executable so that I start it up with start

Tasklist is also useful for determining which programs are memory hogs and determine which processes are still running that you should not be. If you ever accidently write up a python script that starts an infinite loop, you can see that python file running in the tasklist command.

tasklist terminal output

Hint: A programs' executable filename will usually be the name of the programe with a '.exe' extension. E.g. Firefox is simply firefox.exe and chrome is chrome.exe. But there are exceptions like Microsoft word, which is winword.exe, and Microsoft powerpoint, which is powerpnt.exe. If you wanted to startup these applications using the start command, you only need to pass in the base of the filename of a program as a parameter. For example

start chrome
start firefox
start powerpnt
start winword
Enter fullscreen mode Exit fullscreen mode

6. Taskkill

Without tasklist, this command wouldn't be very useful. It is used to terminate processes or programs. Just like we need the name of a programs exectuble to start that program we need the name of it to close it with taskkill.

To terminate a task according to its process id you use the /pid flag and to terminate a task according to its name the you use the /im flag. I think the im stands for image. Or at least that's how I remember it.

The scenario which you would find me using this command the most is to terminate a program that is NOT RESPONDING! 🙄 I don't know why after I click the exit button aggressively, windows doesn't get the hint that I want to close the program. And it has the audacity to ask me whether I want to wait for the program to respond!

gif of frustration

Anyway, if the taskkill is not working, you can try force closing the application. We can use the /f flag to force close programs. For example.

taskkill /f /im powerpnt.exe
taskkill /f /pid 2020
Enter fullscreen mode Exit fullscreen mode

7. Echo

I take it you already know this command, but I'll go over it because I think its special. Now we all know that echo simply prints output to the terminal but did you also know that it can be used to create and overwrite or append to files?

The syntax is as follows

echo Hello world! > Hello.txt
Enter fullscreen mode Exit fullscreen mode

After entering the above command in the terminal you should see a file called Hello.txt in your current directory even if it was not there before. If you then open the file "Hello.txt", it should contain the text "Hello world!".

8. Type

So many times I have come across a tutorial where the tutor uses Linux and they majestically creates files using the touch command. It took me a while but I finally found the windows alternative to touch. Now, echo and type are similar but they are different so do not confuse the two.

What type does is print the contents of a file to the terminal. In other words here is a standard HTML file.

Picture of standard html file

And here is what gets output to the terminal when I enter type index.html in the terminal.

Type index.html terminal ouptut

But this has got nothing to do with creating files. So why mention it? Knowing the purpose behind types helps distinguish the difference between the type and the echo commands. Just to reiterate, Type displays contents of a file and can create a new file, whereas echo simply prints texts to the terminal or a file.

The syntax of the type command:

type nul > afile.txt
Enter fullscreen mode Exit fullscreen mode

echo can also be used in the same way, but it will actually write the characters nul into the textfile. If you leave it empty then

The arrow character, >, overwrites a file completely. It takes what is left of the arrow and places it into the the referenced file, which would be afile.txt in this case.

But If you wanted to append to the the file, you must use the double arrow: >>. The double arrow takes what is left of the arrow and appends it to the referenced file. If I enter the following sequence of commands in my terminal.

type nul > hello.py

echo Hello world > hello.py
echo "How are you? > hello.py

echo Hello world > hello.py
echo How are you? >> hello.py
Enter fullscreen mode Exit fullscreen mode

Now if we enter type hello.py the contents of our file should be

type hello.txt terminal ouput

8. Title ✍️

When you open a new terminal session the title of that session will be a path of the cmd.exe file

without title terminal default title

Title allow us to change the title of a terminal window. If you are like me and open multiple terminal sessions at different directories, switching back and forth between them can be confusing. I find it helpful to give them distinct names instead of the default directory title.

When I work on web development projects I open 2 terminal sessions, One I use to run a local development server with npm start and the other to create new files and folders and manage them with git.

Portfolio
server

Tricks And Things 🤸‍♂️

Technically some the following still falls under commands but I put it in a separate category because they are not stand alone commands.

I have no idea what the name of this "|" symbol is so I'll call it a pipe. Piping commands is a way to redirect output. You can use this in a lot of ways but I mainly just use it for the 2 commands, more and clip.

9. | more

Once you get familiar with the terminal, you will quickly realise that it is searching through large output is tedious and time consuming. If you want to be thorough, you can pipe the output to the more command. It formats output to the size of the terminal so that you can go through each line of output and press hit to move down one line at time.

tasklist | more
Enter fullscreen mode Exit fullscreen mode

tasklist | more terminal output

Once you have found what you are looking for you can press q so to skip through the entire output.

10. | clip 📋

clip copies output to your clipboard. You should know that Ctrl+C does not allow you to copy text but you must instead right click and select copy. clip is a great way to get around this but because it copies the entire output, you don't have much room to select which part of the output you want to copy. Either way I found it useful

cd | clip 
tasklist | clip 
type hello.txt | clip
Enter fullscreen mode Exit fullscreen mode

There are probably more use cases for clip but I only use it to copy the current directory that I am in so that I don't have to retype the entire path when starting a seperate terminal session.

Bonus: file explorer 📁

This one is for the people that simply can't stand to navigate through the command line🤢, or people that can never seem to remember their folder structure thus rendering the cd command useless. This trick isn't even that fancy but I think a lot of people don't know about it because of how simple it is.

To open the command prompt at a specific directory, open the File Explorer and navigate the directory you wish to start the terminal session in. Once you are in said directory, click on the path, which should be at the top of your screen to the left of the search bar, and simply enter cmd.exe. And voila! Your terminal session will start in said path.

picture of url bar

An alternative would be to right click on a folder and open the command prompt

Select prompt

📝 General advice...

If you want to learn a command then practice using it. Once you find it useful it will become more difficult to forget. Also, some commands are harder to grasp than others, for example, taskkill took me a while to understand but after doing it a couple of times it has become nothing more than a reflex.

There are lot of interesting commands that I left out simply because I don't know them well enough or I didn't find them useful at this particular time in my programming journey. If you love the terminal and learned a command recently, share it in the comments below.

Other commands that I find useful but simply don't use often enough to include in this blog are

  • color
  • set
  • setx
  • xcopy
  • at

I think these commands are worth knowing either that or I simply haven't got around to learning them yet.

The command line can be a dangerous place. All it takes is del ** and you can say goodbye to the contents of your current directory. But if you know what you're doing it can increase your productivity a lot. There are so many more commands I want to learn I just thought I'd share some them for those who are as enthusiastic as I am 😉.

Closing

To be honest, my computer is really slow. I mean really slow. I mean so slow that if it were to get any slower it would start going backwards. The command line, however, is light on memory. So as much as many of these commands can be done with the click of a mouse, I save a lot time with these commands.

I know this post was unusually long, I thought I could break it up to a series in order to get more views but I don't think it really matters. I just need this to be out there. I have been quite sluggish of late and I need a win. I consider posting an achievement. And if you got this far I consider that an even bigger achievement. If you find my content useful or at the very least amusing🙃 then I thank you. I hope all is well with you, Stay safe and Happy Coding!💻

Top comments (0)