Using Unix

Getting Started in Linux

Red Hat Linux is a version of Unix with some Windows-like features to make it easier for novices to use. For example, if you just want to browse the files on a Red Hat Linux system, you can do this by clicking on the icon at the bottom of the screen that has a small house in front of a file folder. This will bring up a file viewer somewhat similar to Windows Explorer. Select Show Tree under the View menu and it will look even more like Windows Explorer. You can use this to navigate through the file system and do simple file operations such as copying (often easiest if you right click on the file to be copied).

Real Unix

Unix has traditionally been a text-based operating system, and experience "power" Unix users still use it largely in this way. This means that for the most part, when using Unix, you will type commands rather than using a Graphical User Interface such as Windows-like one described above. Under our Red Hat Linux operating system, the place where you type Unix commands normally is in a "shell window". You can produce a shell window by clicking on the icon at the bottom of the screen that looks like a seashell in front of a monitor.

The shell window will display a "prompt" that has some stuff in square brackets ([]) and ends in a dollar sign ($). When you click on the Shell Tool window and begin typing, the characters you type will appear after the prompt. If you make a mistake as you're typing, use the Delete key to back up as far as necessary, then start typing again. When you're done typing a command, you press the Enter key.

One thing you should know about Unix that is a bit weird if you're not used to it: if things work all right, it doesn't tell you. I guess you could say that Unix is a "no news is good news" style operating system. So here's what a typical sequence of commands that work will look like when you type them into the shell window (remember, the "$" is the Linux prompt (and the stuff in brackets will look different for you than it does for me); you only type the stuff after the prompt):

[jackson@server3 jackson]$ cp sample.html public_html
[jackson@server3 jackson]$ cd public_html
[jackson@server3 jackson]$ 
We'll talk about what these commands mean below.

Unix directories

