Saturday 29 October 2016

Introduction To Command Line For The Complet Beginner

The Command Line
The Command Line


The Command Line

 Kids today! They waste a huge amount of time looking around for icons. Silly mouse pushers. The main tool of a power computer user is the command line. Instead of giving you graphics, the command line offers text input and output. The advantage being that it is very fast and efficient. Instead of looking around for icons and buttons, all you have to do is type a simple command. In this post, I will tell you how to use the command line and get work done fast.

The command line is an essential tool for a system administrator, programmer and a power user. The GUI makes things easy while the command line makes difficult things possible. Sadly, many don't know even what the command line is or have some misconception about it.

The early computers couldn't display graphics and worked only with texts. Later on GUI came and command line was buried inside it. That means, the command line is still there as a separate application.

On *NIX systems it is known by various names and I assume you know how to start it. For Mac and Windows users, here are the instructions.

Windows

On Windows the command line is the PowerShell. There is also the old command prompt but it is obsolete. If you don't have PowerShell, you can get it from the Microsoft website. Relax it is free. Start it using the usual process.

Mac OS X

On OS X it is called Terminal. Start it like any other applications ;-).


Once you start the command line, you will see a prompt like the following:

PowerShell on Windows
 
Terminal on Mac OS X

The Screenshot Explained

Here is the general structure of the prompt shown in the screenshot:
  1. The folder you are in. C:\ in PowerShell and note the ~ in Terminal which means the users home directory.
  2. A cursor.
  3. Terminal has some extra information: localhost is the name of the computer while midnite is the user's name.

Navigating Around the Computer

You interact with the command line by typing commands. Right now type the following and notice the output:

     $ ls

Type only ls and not the $ sign. The $ sign is often used to indicate a general prompt, so get used to it and don't type it.

The ls command lists the contents of the folder/directory you are currently in. That is, it will tell you which folders and files are located inside your current folder/directory.

Now let's try some other commands.

To move to a different folder use the cd command. But you need to understand a few things before that.

Suppose there is a folder called playground which is your current folder. The playground folder contains another folder called swing and the playground folder is located inside C:/ (Windows) or ~ (Mac). You can locate the swing folder in two ways: Using absolute path reference or relative path reference. Path refers to the location of the file or folder represented as text. The path to the swing folder would be:

C:/playground/swing (Windows)
~/playground/swing (OS X)

These examples show the absolute path reference to swing; no matter where you are you can reach swing by following this path. Note that / is used to separate folders in path.

Relative path reference works like this: instead of starting from the root (C:/ or ~), you start with the current folder (playground in this case). So the relative path reference to swing. when you are in the playground folder would be simply swing.

You can now change to a directory by providing cd with the path to that directory, like this:

    PS C:/> cd C:/Windows
    % cd ~/Documents


Intermission

Anything is learned by practicing. Here is an exercise to burn what your learned just now into your brain. Open a GUI file explorer (like Windows Explorer) and go to a folder. Now try to go to the same folder using the command line and list the content of the folder. Repeat till you get bored.

File Operations

You can do (almost) everything using the command line that you can do using the GUI. Some of the common operations are moving, copying or deleting files/folders. But before doing that, let's create a practice folder in which we can experiment.

To create a folder use the mkdir command like this:

    $ mkdir name_of_the_folder

Replace name_of_the_folder with the desired folder name. This command will create a folder inside the current folder. Try creating a folder called practice and cd into that directory to make it your current working directory/folder.

Now open a text editor (like Notepad or TextMate) and create a file called cool.txt in the practice directory. We will perform various file operations on this file using the command line.

Moving/Renaming Files

In the command line moving and renaming are the same things. The following example will explain it better. To move/rename a file we use the mv command. You must provide it with two pieces of data, the target file and where to move it. If the move path is the same as the path of the target file but the name is different, the target will be renamed. Here let me show you:

    $ cd practice
    $ ls
     cool.txt
    $ mv cool.txt uncool.txt
    $ ls
    uncool.txt
    $ mv uncool.txt C:/

The first mv renames cool.txt to uncool.txt and the second mv command moves uncool.txt file to C:/ where C:/ can be any valid path. Easy, isn't it?

Copying Files

Copying works similarly. To copy a file use the cp command. You can copy and rename at the same time:

    $ ls
    uncool.txt
    $ cp uncool.txt cool.txt
    $ ls
    cool.txt uncool.txt

Cool, right?

Deleting Files

Deleting a file is rather simple. Use the rm command (while stands for remove):

    $ ls
    cool.txt uncool.txt
    $ rm uncool.txt
    $ ls
    cool.txt

Directory Operations

There's nothing special for directory operations. The same mv, cp and rm commands also work with directories.

Note: On Mac OS X, you must use rm -r to delete a directory.

Further Study

We have only scratched the surface of the metal. Command line is much more powerful; so powerful that I doubt if what we covered here was even 1 % of the total command line tricks available. Fortunately, you don't have to learn 100 %, just learn enough to accomplish your daily tasks. Just keep in mind that the command line is not hard, it's just a bit vast.

Here are some of the links to help you learn more:

Windows PowerShell Tutorial for Beginners with examples
Command Line Cheat Sheet Download


Please let us know if you liked the post in the comments. If you have any kind of questions, suggestions, etc. please do tell us through the comments. You can post anonymous comments as well.

No comments:

Post a Comment