Using a command line – cd command

CD is one of the top notch command like ls and dir that you will use a lot! One of the best thing is, you do not need to worry about operating system with this command. You can get this cd command to work just fine on any operating system. This article will show you how to make use of it.

Why do you need this command?

So what is it used for? If dir and ls are your command line explorer, then this command is your mouse or navigator. You use this command to change directory. You just simply start with this command and type in the directory name you want to get into. For instance, you have a folder in the root directory, assuming you are in the root directory, called ‘myFolder’.You simply type in cd/myFolder and the focus of the command line will changed to such my folder. Make sure to always hit the enter button after each command to execute it otherwise nothing will happen.

In case, any of the type directory does not exist, you will get an error message like “The system cannot find the path specified”. Sometimes, you may see this error even though you know there is a directory, check your spelling, the typos are the culprit for such message. Go ahead and open your command prompt and type in these command and play around.

When you are done exploring and if you have to return to the root of the directory simply type in cd\ and hit enter. If you want to navigate into the deeply nested sub-folder, you can do so with this cd as well.

Errors while using a command line

One of the things you want to get used to is dealing with frustration and disappointment with errors. There will be times when you will see error messages so often, that is good. Yes. Remember that, errors are good. So good. They are there to tell you exactly what is going on. Could be your command gone wrong and typed, this will help you to redirect your attention to the right path. In fact, when you are learning, purposefully make errors and see what causes those errors and make yourself familiarise with such errors.

Working with directories on different systems

Changing these directories work the same way on macOS and Linux, but remember one of the major thing here is that, in windows you use backward slash, cd|someFolder but on UNIX you use forward slash like C:/cd /myFolder and so on. Remember that on Linux environment you are not supposed to create files, folders in the root directory. As a matter of fact, you will need root permission to do so. This is due to the way Linux operates as multi-user system. This was implemented to prevent user from accidentally or intentionally break the underlying structure of the operating system.

Relative path

Notice that how tedious it would get to type in backward or forward slash like that to navigate your way to the layers of the folders? This was true and mandatory in the past but not now. You can simply navigate from the root directory like -C:\> cd myFolder. Just a cd [space] [folderName]. This will tremendously help you to type in unnecessary backward or forward slash for every path. This is called relative path.

One other trick you should memorise is the use of double dot ( .. ) after cd. This helps you to get up a level of the directory you are in. For example, if you are in the C:>\Users\myname\myFolder directory, typing, cd.. will take you to the C:>\Users\myname directory. This incredibly saves you from typing in all the way to this directory when you just had to get up a level from the directory.

Go ahead and use all of these commands that you have just learned. Simply type in cmd in the search bar and open the command prompt or use shortcut like ‘windows + R’ and type the cmd then hit enter. In case you want to use the administrator privilege, you need to right click the command prompt and select Run as administrator. Have fun exploring command line.