Introduction to Command Line Interface

Introduction to Command Line Interface

A very simple guide for beginners to understand the command line interface (CLI).

An Interface is a medium by which you “inter”-act with a system or device. It helps you communicate your intentions to the internals of a system or device.

For instance, to play a sound on a piano, you press a key. The key is not what produces the sound. The component which produces the sound is an internal component and because of obvious reasons, it is often important to protect the internal implementation of any system while providing other ways for its users to interact with it. In this case, the keyboard is a part of the interface that helps you interact with the piano.

To interact with our computer, we do not directly interact with internal hardware or software (you can't touch ones and zeros and you do not press a microchip, RAM, processor or motherboard to convey your intent). To type letters we simply have a keyboard which is an interface between you and the internal functionality required to convey the value of the key pressed to your screen.

Interfaces provide simpler ways to talk to a system.

The software in our machines needs human input/interaction (instructions) to be able to carry out actions.

For example, on your laptop, if you wish to open a folder, the human interaction needed would be to double-click on that folder's icon or simply right-click on the folder icon and select the "open" option.

Note that the folder is represented graphically i.e., it is an image that denotes a “folder”.

Folders

On a website, if you wish to change screens or perform an action, you have links or buttons for that while images are used to tell a story.

Google Website User Interface

All these are graphical representations of the system and its possible functionalities. Each of these components is generally associated with a certain behaviour so that you know what to expect when you interact with them. This kind of interface is known as a Graphical User Interface.

There is another way to interact with devices which is called the Command Line Interface and today, we'll be learning how to make use of it.

Terminal (CLI for MacOS)

As the name suggests, a command line interface provides a window that allows you to enter commands in-line.

These commands can be used to perform the same task that would have been done by using the graphical user interface i.e., clicking on graphical components on the screen, viewing a list of files etc.).

It is important to note that not all software or platform provides this option for interaction but I believe all major operating system does support the CLI.

In this article, I am going to introduce you to working with the CLI using the most regular commands on Windows and MAC operating systems.

LAUNCHING THE CLI

To get started, follow the steps below to launch your built-in CLI interface.

MAC Users

  1. Click the Launchpad icon in the Dock, and type Terminal in the search field.

  2. Click Terminal.

You can also launch the terminal by using Finder. In the finder window, click on Applications >> Utilities and in the utilities folder, locate the terminal icon and double-click on it.

Windows Users

  1. Click on the "Start" menu on your taskbar and type "cmd".

  2. Click "Command Prompt".

HOW TO CARRY OUT REGULAR TASKS USING THE CLI

When interacting with your computer using a CLI, instead of clicks, we issue commands. These commands are typed in the command line interface and then the enter key is used to issue the command to the computer's operating system.

For instance, to create a folder the usual way (via User Interface), you would do something like this;

  1. Navigate to the location where you wish to create a folder.

  2. Right-click on an empty space in that location (avoid right-clicking any other item on that location).

  3. Click on the folder option.

  4. Name the folder.

  5. Click away from the just created folder or press enter.

But if we wish to create the folder using a CLI, you would do the following.

  1. Launch your CLI app (command line or terminal)

  2. Navigate to the location where you wish to create the new folder e.g cd ~/documents

  3. Type the command mkdir books_test_ii and press enter.

Just like this, you will have a folder called books_test_ii created in your documents directory. You'd agree with me that using the CLI can be easier with regards to performing some tasks especially if you are a developer - in which case you'd most likely always have your terminal open (which eliminates step 1).

BASIC CLI COMMANDS

The following are some basic CLI commands to perform everyday tasks based on the operating system you are on.

NB: In the following section, the text between the square brackets [ ] are just placeholders for expressing what the actual text that should replace it represents (you may wish to read it again).

This icon ↩ is used to depict the pressing of the "Enter" key on your keyboard.

Lastly, where there are two different commands for window and MacOS, the command for Windows will be listed first, while that of MacOS will follow in this format: window | macOS

1. Change Directory: cd

e.g., cd ./documents
This will change the working directory to "documents" - assuming that "documents" is a folder residing in the current directory. If we want to go a step out of our current folder the command would start with ../ and ../../ for two steps out.

2. Print Working Directory: dir | pwd

e.g., dir or pwd
This just prints the address of your current working directory.

3. List directory content: dir | ls

e.g., ls
This command lists all the files and folders in the current location.

4. Create file: echo [text] > [filename(.extension)] | cat [text] > [filename(.extension)]

e.g., cat "Hi" > note.txt
Creates a new file in the current working directory.

5. Remove directory: rmdir

e.g., rmdir ./documentx
Remove the "documentx" folder if it exists in the current working directory.

rmdir ../../documentx
Move two steps out of the current folder and delete the "documentx" folder if it exists in the destination.

6. Rename a folder: ren [oldName] [newName] | mv [oldName] [newName] |

e.g mv ./old-folder ./new-folder
Moves old-folder content to new-folder (technically renaming).

7. Copy folder: robocopy [folderName] [path to destination directory] | cp -r [folderName] [path to destination directory]

e.g., cp -r documents ./lectures/documents
Copies the folder document (which is assumed to be in the current working directory) to the lectures folder.

8. Move folder: move [folderName] [path to destination directory] | mv [folderName] [path to destination directory]

e.g., mv ./old-folder ./new-folder
Moves old-folder content to new-folder.

9. Make Directory (create a folder): mkdir

e.g., mkdir newFolder
Creates a new folder named "newFolder" in the current working directory.

10. Remove File: del [filename] | rm [filename]

e.g., rm books.txt
Remove books.txt from the current working directory.

11. Copy File: copy [filename] [path to destination] | cp [filename] [path to destination]

e.g., cp books.txt ./documents
Copy books.txt to ./documents

12. Rename File: ren [oldName] [newName] | mv [oldName] [newName]

e.g., mv books.txt text-books.txt
Move the contents of books.txt to text-books.txt and delete the original (technically renaming).

13. Move File: move [filename] [path to destination] | mv [filename] [path to destination]

e.g., mv books.txt ./documents
Move the books.txt file to the ./documents folder.

14. Clear screen: cls | clear

e.g., clear
Clears all text from the command line to free up visual space.

Summary

Although it feels good to write commands in the CLI for your tasks, I believe both the UI and CLI serve different purposes.

As a programmer, the CLI is a very handy tool to use because you just keep typing to do everything instead of having to leave the keyboard to use a mouse or trackpad. It just makes you more proficient.

But for everyday/regular use of the computer, you may find using the User interface more natural.

Do not be intimidated by the commands, they become part of you as you repeatedly use them.

If this article was helpful, kindly subscribe to my newsletter to receive updates when I make new posts.