Introduction To The Command-Line Interface

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:

$

Prompt: Windows

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

>

Your first command (YAY!)

Let's start by typing this command:

Your first command: OS X and Linux

$ whoami

Your first command: Windows

> whoami

And then hit enter . This is our result:

$ whoami

altaf

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

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

Current directory: Windows

> cd

C:\Users\altaf

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

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

...

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

Change current directory: Windows

> cd Desktop

Check if it's really changed:

Check if changed: OS X and Linux

$ pwd

/Users/altaf/Desktop

Check if changed: Windows

> cd

C:\Users\altaf\Desktop

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

Create directory: Windows

> mkdir practice

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 ..

Clean up: Windows

> cd ..

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

Check location: Windows

> cd

C:\Users\altaf\Desktop

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

Delete directory: Windows Command Prompt

> rmdir /S practice

practice, Are you sure <Y/N>? Y

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

Check deletion: OS X and Linux

$ ls

Check deletion: Windows

> dir

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

Exit: Windows

> exit

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 CommandCommand (Mac OS / Linux)DescriptionExample
See which files are present in a directorydirlslist directories/filesdir
Create a directorymkdirmkdircreate a new directorymkdir testdirectory
Move a file or dir into other locationmovemvmove filemove c:\test\test.txt c:\windows\test.txt
Exit from the terminalexitexitclose the windowexit
Copy a file or dir into other locationcopycpcopy filecopy c:\test\test.txt c:\windows\test.txt
Go inside a directorycdcdchange directorycd test
See the full path of the current directorycdcdshow the current directorycd (Windows) or pwd (Mac OS / Linux)
Delete a file from the systemrmdir (or del)rmdelete a filedel c:\test\test.txt
Delete directory from the systemrmdir /Srm -rdelete a directoryrm -r testdirectory
Check the usage of a command[CMD] /?man [CMD]get help for a commandcd /? (Windows) or man cd (Mac OS / Linux)

I hope the article was helpful to you!