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 detailled 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, 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 jupiter or one of the other Linux machines in Julian A216. If you want to log in from across the network, use telnet jupiter.csc.depauw.edu (more securely, use SSH instead of TELNET; on Windows you will need to install a client such as PuTTY, available from http://www.chiark.greenend.org.uk/~sgtatham/putty/). You will need the username and password from the sheet I gave you on the first day (and you will probably want to change the password to something more memorable, with the passwd command).
The unix prompt
  Unix is a command-line operating system. There are no icons to click on. 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 ``jupiter$''. 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. jupiter$ 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. jupiter$ cp /etc/motd motd
    This copies an existing file into the current directory. You own the copy.
  3. jupiter$ ls
    You should now see ``motd'' among any other files that you have.
  4. jupiter$ 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. jupiter$ cat motd
    This prints the contents of the file. Notice that this is the ``Message of the Day'' that appeared when you first logged in. The system administrator can change this file to inform you about what's new on this host. But the copy that you made of today's motd is yours and does not change until you change it yourself.
  6. jupiter$ 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. jupiter$ 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. jupiter$ 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. jupiter$ cd xyz
    This changes your current working directory to the xyz subdirectory.
  10. jupiter$ pwd
    Notice that the xyz node has been added to the path.
  11. jupiter$ cat motd
    This results in an error because there is no file called motd in the current directory.
  12. jupiter$ pico tutorial
    When you are in pico, there should be a menu at the bottom of the screen. Type the opening paragraph of this tutorial. Use the cursor keys to move the cursor around. Use backspace to correct mistakes. When you are finished hit `Control-O' to save the file and then `Control-X' to quit. (You need to answer some questions that appear on the message line.) You should now be out of pico and back at the unix prompt.
  13. jupiter$ ls
    The ls command shows the file 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. jupiter$ cd ..
    The double-period designation in this command is unix notation for the ``parent directory''.
  15. jupiter$ 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. jupiter$ mv motd oldmessage
    This moves the file. In this case that means that the name of the file is changed.
  17. jupiter$ ls
    Note that motd is gone, but oldmessage is now there. It is the same file, but with a different name.
  18. jupiter$ mv oldmessage xyz
    This is different from the previous mv because the destination is a directory.
  19. jupiter$ cd xyz
  20. jupiter$ ls
    Note that oldmessage has been moved into the xyz subdirectory.
  21. jupiter$ cat motd
    Again this results in an error because there is no file called motd in the current directory. But we know that there is a file named motd somewhere in the tree. How do we access it?
  22. jupiter$ cat /etc/motd
    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 are many nodes to type.
  23. jupiter$ cd
    Sometimes you just want to get back to your home directory. The cd with no arguments does exactly that.
  24. jupiter$ ls xyz
    This lists the files in the xyz directory.
  25. jupiter$ cat xyz/oldmessage
    This displays the contents of the oldmessage 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. jupiter$ 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. jupiter$ 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
passwd Change your login password
chmod Change access permissions of a file
date Show the date and time
pico Text editor (vi and emacs are 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++.