1K Linux Commands

From wiki.ucalgary.ca
Revision as of 05:37, 2 March 2011 by Locasto (talk | contribs) (Getting Oriented)
Jump to: navigation, search

1024 Commands: An Introduction to the Linux Command Line

A shell is just a program stored on a computer. When executed, the shell provides an interactive process for issuing commands. For users accustomed to interacting with a computer through a GUI, mouse, or touchscreen, a command line can seem daunting, particularly since the "prompt" (a terse snippit of characters indicating the input location) does very little prompting and often spits back obtuse error messages.

This brief tutorial demonstrates some of the basics of the Linux command line environment using the Bash shell. A reader should have no trouble using another shell as this tutorial largely avoids shell-specific features. Instead, it focuses on a general set of Linux commands.

The Basics

A shell is a program that helps a user interact with a computing environment. The core functionality of most shells, particularly in a Unix environment, is to locate and execute programs on behalf of the user. The user types a command; commands typically include the name of a program and some arguments or data for that program to operate on. The shell locates the named program, supplies the arguments to the program, and waits for the program to complete.

It is important to realize that the shell typically acts like a dispatcher -- except in the cases where it uses its own built-in functionality (or in cases where the user invokes shell-specific scripting commands), the shell merely looks up the location of the named program and asks the operating system to execute that program. Commands (most of which are separate programs) are typically stored in a few well-known locations in the file system, such as /bin, /usr/bin, /sbin, /usr/sbin and the user's own bin/ directory in their home directory. For example, the /bin directory of a recent distribution of Linux includes more than 100 commands, including ones like `arch', `cat', `cp', and `date'. It has over 1800 commands in the /usr/bin directory, 280 in /sbin, and 375 in /usr/sbin. This amounts to more than 2500 possible programs to run.

Unix and Linux environments come with a large variety of commands. Commands are typically external programs, although some functionality is built into the shell because it is simple.

Takeaway Message: the shell is a program that waits for your input. You provide input by typing commands. Commands start with the name of some program and have optional arguments or parameters.

Getting Oriented

So your prompt is waiting for you. Patiently. Before we tell it to do something, let's give you a fallback so you know where to get help --- right in your shell (without resorting to digging through Google results or bugging your friends or system administrator).

The first place to start is knowing how to access documentation. Most Linux shell environments come equipped with a command to do this, the `man' (short for "manual" command). You can supply the `man' command with a number of arguments to control its behavior in various ways, but the simplest form is by giving it just the name of a program whose functionality you'd like to determine. For example, typing the following command and hitting <enter>:

[michael@host 1k]$ man man

causes the manual page for the `man' command to be displayed in the terminal window. Man pages provide the name and brief explanation of the command, a short synopsis, and a list of accepted arguments to the command.

Takeaway Message: Use the manual pages to explore the rich functionality of the many different Linux commands available on a typical system.

1024 Commands

This tutorial is based on a history of 1024 commands I gathered from one of my terminal windows (I have multiple open, each with their own extensive history). The history of each shell sessions is maintained in memory by the shell during runtime and written to the file .bash_history when the shell exits.

One thousand commands didn't seem like enough, and 1024 is a nice power of two.

So, what have I been typing for the last one-thousand twenty four commands? The simplest way to find out is to ask the shell with the `history' command:

[michael@host 1k]$ history
1  clear
2  more disorder.c
3  more ../include/disorder.h
4  more disorder.c
5  clear
6  cat disorder.c
7  clear
8  grep 8080 /etc/services
[michael@host 1k]$

What the shell displays is a numbered list of all commands it has kept track of. Already we can see a few different commands in play: `clear', `more', `cat', and `grep' -- and these commands seem to be operating on some additional data.

How can I save this history into a file so I can work with it? Fortunately, the shell provides input and output redirection. As you noted, the `history' command dumps its output to the terminal screen (named stdout for "standard output"). Using the output redirection character > we can send this output to a new file named 1k.dat.

[michael@host 1k]$ history 1024 > 1k.dat
[michael@host 1k]$

Here, we've asked the shell to print the history of the last 1024 commands, but instead of sending the output to the stdout, we've asked the shell to take that output and write it to a file named `1k.dat' (there is nothing special about this file name, and nothing special about the extension ".dat" -- we could have easily named the file just `1k'...file names in Unix do not require extensions.

One thing you'll notice about the command line is that it isn't very chatty -- when things work, it just returns a prompt to you. When things don't, it often returns a terse error message:

[michael@host 1k]$ foo
bash: foo: command not found
[michael@host 1k]$

Here, the shell (named `bash') tried to lookup and find the program named `foo', but did not find it (how this lookup proceeds is outside the scope of this tutorial, but the shell basically searches some common locations like /bin and /usr/bin).

Now that we have our last

Other Resources

  • In the Beginning Was the Command Line (Neal Stephenson)