Commands line up
The command line, the command prompt, the terminal, the CLI a.k.a. Command Line Interface a.k.a. the Command Line Interpreter. Many names for this useful tool. It is very powerful and grants you access to all the files on your computer but does look scary if you don’t know where to start. No hints are given and you have to do everything yourself. This post will get you started.
- Why use the command line?
- Useful to know before you start
- How to open the command line
- Where you are and how to move to another location
- Two tricks to make your life easier
- Create and delete folders
- Special roles: sudo & administrator
- Interrupting a command
1. Why use the command line?
Because it is again increasing its popularity among the new technologies. There are some tools that are the most efficient when used on the command line like npm which we’ll use a lot with React Native or Git for version control.
The upside to using the command line is that the commands can be the same for different operating systems. So all the instructions and support can be given as commands. Instead of having to tell you where to click what and in the worst case three different instructions for Windows, Macs and Linux. This makes the process quicker and simpler.
And as a beginner programmer I don’t know anything that would give you the feeling of “Oh boy, here I am. Just coding away!” as quickly as running commands with the command line.
Besides you might soon find yourself in the situation where you need to install a virtual machine on your computer. Then you can test a program or develop one in a different operating system and tools. Then very often this virtual machine appears as a command line window to you.
2. Useful to know before you start
It is possible to harm your system with commands from the command line. But those are very specific commands you will not accidentally write when trying out these commands listed here. The worst thing you can do with these is to delete a file you didn’t mean to delete. But that is always a possibility, even when using the plain old graphic interface and icons.
No news is good news. If the command doesn’t throw an error it did what it was supposed to. Even though on the screen it seems like nothing happened. If you made a typo, it will not be recognised and you will be told so. If you are missing something crucial you will be told so.
Directory means exactly the same as folder. Originally it was thought that data was stored in directories but once graphic interfaces came along with icons the idea of a folder became stronger. A folder can contain other folders and files.
3. How to open the command line?
On Windows the program is called Command Prompt. Find it by opening the Windows menu and typing “cmd” in the search.
On Macs the program is called Terminal. You can find it from the Utilities-folder. Easy access to Utilities by pressing cmd+U in Finder.
Linux also calls it Terminal. To find it click on the Activities menu and search for “terminal”, “shell”, “prompt” or “command”.
These are the default command line tools for the operating systems. You can also install another command line tool if you want. One option is Gitbash which allows you to run Unix commands on Windows.
4. Where you are and how to move to another location
The following commands are different depending on if you are running a Windows machine or a Mac / Linux which are both based on Unix.
So if you don’t know where you are you can write either cd or pwd. I have modified my command line prompt to always show my file path. Windows uses backslashes \ in its paths, Macs and Linux use slashes / in theirs.
If you want to see what the folder contains use command dir or ls .
When you want to move into one of the folders in this location write cd [folder name].
If you want to go back to the previous folder type cd .. -Don’t forget the space before the two dots!
If you need to change the drive on Windows for example from D drive to C drive just type C:
If you want to go back to the home directory on a Unix machine just type cd
And now you know the most important commands! Some of them might be familiar from your childhood and the very first home computers.
5. Two tricks to make your life easier
Making typos is annoying. Here are two tricks that will have you typing less and hence making less typos. Thank you lazy programmers you made my life so much easier!
If you want to change into a folder you can start typing the first letter of that folder and press on the Tab key (short for tabulator) which will complete the name for you. So handy! And if you remember the path to a specific folder or file you can continue writing it after the forward slash (*NIX) or backward slash (DOS) and use the Tabs to complete the other names on it as well.
There are two scenarios when the Tab complete doesn’t work. 1) There isn’t a file or folder inside the folder that begins with the letters you typed. 2) There is more than file or folder that starts with what you typed and the command line doesn’t know which one to choose. In either case you can press the Tab key twice and it will list out the options for you if they exist. If there are multiple options you only need to continue typing until only one option remains.
Using Tab completion is very useful in many situations, one of them being that there is a space in the folder name. Using the cd-command with a plain space won’t do. There is a work around you can learn to type but the tab completion does it for you.
The other very nice trick is using the up and down arrow keys to go back and forth the commands you have used. I often find myself repeating the same two commands over and over. With the arrow key up I get to them quickly. Notice that if you used the “cd ..” five times in a row, you also have to press the arrow key five times to get to the next command. But it is there. Again less typing and less chances to make mistakes.
And now you’re already approaching some pro-level command line habits!
6. Create and delete folders
Often we use the command line to install software and to initialise projects. Here is how you can create a folder for a project.
Create a new folder with md or mkdir [folder name]
I you want to delete a folder type rd or rmdir [folder name] But this only works on an empty folder!
The remove directory command doesn’t know what to do with the things inside the folder and so it can’t delete the folder either. For this we have a recursive command which goes inside the folder, deletes what it contains, comes back outside the folder and deletes it as well. For that type deltree or rm -R [folder name]
Be careful when deleting folders so you don’t accidentally delete something important.
7. Special roles: Sudo and Administrator
Sometimes you need more permissions to run a specific command. Luckily there are special roles for that
For Windows it is called the administrator.
Open the Windows menu and search for the Command prompt. Click on the second mouse button and choose “Run as administrator” like in the image. Or click on the option on the list that shows on the right side of Best match panel.
This opens a new Command prompt window and as long as you are using this window, you are the administrator and have additional permissions.
Macs and Linux machines use the role of sudo, short for “super user do”. If your command fails do to insufficient permissions use the same command with “sudo” added in front of it or repeat the previous command as sudo by using the command “sudo !!”
You are prompted for your password. Sudo has a timer while it remembers your password. So new sudo commands you run right after the first one do not ask for your password. But after a while it is prompted again, to make sure it is still you running the commands.
8. Interrupting a command
When you install software to your computer with the command line some of the commands take a long time to process with all the downloads and updates. So what if you hit the same command again? You might want to interrupt the process by pressing control and c.
This does not cancel a command that has already ran through. It only interrupts an ongoing process.