As in Windows, the Unix operating system supports the idea of a directory (but it's called a folder in Windows). A directory is just a container for other files and directories. The root directory on the Unix machines is "/" (there is no drive designator, such as A:, in Unix). This root diretory has a sub-subdirectory /home which in turn has a lot of subdirectories, one for each person who has an account on the Math/CS Unix machines. The name of each of these directories is the login-name of a user of these machines, where "login-name" is the name from the sheet of paper handed out earlier in the course that contained your password. For simplicity, let's assume your login name is "doe". Then the directory /home/doe exists on our machines. In fact, normally, this directory (and its subdirectories) is the only place where you can create new files on the Unix machines. Similarly, normally you cannot even see the files in other users' directories. This way you don't accidently mess up someone else's files, and they can't see your files unless you want them to.

Many commands that you type into the Unix operating system instruct it to do something with a file or directory. For example, you may want to see all of the files and subdirectories contained in the directory /home/doe. To do this, you can type at the Unix command prompt "[doe@server3 doe]$":

ls /home/doe
(ls is short for "list"). If you want to see all of the files in the public_html subdirectory of doe (this subdirectory was automatically created for you when the doe directory was created), you could type
ls /home/doe/public_html
But as we already said, Unix is set up so that for the most part, the only files you are going to use are in /home/doe. So it seems to be a waste to have to type this in each time. Thus Unix provides the notion of the "current (or "default") directory. When you log in, your "current" or "default" directory is /home/doe. If in a Unix command you type a directory or file name that does not start with a "/" (the root directory name), then Unix assumes that you want it to append "/home/doe/" to the front of the directory or file name. So if after logging in you type
ls public_html
then you will see a list of the files in /home/doe/public_html, and typing "ls" by itself will show you the files in /home/doe.

Sometimes, you may want to have Unix append the name of some other directory to your file and directory names. The "cd" (for "change directory") Unix command allows you to do this. For example, if after logging in you type at the Unix prompt [doe@server3 doe]$:

[doe@server3 doe]$ cd /home/doe/public_html
[doe@server3 doe]$ ls
then you will see the files in the public_html subdirectory of doe. In fact, since /home/doe is the current directory when you first log in, you could have instead typed "cd public_html" above and had the same effect.

The name of the "parent" directory (the directory one above the current directory) is "..". So if the current directory is /home/doe/public_html and you want to go back to doe, you can type either

cd /home/doe
cd ..
But note that if you type "cd doe" then Unix will think you want to go to the doe subdirectory of public_html, that is, to the directory /home/doe/public_html/doe. If this directory has not been created, you will get an error message.

Unix also provides the notion of a "home" directory. This is the current directory you have automatically when you log in, that is, /home/doe. You can also change the current directory back to the home directory by typing just "cd" at the Unix prompt.

Finally, the current directory has a shorthand name as well: the current directory is ".". So the commands "ls" and "ls ." give the same results.

Some Useful Unix Commands

We discussed the ls and cd commands above. Two other useful commands are mv (move) and cp (copy). After moving a file, there is still just one copy of the file, but now it has a new name and/or is in a new directory. A copy, on the other hand, produces a second copy of the file with a new name and/or directory. Unlike Windows 95, you use mv to accomplish renaming as well as to move from one directory to another.

The way you type in a mv or cp command is:

mv old new
cp old new
Here "old" is the name of the file you want to move or copy, and "new" is either the name of a directory (in which case the old file is moved/copied from its old directory to the new one and given the same name) or is a file name (in which case the old file is given the new name). So if you have a file "sample.html" in the doe directory and want to move it to the public_html subdirectory of doe, you can type
mv /home/doe/sample.html /home/doe/public_html
Or, if your current directory is /home/doe, you can just type
mv sample.html public_html
Note, however, that once you do the move, sample.html is no longer in the doe directory. So if you type this command a second time, you'll get an error message:
[doe@server3 doe]$ mv sample.html public_html
mv: cannot access sample.html
[doe@server3 doe]$ 
Unix is saying it cannot access (find) sample.html in the current directory. You'll also get this message if you mistype sample.html; if so, just type the command again, this time with the correct spelling.

Another message you may get when you try to mv is:

[doe@server3 doe]$ mv sample.html public_html
mv: overwrite public_html/sample.html (y/n)?  
This message asks if you want to replace whatever was in public_html with your current directory's version of sample.html. You could get this message, for example, if you created a sample.html, decided you hadn't done it write, and started all over by using Save As from Netscape to save a new sample.html to your current directory. If you want the replacement to occur, type "y" (no quotes) before pressing the Return key, otherwise type "n".

Sometimes, you can make a mistake in Unix and not produce an error message. Let's say you have the file sample.html in the doe directory and want to move it to the public_html subdirectory, but accidentally type

mv sample.html public-html
instead (you typed a dash (-) rather than an underscore (_)). In this case, Unix will assume that you wanted to move sample.html to the file named public-html, since there is no directory by that name in doe (unless you created it, but let's assume you didn't). In this case, you won't get an error message, but you also won't have sample.html in public_html after typing this, which is what you wanted. But all is not lost: you could now type:
mv public-html public_html/sample.html
This says to move the file public-html (which you created accidentally by the mv above, and which contains the same information that sample.html contained) to the subdirectory public_html and to rename the file to sample.html. This is a bit tricky, and more than you need to know about Unix. But just in case you made the mistake above, this is a way to get back on track.

Another useful Unix command is chmod (change mode). Remember that we said Unix protects your files so that other people cannot normally read them. chmod let's you override this protection for specified files. There are many possible ways to use chmod, but the one we usually use in connection with html files is:

chmod a+r *
The "*" here is the Unix filename "wildcard"; it stands for "all files in the current directory". The a+r translates to "all can read", that is, everyone can read all of the files in the directory where you type this command. So you should be careful what directory is the current directory before typing this command! You can type "pwd" (which stands for "present working directory") at the Unix prompt to find out what your current directory is.

Another thing you may want to do in Unix is "clean up" your files by getting rid of files you are no longer using or that got into your directories by mistake. The "rm" (remove) command is what you need. For example, if you accidentally created a file named oops.html and now want to get rid of it, you would cd to the directory containing the file and type:

rm oops.html
Finally, to learn more about these Unix command (or other commands), you can use the "man" (manual, as in, "when in doubt, read the manual) command. Just type man followed by a space followed by the name of the command you want information about. For example, to learn more about the rm command, type
man rm
If the manual page for a command won't fit on a single screen, which is often the case, you will see --More-- at the bottom of the screen. Pressing the space bar will take you to the next page of information. Pressing the Enter key will take your forward one line in the manual entry. Pressing the letter b (for "back") will go back a page, and pressing q (for "quit") will tell Unix to stop showing the manual entry to you. To learn more about the "more" facility, just type "man more"!

Transferring files between Windows and Unix

If you have a Windows file on floppy disk and want to transfer it to your Unix account, simply insert the floppy into the drive and click (once) on the floppy disk icon along the left side of the screen. This will pop up a window (like Windows Explorer) showing the files on your floppy. You can either use the window to copy files to somewhere in your home directory structure, or you can use the cp command to copy files from /mnt/floppy (the name of the root directory of your floppy, akin to A:\ in Windows). Another way to transfer files to Unix without using a floppy disk is by using a utility called "ftp" (File Transport Protocol). To ftp a file from Windows to Unix, bring up the Run... box from the Windows Start menu, then type in this box:
A window pops up and asks for your username. Supply your mathcs username and, when prompted, type in your mathcs password. Once logged in, you will see the prompt FTP> in the window. Now lets again say the the file you want to transfer is called pc.html. Then just type after this prompt:
put a:pc.html
This will put the file pc.html in your home directory on Unix. You can then log in to your Unix account and use mv as described above to move the file to any other directory, rename it, etc. If you instead want the file to go directly into a different directory, you can tell ftp to do this. For example, let's say you want the file to go into your public_html directory. Then after you get the FTP> prompt you would type:
cd public_html
put a:pc.html
This tells ftp to first go to the public_html directory on Unix and then perform the transfer.

No matter how you transfer the file, you may need to perform a chmod operation as described earlier if you want other people to be able to see your file.