UNIX
Basic Training


Today's your first day on a UNIX system; you want to learn just enough to do something useful. No long explanations, please!

Most computers running UNIX include a visual interface or "GUI", and a "typewriter" interface called the "terminal". We'll concentrate on the terminal. But you might be interested in keeping an eye on the visual interface as you issue commands to the terminal; some of your commands will make icons disappear or change their names.

Once you've logged in with your username and password, you'll probably see the visual interface. But we're looking for a terminal window, a rectangular box that fills up with letters when we type commands. Look on the menu bar for something like "Applications" or "System Tools", and then perhaps "Terminal".

Once you get the terminal open, Unix places you in what's called your home directory. In this home directory, you can create and store files, and issue commands. In particular, let's use the pwd command to find out the full name of our current location, and the ls command to get a list of what's there now (probably nothing!):

        pwd
        ls
      
Now would be a good time to find your current directory on the visual interface so that we can watch what happens next. The touch f1 command will create a file called f1 if such a file doesn't already exist. Let's make some dummy files:
        touch watson.c
        touch crick
        ls
      
The ls command should show two new files, and if you've got the visual interface view of your home directory, you should also be able to see them.

We can use commands to copy or rename files as well:

        cp watson.c holmes.c
        ls
        mv crick crock
        ls
      
See how the cp command makes a copy, but the mv command just renames a file?

Our files don't actually contain any information. We're not ready for an editor yet, so let's find a simple way to put stuff into a file. The output of any Unix command can be used to create a new file by using the "output redirect" symbol, which is >. Now ls is a command, and it makes output. Let's create a new file that grabs that output. Once we have it, one way to examine it is with the more command:

        ls > listing
        more listing
      

Now we have created four files in our home directory. Suppose we are sure we don't need "holmes.c" any more. We can use the rm command to get rid of it. Warning: the rm command is merciless. There is no trashcan where you can retrieve a file after you're rm'd it!

        ls
        rm holmes.c
        ls
      

From your home directory, you can create subdirectories. You choose a name for the subdirectory, and you can store files there as well. This is a convenient way to organize your information. (And you can make subsubdirectories and so on.) We create a subdirectory of our current directory with the mkdir command. A subdirectory will show up in the ls command along with the files we have:

        mkdir history
        mkdir geography
        ls
        cp listing history
      

Unix keeps track of where we are, that is, which directory we are in. To change directory, we use the cd command. Let's go to the history subdirectory. If you use the pwd command to report your location, you'll see that that it's simply the location of the home directory, followed by a slash and the name of the subdirectory. Since we just created this place, the ls command indicates there are no files, so we'll create two. Then we'll copy one file to our home directory, and move another to geography:

        cd history
        pwd
        ls
        touch apple
        touch orange
        ls
        cp apple ..
        mv orange ../geography
        ls
      
Notice that the mv command has now been used in what seem like two different ways: we used it to change the name of a file, and now we've used it to change the location of a file. Actually, the mv command can always change a file's location or name or both.

Let's go back up to the home directory, using a plain cd command with no subdirectory listed. Unix understands such a command to mean "go home!". Then we'll pop down to the geography directory and see what's there, then return to the home directory, but this time using ".." as our destination, which Unix understands to mean "go upstairs".

        cd
        cd geography
        ls
        cd ..
      

Now we'll try to get rid of the history directory with the rmdir command. Unix won't let us do this, because the directory still has files in it. So we have to go down to the "basement", throw out the rubbish, go upstairs and then...we can delete the basement!

        rmdir history
        cd history
        ls
        rm apple
        rm listing
        cd ..
        rmdir history
        ls
      

To exit from the terminal, you can issue the command exit:

        exit
      

CommandMeaning
pwdWhat is the name of the present working directory?
lsList files (and subdirectories) in this directory
touch f1Create file f1 if it doesn't exist
cp f1 f2Copy file f1 as file f2 (now you have two files)
mv f1 f2Rename file f1 as file f2 (one file, new name)
command > f1Redirects output of command into file f1
more f1Types out the contents of file f1
rm f1Remove (delete) file f1
mkdir subCreate subdirectory sub
cp fi subCopy file f1 to subdirectory sub.
cd subGo DOWN to subdirectory sub
mv ../fi .Move file f1 from directory above down to this subdirectory.
cp fi ..Copy file f1 from this subdirectory to directory above.
cp fi ../subCopy file f1 from this subdirectory to the "neighboring" subdirectory.
cdReturn to HOME directory
cd ..Go UP one directory
rmdir subRemove subdirectory sub
exitEnd the terminal session


Last revised on 17 May 2012.