This is an extract from PvdL's Guide to Linux.

The book for Windows users who want to try Linux.


Although this extract is about commands, most of the book uses GUI tools. You don't have to become a command line wizard to use Linux, but you shouldn't be afraid of it either. It's like learning how to drive a stick shift car as well as an automatic - you'll understand the vehicle better, and use its performance better.


Appendix 2 - Commands for the Command Line


A.2 Linux Commands

Too many people think the command line interface is hard to learn; it can be, if commands are presented as a long dreary list of mysterious incantations. But even a GUI is impossibly hard, if you don't know it exists or how to start it running.

We'll come at it from another direction: instead of focusing on commands, what's the object you're trying to look at or adjust? Is it a file? A folder? A filesystem? A running program? If you keep in mind what you are trying to work with, the number of applicable commands is smaller and much more manageable. Knowing and using a few commands raises you from the level of passenger ("where do you want to go today, pal?") to commander ("here's where I'm leading us").

When you get right down to it, the job of an operating system is to maintain your data in files, and to run programs to process those files. Everything is there to help with those two tasks. There are only about twenty or so frequently-used commands, and they are all very similar on Windows, MacOS X and Linux. Know these commands, and you'll be more effective with your PC.

We'll look at the commands in three sections: a primer on how commands are identified, then information about commands for files, then commands that work on folders.

About commands generally

There's nothing special or magic about the commands in Linux. They are ordinary programs which do ordinary things. You run any program by typing the complete pathname to the executable file. If you want to try these commands as you go along, start a command window (also called "konsole" in many Linux versions).

Executable files don't need a special file extension, like .com or .exe seen in Windows. As an example, there is an executable program in file /bin/date. It prints the current date and time. Try it. You can run the program like this.

It's not very convenient to have to remember and type an entire pathname for each program you have to run. It would be easier if you could just remember the filename "date", rather than the entire pathname "/bin/date". So that's the way the OS designers made it work. They did it in a general way that works for everybody. You specify the places (list of folders) where you want the system to look when you type a filename. This list of folders is given in a file you may not even be aware of.

The path

The file is called /etc/profile and it has a list of all the folders that the command line interpreter should look in, to find any command that you type. The list of folders is called the "path" and if you look in /etc/profile, you'll see a line like this that sets the path:
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/bin/X11" (There are actually two such lines; one sets the path for the root user, the other for other users).

That line is saying whenever I type the name of some executable file, the command line interpreter should look in these folders in this order: /usr/local/sbin, then in /usr/local/bin, then in /usr/sbin, then /usr/bin, then /sbin, then /bin, finally in /usr/bin/X11.

If the command line interpreter finds an executable file with that name as it looks in those folders in order, it runs it. If it looks in all of those folders, and doesn't find a file with the name you typed, it prints an error message saying "command not found".

The /etc/profile file applies to all users on the system who are using the command line interpreter called "bash". You can add to or change that system-wide path list in the file ~/.profile which is pre-installed for you in your home directory.

Watch out for that leading "." in the name of ~/.profile - it makes the file invisible to default directory listings (more on that coming up). Everyone starts with the same settings from the one /etc/profile file used by all. Each user has their own copy of this second .profile file so that they can customize the settings. The folders in the path are separated by colons, so if you add another folder to the path in ~/.profile, be sure to add the colon separator, too.

You can still type the entire pathname if you want, as we saw above. "/bin/date" produces the same result as "date".

The path is saved in an environment variable
The concept of having a path or list of folders in which to look when you type something that might be a command name is pretty useful. Windows uses exactly the same concept in its command line.

However, it's a bit inconvenient if a file is the only way to set the path. You'd really like something that can store the string representing the path, and have a way to change it or add to it from the command line (without needing to edit a file).

That's the purpose of environment variables. These are strings that the command line interpreter (also called shell) keeps track of on your behalf. Different shells use different syntax to set environment variables. Most of them let you retrieve the value in an environment variable by giving its name preceded by a dollar sign. The path is stored in an environment variable called PATH. You can see its value by typing the command

    echo $PATH 
      /bin:/usr/bin:/sbin:/usr/sbin:/usr/X11R6/bin 
