This tutorial helps you experiment with some basic unix commands. Work through the given examples and then clean up your experiments before continuing with your lab.

Before you begin, you’ll need to know the answers to the following questions:

  1. Should you turn the computer on and off, or does it remain on all of the time?
  2. You should have your own account on the lab’s computers:
    1. What’s your username?
    2. What’s your password? And how do you change it?
  3. In your windowing environment (e.g., MacOS, KDE, Microsoft Windows, etc.):
    1. How do you use the environment?
    2. How do you exit your environment?

Getting Started with UNIX

Your instructor will have to tell you how to access your account and how to start up the X Windows System and desktop environment. Unlike Microsoft Windows and Macintosh operating systems, there are several variations of X11 and many different desktop environments built on top of X11. Generally these desktop environments work on the same principles as Microsoft Windows and Macintosh OSs, so let your intuition guide you.

Regardless of what GUI you use on your UNIX machine, you can always interact with UNIX directly with a Command-Line Interface (CLI). Eventually, your instructor should tell you how to open a terminal window (a.k.a., an xterm or a console window). It will look something like this:

konsole picture

As pictured in this example, I use a two-line prompt:

[localhost:~]
$

A UNIX prompt is highly flexible. Yours is probably very simple. The purpose of the UNIX prompt is to prompt you for a command. If you don’t see a prompt, the terminal window is busy working on something else.

For this lab manual, we will use unix-% as a generic UNIX prompt. You don’t need to type in the UNIX prompt yourself. That’s the prompt from UNIX; you must enter in the command that you see after the unix-% prompt.

Interacting with UNIX

UNIX interaction consists of three steps which are repeated until you close the terminal window:

  1. The computer displays a prompt and waits for you to enter a command.
  2. You enter a command.
  3. The computer executes (performs) that command, and it displays the results (if any).

The UNIX environment is case-sensitive, and virtually all UNIX commands use lower-case letters. This will cause you a great deal of frustration if you forget it, so keep this in mind.

In order to use a command-line environment, you must learn those commands that the environment understands. Let’s first see what happens if you type something that the environment does not recognize.

Do this...
Enter
unix-% qwerty
				
Remember, you don’t enter the unix prompt (“ unix-% ”).

This is the way that UNIX responds when you give it a command it doesn’t understand.

Working with Directories

You will create dozens of files as you go through this lab manual, and it’s best to keep them organized separately from the other work that you do. Just as the documents (i.e., files) in a filing cabinet are often kept in manila folders, we can create a container in which to keep related files, called a directory (or a folder).

Do this...
Enter this UNIX command to create a new directory:
unix-% mkdir computing
				

After you type in the command, you should get the prompt back. If something goes wrong, then you’ll get an error message. But no message means that something was done successfully.

The mkdir command is the UNIX command to make a directory. By placing the word computing after the mkdir command, you tell the system that you want to make a (sub-)directory named computing.

Do this...
Practice using the mkdir command to make a directory in the computing directory named computing/practice .

The slash / character in these directory names is very important because you’re creating directories within a directory. This allows you to keep the files for this course separate from other files you might need.

Viewing the Contents of a Directory

The mkdir gives no feedback unless something goes wrong. So how do we know it did anything?

Do this...
Enter this command:
unix-% ls
				

The ls command displays a listing of the contents of a directory. If all is well, you’ll see at least the computing directory. You’ll undoubtedly see other directories and files, but the specifics differ in each type of UNIX.

But where are those other directories you created? They’re inside the computing directory. The ls command can also be used to find out what is in a particular directory.

Do this...
Enter this command:
unix-% ls computing
				

If you follow the ls command with the name of a directory, it displays that contents of that directory. Now you can see the (sub-)directory that you created before.

To get a more detailed listing of the current directory, enter

unix-% ls -l
			
The -l is called a switch or flag that causes the ls command to generate a long (i.e., detailed) listing. The hyphen indicates to ls that the l has a special meaning; it’s not the name of a directory or file. All UNIX commands have flags like this.

Identifying the Working Directory

When you begin a computing session, UNIX always starts you out in your home directory. You do not have to remain in your home directory; you can change to a different directory whenever you like.

The directory in which you are working at any particular time is called your working directory. To find out the name of the working directory, you can use the pwd (print working directory) command:

unix-% pwd
			

UNIX systems (like all modern operating systems) utilize what is called a hierarchical directory system, meaning that directories can contain other directories. There is one root directory denoted by / (a single forward slash) which is the top directory; all other directories are directly or indirectly inside this root directory.

