Programming in Linux (part 1)

This document and its successor summarize the essential commands needed to edit, compile, and run programs in the Linux environment. There are many more detailed tutorials on these topics on the Web; here are a few:

There is also a good book available from O'Reilly which covers all of this material in more depth: ``Programming With Gnu Software'', by Mike Loukides and Andy Oram.

The following material was originally written by Art Matheny of the University of South Florida, formerly available at http://curiac.acomp.usf.edu/ism3230/unix.html. It has been modified slightly to match the environment here at DePauw.

Getting Started with Unix

This tutorial is for those who have never used Unix before. It covers only the most essential commands that you need to begin with and indicates where to look to learn more. It is assumed that you have this document in printed form so that you can go through the steps on the computer as you read.

Log In
 

To begin with, you need to log in to oort or one of the Linux machines in Julian 276. If you want to log in from across the network, use ssh oort.csc.depauw.edu (on Windows you will need to install a client such as PuTTY, available from http://www.chiark.greenend.org.uk/~sgtatham/putty/). For a graphical interface, use the X-Win32 program installed on the campus lab PCs.

You will need the username and password from the sheet I gave you (and you will probably want to change the password to something more memorable, with the kpasswd command).

The unix prompt
 

Unix is fundamentally a command-line operating system. If you are logged in with an X Windows session (either in 276 or by using X-Win32), you will see a familiar windowed interface, but this is just a front-end for the underlying system. To do much of your work, you will need to open one or more ``Terminal'' windows.

To make it go, you have to type in commands at what is called ``the unix prompt''. The prompt is usually the name of the host followed by a dollar sign, but it varies depending on how the system administrator has set it up. When the cursor is on the line with this prompt, unix is awaiting your command. In the examples below, we will assume the prompt is ``oort$''. You do not type the prompt; you type what comes after it.

Some Essential Commands
 

Here is a walking tour of some essential unix commands:

  1. oort$ ls
    This shows a list of the files in the current directory. You might not have any files yet, so let's make one.

  2. oort$ cp /proc/version version
    This copies an existing file into the current directory. You own the copy.

  3. oort$ ls
    You should now see ``version'' among any other files that you have.

  4. oort$ ls -l
    Unix commands usually have options to modify how the command behaves. Options are usually specified by a hyphen followed by a single letter. In this case the ``-l'' option specifies that you want a ``long'' listing, which shows lots more information that just the file name.

  5. oort$ cat version
    This prints the contents of the file. It describes some of the details of the version of unix (actually, linux) that we are running. If a new version is installed, the original file will change. But the copy that you just made is yours and does not change until you change it yourself.

  6. oort$ pwd
    This shows the ``present working directory''. Unless you have gotten way ahead of me, this is your ``home directory''. You own this directory. You can create new files in you home directory and modify and delete them at will. Other users on the system have their own home directories, and you cannot mess with their files unless they explicitly grant permissions that allow you access to them.

    Take a close look at the output of the pwd command. This is what is called a ``path'' in unix. Unix organizes files in one big tree, where each node of the tree has a name. The path is the sequence of nodes the system needs to follow to get from the root of the tree to your home directory.

  7. oort$ mkdir xyz
    You can build up the unix directory tree starting from your home directory. The mkdir command makes a new directory. Let's see where unix put it.

  8. oort$ ls
    The xyz directory is within your home directory. Directories can contain directories as well as files! You can now put files in the xyz directory.

  9. oort$ cd xyz
    This changes your current working directory to the xyz subdirectory.

  10. oort$ pwd
    Notice that the xyz node has been added to the path.

  11. oort$ cat version
    This results in an error because there is no file called version in the current directory.

  12. oort$ emacs tutorial
    Type the current paragraph of this tutorial. Use the cursor keys to move the cursor around. Use backspace to correct mistakes. When you are finished hit `Control-X Control-S' to save the file and then `Control-X Control-C' to quit (if you are using X Windows, you may also perform these actions by selecting menu actions or clicking on toolbar icons). You should now be out of emacs and back at the unix prompt.

  13. oort$ ls
    The ls command shows the files in the current directory, which is the xyz subdirectory. So the files in your home directory do not appear. The only file that show up at this point is the file that you just created with the text editor.

  14. oort$ cd ..
    The double-period designation in this command is unix notation for the ``parent directory''.

  15. oort$ pwd

    You should now be back at your home directory. When you said ``cd ..'', the current directory moved from xyz to its parent, which is your home directory. The xyz directory is a ``subdirectory'' of your home directory. The home directory is the ``parent'' of xyz. A directory may contain any number of subdirectories, but a directory has one and only one parent. (Exception: the root directory, whose name is just /, has no parent.)

  16. oort$ mv version oldversion
    This moves the file. In this case that means that the name of the file is changed.

  17. oort$ ls
    Note that version is gone, but oldversion is now there. It is the same file, but with a different name.

  18. oort$ mv oldversion xyz
    This is different from the previous mv because the destination is a directory.

  19. oort$ cd xyz

  20. oort$ ls
    Note that oldversion has been moved into the xyz subdirectory.

  21. oort$ cat version
    Again this results in an error because there is no file called version in the current directory. But we know that there is a file named version somewhere in the tree. How do we access it?

  22. oort$ cat /proc/version
    You can always give the ``full path'' name of a file regardless of the current directory. Full path names begin with a slash (/) and list all of the nodes in the path from the root directory to the file. In this case the path is short, but in general there may be many nodes to type.

  23. oort$ cd
    Sometimes you just want to get back to your home directory. The cd with no arguments does exactly that.

  24. oort$ ls xyz
    This lists the files in the xyz directory.

  25. oort$ cat xyz/oldversion
    This displays the contents of the oldversion file. This command illustrates what is called a ``relative path'' name of a file. Since the path does not begin with a slash (/), the system follows the specified tree nodes beginning with the current directory. So the path is relative to the current directory.

  26. oort$ ls ..
    The relative path may contain ``..'' to access the parent directory. This command therefore shows what is in the parent of your home directory (probably a bunch of other people's home directories).

  27. oort$ man ls
    There are many, many commands on a unix system. The printed manual that explains the syntax and options for each command is several volumes in size (and very expensive). But the manual pages are installed on-line on most systems. The man command takes the name of a command or program as its argument. It displays the manual page for that command or program. In this case, you see the man page for the ls command.

    When you are in the man display, use the following commands:

    space bar advance to the next page
    return (or enter) advance one line
    q quit
    ? help

Explore man pages
 

Now that you know about the man command, you can find out about other important unix commands. What you should do now is read the man pages in the list of must-know commands below. These give a lot more detail than what you need to know to get started, but read the description of what the command does and skim the rest just to see what information is available.

man Manual page
more Display a file one screen at a time
ls List files in a directory
cp Copy a file
pwd Show path of current directory
rm Remove (delete) a file
rmdir Remove a directory
mkdir Make a new directory
kpasswd Change your login password
date Show the date and time
emacs Text editor (vi is also popular)
lpr Print a file
talk Live communication with other users
w Show who is logged in and what they are doing
ps Show what processes you are running
wc Word count utility

Part 2 will continue with the use of g++ and make to build programs written in C++.