DEV Community

Cover image for Introduction To The Command-Line Interface
Altaf Shaikh
Altaf Shaikh

Posted on

Introduction To The Command-Line Interface

In this blog, we will learn about the command-line interface in Windows, Linux, and Mac-OS and we will learn some basic commands and how to use them.

What is the command line?

  • The command line, also called the Windows command line, command screen, or text interface is a user interface that is navigated by typing commands at prompts, instead of using the mouse.
  • In the 1960s, using only computer terminals, this was the only way to interact with computers.
  • In the 1970s and 1980s, command line input was commonly used by Unix systems and PC systems like MS-DOS and Apple DOS.
  • Today, with graphical user interfaces (GUI), most users never use command-line interfaces (CLI).
  • However, CLI is still used by software developers and system administrators to configure computers, install software, and access features that are not available in the graphical interface.

Open the command-line interface. To start some experiments we need to open our command-line interface first.

Opening: Windows

Depending on your version of Windows and your keyboard, one of the following should open a command window (you may have to experiment a bit, but you don't have to try all of these suggestions):

  1. Go to the Start menu or screen, and enter "Command Prompt" in the search field.
  2. Go to Start menu → Windows System → Command Prompt.
  3. Go to Start menu → All Programs → Accessories → Command Prompt.
  4. Go to the Start screen, hover your mouse in the lower-left corner of the screen, and click the down arrow that appears (on a touch screen, instead flick up from the bottom of the screen). The Apps page should open. Click on Command Prompt in the Windows System section.
  5. Hold the special Windows key on your keyboard and press the "X" key. Choose "Command Prompt" from the pop-up menu.
  6. Hold the Windows key and press the "R" key to get a "Run" window. Type "cmd" in the box, and click the OK key.

Opening: OS X

Go to Applications → Utilities → Terminal.

Opening: Linux

It's probably under Applications → Accessories → Terminal, or Applications → System → Terminal, but that may depend on your system. If it's not there, you can try to Google it. :)

Prompt

You now should see a white or black window that is waiting for your commands.

Prompt: OS X and Linux

If you're on Mac or Linux, you probably see a $ , like this:

$
Enter fullscreen mode Exit fullscreen mode

Prompt: Windows

On Windows, you probably see a > , like this:

>
Enter fullscreen mode Exit fullscreen mode

Your first command (YAY!)

Let's start by typing this command:

Your first command: OS X and Linux

$ whoami
Enter fullscreen mode Exit fullscreen mode

Your first command: Windows

> whoami
Enter fullscreen mode Exit fullscreen mode

And then hit enter . This is our result:

$ whoami

altaf
Enter fullscreen mode Exit fullscreen mode

As you can see, the computer has just printed your username. Neat, huh? :)

Use CLI commands with great attention!!! Wrong use can easily delete files or destroy your computer system completely.

Basics

Each operating system has a slightly different set of commands for the command line, so make sure to follow instructions for your operating system. 

Current directory

It'd be nice to know where are we now, right? Let's see. Type this command and hit enter :

Current directory: OS X and Linux

$ pwd

/home/altaf
Enter fullscreen mode Exit fullscreen mode

Note: 'pwd' stands for 'print working directory'.

Current directory: Windows

> cd

C:\Users\altaf
Enter fullscreen mode Exit fullscreen mode

Note: 'cd' stands for 'change directory'. With powershell you can use pwd just like on Linux or Mac OS X.

Learn more about a command

Many commands you can type at the command prompt have built-in help that you can display and read! For example, to learn more about the current directory command:

Command help: OS X and Linux

OS X and Linux have a man command, which gives you help on commands. Try man pwd and see what it says, or put man before other commands to see their help. The output of man is normally paged. Use the space bar to move to the next page, and q to quit looking at the help.

Command Help: Windows

Adding a /? suffix to most commands will print the help page. You may need to scroll your command window up to see it all. Try cd /? .

List files and directories

So what's in it? It'd be cool to find out. Let's see:

List files and directories: OS X and Linux

$ ls

Documents                Pictures            Downloads        Public 

Music                         Videos              Desktop    
Enter fullscreen mode Exit fullscreen mode

List files and directories: Windows command-line

    

> dir

Directory of C:\Users\altaf

05/08/2014 07:28 PM <DIR> Applications

05/08/2014 07:28 PM <DIR> Desktop

05/08/2014 07:28 PM <DIR> Downloads

05/08/2014 07:28 PM <DIR> Music

...
Enter fullscreen mode Exit fullscreen mode

Note: In powershell you can also use 'ls' like on Linux and Mac OS X.

Change current directory

Now, let's go to our Desktop directory:

Change current directory: OS X and Linux

$ cd Desktop
Enter fullscreen mode Exit fullscreen mode