Do this...
Run this command:
unix-% ls /

This lists the contents of the root directory. Within / are a number of subdirectories, including usr , bin , include , and home . Each of these contain other directories and files. On many UNIX systems, the home directories of the users are stored within the home directory. Here is what this might look like on a particular machine:

directory tree

UNIX treats the tilde character ( ~ ) as an abbreviation for your home directory, which can save you a lot of typing if the path to your home directory has lots of characters.

Changing the Working Directory

If you created all of your files in your home directory, it would soon become cluttered. By grouping related files within a directory, you can organize your files.

Since you are working on a lab exercise for this course, let’s change the working directory to the appropriate directory.

Do this...
First, enter this command:
unix-% cd computing
				

The cd command is the change directory command that changes the working directory to the specified directory. Similar to the mkdir command, cd will not display anything if it works successfully (although you might notice a change in your prompt). You will see an error message if something fails.

Do this...
Now enter this command:
unix-% cd practice
				

Use the pwd command to see if you’re where you think you should be.

So the cd command allows you to navigate down a directory hierarchy. What about going back up the hierarchy?

Do this...
First check out everything in your current directory by entering this command:
unix-% ls -a
				

The -a switch tells ls to list all of the contents of a directory, including files and directories whose names start with a period ( . ).

You will notice two odd items in this output: a period . and a double-period .. . In the UNIX file system, . is another name for the working directory (whatever directory you are in); and .. is another name for the parent directory of (i.e., the directory above) the working directory.

You created computing within your home directory. The cd computing command took you from your home directory “down” into computing . Once in that directory, . refers to that same directory while .. refers to the directory that contains it (i.e., your home directory). The cd practice command took you one level lower into the practice directory. Now that you’re in the practice directory, its . entry refers to itself, and its .. refers to the computing directory.

So now, to go back “up” a directory, you can enter this command:

unix-% cd ..
			

Since .. is always a synonym for the parent of the working directory, regardless of any actual names, you can always use this command in any directory.

There’s also a shortcut for changing directories back to your home directory: use the cd command without any arguments:

unix-% cd
			

Use pwd to verify that you are currently in your home directory.

Working with Files

UNIX stores all data in files. It stores textual data and programs in text files.

Creating a File

You can create a simple text file using a standard editor (e.g., VI, emacs, gedit, etc.) or within a more complex integrated development environment (IDE) (e.g. Eclipse).

Do this...
Use the simple editor gedit to create a text file containing the message “Hello, text file!”. You can start gedit by typing gedit & at the prompt (the ampersand makes it so you can continue to use the terminal window even while gedit is running). Save the file in the computing/practice sub-directory you created above under the name helloText.txt .

Viewing the Contents of a File

It is sometimes useful to view the contents of a file (without printing it or loading it into an editor). For example, this can be useful to verify that the files you’ve submitted electronically were submitted properly. When you do need to view the contents of a file, use the following command:

unix-% cat filename
			
Do this...
Make sure that you can view the contents of your sample text file. You will need to cd into the directory that contains the file (i.e., computing/practice/ ) and then use the cat command.

Copying Files/Directories

UNIX provides commands for copying files or directories from one directory to another. The smae command is used for copying a file to a new filename, and for copying the file into a different directory. The general form of the command is:

unix-% cp filename targetDirectoryOrName
			

If the targetDirectoryOrName given is a directory, a copy of the file will be put in the specified directory. If it is a filename, a copy of the file will be made with the new name.

Do this...
Make a copy of your helloText.txt file named anotherText.txt . Use ls and cat to verify that you now have two files with the same contents.

This copy command will copy a single file to the specified target directory. To move an entire directory and all its descendants, you can use the -r switch:

unix-% cp -r directoryname targetdirectory
			

This command copies the directory file and recursively copies all of its contents.

Removing a Directory

You can use the rmdir command to remove a directory just like you’d use mkdir to make a directory.

Whatever directory you specify following the rmdir command will be removed from the file system, provided that the directory being removed is empty. To remove a non-empty directory named directoryName , use this command:

unix-% rm -r directoryName

Be very careful when you use this command. The system will delete this directory and all its sub-directories without giving you any feedback, so be sure that directoryName contains nothing important!

Do this...
The system will not allow you to remove a directory that you are currently in, so begin by changing to your home directory. Now, use the rm -r command to remove the computing directory. Then, use the ls command to verify that the computing directory (and its sub-directories) no longer exist.

Here is a quick reference guide for these basic Linux/UNIX commands.

If you are in the midst of lab01, you should now return to those instructions.