The default shell on many Linux installations is the bash shell. The bash shell syntax for setting an environment variable, PATH for example, is:
    PATH="/bin:/usr/bin:/sbin:/usr/sbin:/usr/X11R6/bin:/new/folder/here 
You'll see people apply a shortcut, using the old value to set the new value, like this:
    PATH=$PATH:/new/folder/here 
That means "take the existing value of PATH and cram ":/new/folder/here" on the end".

When you change the PATH (or any) environment variable, the change is immediately effective.

Now, clearly, if you want to run a file that isn't in one of the folders on your path, you have to type the full pathname to the file. This often confuses new users. They install a program, and the installer puts it in a folder that isn't on the path. Then they try and run the program by typing the name, e.g. transmogrify. The command line interpreter gives the mysterious error message "sh: transmogrify: command not found", and the user gets mad and says "but it's right there - I just installed it!". (Other versions of Linux issue the message "No such file or directory").

The error message would be a lot clearer if it just mentioned the path: "shell error: file called transmogrify not found in any of the folders in the path, /usr/local/sbin, /usr/sbin, etc". At least then the user would have a shot at asking the right questions.

The Shell
We've mentioned "the command line interpreter" a couple of times. This is just another program, like all the rest. It is the program that takes input from the command line and carries out the commands, either directly or indirectly by running another program.

Linux has several command line interpreters, and the one you use by default is called "bash". The name is a multi-level pun and joke which you can look up on the web if you're so disposed.

The bash command line interpreter is the executable file "/bin/bash". A command line interpreter is called a "shell" in Unix. It's like a thin hard covering for all the real programs below. A shell has the job of getting lines typed by the user, looking along the path to find the corresponding file, and then running that file. If the name isn't a filename, perhaps it is one of the "built-in" commands to the shell itself (like the command to set an environment variable, or to change directory). If so, the shell will carry out that command. If the name isn't an executable file somewhere on the path, and it isn't one of the built-in commands, then the shell doesn't know what it is, and prints an error message.

Another use for Environment Variables

The shell also does several other important things we have not met yet: expands wildcards in filenames, and supports pipelines of commands. We'll get to these. The shell also keeps track of a dozen or so settings for each process (running program) using environment variables. The PATH is one of these. The Present Working Directory is another.

Present working directory is the notion of "where" in the filesystem a running program is. In other words the directory it will use if it writes a file. This starts out as your home directory by default, but a program can change the PWD. Type

    printenv
to see all your environment variables.

After the built-in command or executable program has completed, the shell will return to get the next line of input from the user, and repeat this over and over again.

Let's take a look at eight commands for doing things with files.

Working with files

Whatever data it contains - a word processing document, a web page, an image - there are some common things you might want to do with a file:

Here's how we do these things. In each case we show the general form of the command, with the part you have to supply in italics. Then we show a typical example of running the command, complete with output (if any).

Copy a file
    command: cp, meaning "copy"
    general form: cp   existingfile   copy-of-existingfile
    example: cp   ~/.profile   ~/.profile.bak

Move a file somewhere else
    command: mv, meaning "move"
    general form: mv   file-or-directory   new-filename-or-directory
    example: mv   ~/dogs.jpg   ~/Desktop

Give a file a different name
renaming a file is the same as moving it to a new name
(see previous command)
    example: mv   resume.draft.doc   resume.final.doc