Change current directory: Windows

> cd Desktop
Enter fullscreen mode Exit fullscreen mode

Check if it's really changed:

Check if changed: OS X and Linux

$ pwd

/Users/altaf/Desktop
Enter fullscreen mode Exit fullscreen mode

Check if changed: Windows

> cd

C:\Users\altaf\Desktop
Enter fullscreen mode Exit fullscreen mode

Here it is!

PRO tip: if you type cd D and then hit tab on your keyboard, the command line will automatically fill in the rest of the name so you can navigate faster. If there is more than one folder starting with "D", hit the tab key twice to get a list of options.

Create directory

How about creating a practice directory on your desktop? You can do it this way:

Create directory: OS X and Linux

$ mkdir practice
Enter fullscreen mode Exit fullscreen mode

Create directory: Windows

> mkdir practice
Enter fullscreen mode Exit fullscreen mode

This little command will create a folder with the name practice on your desktop. You can check if it's there by looking on your Desktop or by running a ls or dir command! Try it. :)

PRO tip: If you don't want to type the same commands over and over, try pressing the up arrow and down arrow on your keyboard to cycle through recently used commands.

Clean up

We don't want to leave a mess, so let's remove everything we did until that point. First, we need to get back to Desktop:

Clean up: OS X and Linux

$ cd ..
Enter fullscreen mode Exit fullscreen mode

Clean up: Windows

> cd ..
Enter fullscreen mode Exit fullscreen mode

Using .. with the cd command will change your current directory to the parent directory (that is, the directory that contains your current directory).

Check where you are:

Check location: OS X and Linux

$ pwd

/Users/altaf/Desktop
Enter fullscreen mode Exit fullscreen mode

Check location: Windows

> cd

C:\Users\altaf\Desktop
Enter fullscreen mode Exit fullscreen mode

Now time to delete the practice directory:

**Attention:** Deleting files using **del**, **rmdir** or **rm** is irrecoverable, meaning the deleted files will be gone forever!

So be very careful with this command.

Delete directory: Windows Powershell, OS X and Linux

$ rm -r practice
Enter fullscreen mode Exit fullscreen mode

Delete directory: Windows Command Prompt

> rmdir /S practice

practice, Are you sure <Y/N>? Y
Enter fullscreen mode Exit fullscreen mode

Done! To be sure it's actually deleted, let's check it:

Check deletion: OS X and Linux

$ ls
Enter fullscreen mode Exit fullscreen mode

Check deletion: Windows

> dir
Enter fullscreen mode Exit fullscreen mode

Exit

That's it for now! You can safely close the command line now. Let's do it the hacker way, alright? :)

Exit: OS X and Linux

$ exit
Enter fullscreen mode Exit fullscreen mode

Exit: Windows

> exit
Enter fullscreen mode Exit fullscreen mode

Cool, huh? :)

Pro Tip :

When you want to copy from the terminal or paste into the terminal instead of using the right-click option you can use a keyboard shortcut for copy and paste. To copy from terminal use ctrl + shift +c and to paste into the terminal use ctrl + shift + v. This will save your time. Cool!, huh? :)

Summary 

Here is a summary of some useful commands:

Scenario(when u want to) Windows Command Command (Mac OS / Linux) Description Example
See which files are present in a directory dir ls list directories/files dir
Create a directory mkdir mkdir create a new directory mkdir testdirectory
Move a file or dir into other location move mv move file move c:\test\test.txt c:\windows\test.txt
Exit from the terminal exit exit close the window exit
Copy a file or dir into other location copy cp copy file copy c:\test\test.txt c:\windows\test.txt
Go inside a directory cd cd change directory cd test
See the full path of the current directory cd cd show the current directory cd (Windows) or pwd (Mac OS / Linux)
Delete a file from the system rmdir (or del) rm delete a file del c:\test\test.txt
Delete directory from the system rmdir /S rm -r delete a directory rm -r testdirectory
Check the usage of a command [CMD] /? man [CMD] get help for a command cd /? (Windows) or man cd (Mac OS / Linux)

I hope the article was helpful to you! 

Oldest comments (3)

Collapse
 
moopet profile image
Ben Sinclair

Your "tidy up" section implies you're missing a step. You create a directory, then move up a directory, then try to delete the directory you created. You never changed into it, so you're now in the wrong place.

Collapse
 
ialtafshaikh profile image
Altaf Shaikh

Hello Ben, I am assuming that user is in the practice dir and then I am going one level up and using cd command (without path) to check the present working dir and not for changing the dir. so that we can delete it from the parent dir.

Hope you got clarity on this : D

Collapse
 
aoayoola profile image
Abayomi Ayoola

Powershell is more common with WindowsOS nowadays as I believe they intend to retire the Command Prompt