Taming the command
When it comes to everyday computing, one of the most useful and powerful tools at your disposal is the command line. For the uninitiated, however, trying to find your way on the command line can also be very daunting. And no wonder, with so many commands, so many switches, options, parameters, flags, etc. And what if you type the wrong command and cause problems? It can be easy to get lost and create a mess, right? Well, there are a few very simple things you can do to tame the command line and we’ll look at a few of them here. Perhaps one of the most daunting computing environments for many people is Linux, so this is where we’ll start.
In Linux, all commands are executed by what’s called the ‘shell’. Think of the shell as the session between the user and the operating system. The shell prompts the user for input, processes that input and, where appropriate, provides output (or does something else). When you open a shell, you are presented with a prompt. This is nothing more than a piece of text displayed to tell the user that the shell is ready to accept commands. Depending on how the system has been configured, the prompt can be very simple like this:
Here, the prompt is a simple dollar sign. A slightly more complicated prompt could look like this:
In this example, the name of the computer (myhostname) is included in the prompt and is followed by a greater than sign. A prompt can be configured to include lots of difference pieces of information, like the following:
[user@myhostname 12:34] $
Here, our prompt contains the name of the user you are logged in as (user), followed by the @ sign, followed by the computer name (myhostname) and finally the current time. It doesn’t really matter what the prompt looks like, as it’s nothing more than an indication the shell is ready to accept commands. When you type a command, you instruct the shell to execute that command by hitting the enter or return key. The shell will then perform whatever command you’ve asked it to. It may or may not generate any output but when it’s finished executing the command, it will display a new prompt to let you know that it’s again ready to accept new commands:
$ date <enter>
Tue Dec 8 16:21:04 EST 2015
Here, I’ve issued the date command, followed by the enter key, to find out the current time and date. The shell has executed the date command, which has generated a line of output to the screen – i.e., the actual date and time information. As you can see, a new prompt has also been printed to indicate that the shell is ready to accept more commands again.
Ok, so far, so good. Now let’s see what happens if we do something wrong. Let’s say I hit the wrong key and instead of running the date command, I typed in sate (I hit the s key instead of the d key):
$ sate <enter>
-bash: sate: command not found
Ok, so the shell attempted to execute my sate command but instead of printing the date, it printed an error. In this instance, the error tells me that the shell doesn’t know what the sate command is – it couldn’t find it. Not to worry, the shell just issued me with a new prompt and we can simply try again. See? Nothing bad happened.
Let’s try another command, such as the pwd command (which stands for Present Working Directory):
$ pwd <enter>
The shell has executed the pwd command, which has generated some output – i.e, the name of the present working directory, and we have a new prompt to indicate that the shell is again ready to accept more commands.
Many Linux commands accept switches. These are additional pieces of information you provide to the command to modify its behavior. A command switch in Linux is typically one or more letters preceded with a minus sign. Let’s take a look at the ps command, which is used to list any processes that are currently running:
$ ps <enter>
PID TTY TIME CMD
7341 pts/0 00:00:00 bash
7462 pts/0 00:00:00 ps
The ps command has produced 3 lines of output and the shell has given us another new prompt again. We can see from this output there are 2 processes running. The first is a process called bash, which is the shell that’s currently providing us with the prompt. The second is the command ps that was actually running to generate the output. The default behavior of the ps command is to list some brief information on all the processes that are running, but we can modify this by instructing it to provide use with more detailed information. Using the -f switch, we can instruct ps to provide us with a full listing (the choice of letter for a given switch will often relate to what it’s doing – i.e., f stands for full):
$ ps -f <enter>
UID PID PPID C STIME TTY TIME CMD
chrism 7341 7340 0 16:20 pts/0 00:00:00 -bash
chrism 7494 7341 0 16:44 pts/0 00:00:00 ps -f
Now, instead of providing us with just the default 4 columns of information, we can now see 8 columns.
Here are some simple commands you can try to run in a shell window (all of these can be safely executed):
$ ps –e
$ ps -f
$ ps -ef
$ df -h
$ ls –a
$ ls –l
$ ls –al
$ uname -a
As you can see, it doesn’t take much to tame the command line and an entirely new world has just opened up for us. The commands featured here are just the tip of the ice berg. We’ll explore some more in the next edition of this blog.