Delete a file
    command: rm, meaning "remove"
    general form: rm   file
    example: rm   /tmp/*

Find out what kind of file it is
    command: file, meaning "tell me about this file"
    general form: file   somefile
    example: file   ~/dogs.jpg  
    output: /root/dogs.jpg: JPEG image data, JFIF standard 1.01

List the meta-data of a file
    command: ls -l, meaning "list" and the "-l" option says "in long form"
    general form: ls -l   file-or-directory
    example: ls -l   ~/dogs.jpg
    output: -rw-r--r-- 1 root friends 993019 Jan 19 19:29 dogs.jpg

Type out text file contents
    command: cat, meaning "concatenate" (this name has alway been ill-fitting; it would have been named "type" except it is also used for joining several files together)
  general form: cat   somefile
    example: cat   ~/.profile
    output: the contents of the ~/.profile file, namely:
# ~/.profile: executed by Bourne-compatible shells.

if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi

mesg n

Search through a text file for a particular string
    command: grep, meaning "Globally search for Regular Expression and Print"
    general form: grep   "some-string"   somefile
    example: grep   ";" ~/.profile
    output: the lines in the file that contain the search string, namely:
if [ -f ~/.bashrc ]; then


To learn more about commands, download the "manpages" and "man-db" packages from CNR. Manpages is an abbreviation for "manual pages". These are "help" files for each command.

After installing the manpages and man-db (manual database), you can look at the manpage description of any command, by typing this:

For example, "man date" produces a couple of pages of output, starting like this. Whether or not you install the manpages, most commands will give you a brief summary of the options they expect, if you give them a single "--help" option, like this: You'll get about a page of output, starting like this:

Y r cmd nms so mystrsly shrt?
The architects of Unix, Ken Thompson and Dennis Ritchie of Bell Labs, obviously like very short names. Linux adopted the same conventions wholesale, so Linux commands are really not intuitive.

There's no getting away from it; command line Unix was designed to allow specialists to work with the minimum of typing. Ken Thompson was once asked if, in hindsight, there was anything he would change about Unix.

In a tongue-in-cheek acknowledgment that brevity can be taken too far, Thompson replied that next time round, he would spell the "creat" system call with an "e" at the end.

Only computer programmers ever see "creat" and we can't spell anyway, so personally I'd be happier putting the "n" back into "umount".


Working with folders

We mentioned the "present working directory". This is the default directory used when a program reads or writes a file without giving a full pathname. Sometimes you might see someone type "./date" meaning they want to run the executable file called "date" that is in the current directory. The pathname "." is an abbreviation for "the current directory". The pathname ".." is an abbreviation for "the parent directory". The pathname "~" is an abbreviation for "my home directory".

Here are some common commands used with folders (folder and directory are synonyms):

Print working directory
    command: pwd, meaning "print working directory"
    example: pwd  
    output: the directory in the filesystem that is your present working directory, e.g.:
/root/Desktop

Move into another directory
    command: cd, meaning "change directory"
  general form: cd   some-directory
    example: cd   My\ Documents

List files in directory
    comments: Listing a directory is the same command as listing a file. Just give a directory name instead of a file name. No name means "list the current directory". Wildcards are fine.
    example: ls   /home/pvdl/Desktop

Create a directory
    command: mkdir, meaning "make directory"
  general form: mkdir   some-directory
    example: mkdir   My\ Documents/todays-work

Delete directory
    command: rmdir, meaning "remove directory"
  general form: rmdir   some-directory
    example: rmdir   Draft-docs
    alternative: rm -rf   Draft-docs

Move or rename a directory
    comments: renaming a directory is the same as renaming or moving a file
(see "mv" command above)

Copy a directory
    comments: copying a directory uses the same command as copying a file, with the addition that you must use the "-R" option. That option says "copy recursively". The second directory gets a copy of the first directory and everything in it, including subdirectories, and everything in those, and so on.
    command: cp, meaning "copy"
    general form: cp -R   existing-directory   copy-of-existing-directory
    example: cp -R   ~   /homedir-backup

What Else Is There to Commands?

Continuing the progression of file, and folder, there are half a dozen common commands that deal with a filesystem as a whole. They include commands like mount and umount (to make a filesystem active or inactive). There's a "gotcha" with umount (apart from the fact that it is not spelled "unmount") - the command won't succeed if any file in the filesystem is in use. So you have to cd out of all directories in the filesystem you are trying to umount, and stop all processes that are reading or writing to files in the filesystem.

There is also the filesystem checking utility reiserfsck that can be run on an unmounted reiser filesystem to confirm or restore its consistency. You might run reiserfs after an unscheduled power off. You can run it to check your main disk by booting under a live CD (you cannot check a hard disk that is in use, and the Live CD leaves your disk unused). Then issue the command:

You should use the device name for your actual hard disk, which might or might not be /dev/hda as in the example.

We have also omitted mentioning utilities which let you look at processes, change their priority, stop them, find out what files they have open, and so on. The command

is a particularly effective one for finding out which processes started other processes.

My editor is frantically waving his hands and making chopping motions to tell me to cut this manuscript off now and put it in print. (All right, Greg). So I don't have the go-ahead to even mention the commands that describe your PC hardware, like:
dmesg display the system message buffer
/sbin/lsmod show information about all loaded modules (device drivers)
/sbin/modinfo somemodule show more about somemodule
lspci show information about all devices on the PCI bus
cardctl info show information about PCMCIA ports
netstat list IP connections
free show the amount of physical and virtual memory

A Linux module is a program or library (usually a low level one) that can be compiled separately from the kernel, and is loaded at run time into the kernel address space while the kernel is running. Modules are typically used to implement device drivers, so drivers follow both the device driver interface and the module interface.

How powerful are Unix commands?

rev lewis.txt | sort | rev > lewis-rhymes.txt

How powerful are standard Unix commands? Let me show you something that was shown to me twenty years ago when I joined a fast-growing start-up company called Sun Microsystems.

If you have a list of English words, how difficult would it be to create a rhyming dictionary out of them? A rhyming dictionary is used by poets. It lists words in order of what they sound like, not how they are spelled. In a regular dictionary, the word "ride" is near the words "rice" and "rife". In a rhyming dictionary "ride" is near "glide", "hide", and "slide".

Perhaps your first thought, like mine, was that it cannot be done at all. Judging whether or not something rhymes takes human intelligence. Well, it turns out you can get a pretty good approximation by noting that words that are spelled similarly at the end (like "shoot" and "loot") probably rhyme.

So if you take your word list, and run a Unix command to reverse the order of letters of every word, then sort it so that words with the same letters at the end come next to each other, then reverse the letter order of each word again (without changing which word is adjacent to which other word) well, then you've got yourself a pretty good rhyming dictionary.

The Linux command line to do exactly that is the subheading on this sidebar. The word list can be downloaded from http://afu.com/linux/lewis.txt. This word list is a compilation of all the words that appear in Lewis Carroll's "Alice in Wonderland" popular books (a cheap and quick way to whisk up a manageable wordlist). Download the wordlist and try it yourself. The result is in file lewis-rhymes.txt.

An extract from the rhyming list is:

    
    hiding
    gliding
    sliding
    riding
    scolding
    folding
    holding
    handing
    landing
    standing
    ending
    bending
    defending
    sending
    pretending
             
How long would it take you to create a rhyming dictionary in a Windows GUI?

The biggest piece that we omit in this introductory primer is the piece that gives the Unix command line its greatest power. I am referring here to the shell constructs known as I/O redirection, filtering and pipelines.

Old hands don't prefer the Unix command line because they are stubborn die-hards. Well, maybe that's true for some of them. But most of them use the command line because almost all the commands give simple text output, and there are exceptionally good tools for taking the text output from one program and feeding it into another program, and the results of that into another program, and so on.

The Unix philosophy is that commands are small, and do just one thing well. Complex results are obtained by stringing a series of commands together with pipes. The second line of the sidebar title shows an example of a command pipeline.

That says run the "rev" command on file lewis.txt. Feed the output directly into the "sort" command. And feed the output from the sort into the "rev" program again, putting the output from that into the file "lewis-rhymes.txt". The vertical bar or pipe symbol "|" connects the output of one program into the input of the next.

By learning 20 or 30 basic commands plus the ways to glue them together, you can build up very powerful composite tools that tell you what a system is doing, keep it running, analyze your data etc.

If you want to learn more about these fascinating topics, Linspire has a thorough, if lengthy, guide at http://info.linspire.com/lindowsoscommands.html. You can also do a web search for "unix command line guide". And beyond even that, there is shell programming. In shell programming you write scripts of commands, that can be invoked by typing the name of the script file.

The installation of the operating system (any Unix operating system) is accomplished by running elaborate and extensive shell scripts that invoke the regular Unix commands. But we are getting a little beyond an introductory primer. And Greg is running out of patience with me.



Copyright © 2005 by Peter van der Linden. Distribution of this document is prohibited without the explicit permission of the copyright holder.