WebFactory

Working with file systems

UNIX

Unix is not typically a point and click, visual icon system like Macs and PC's. Most UNIX systems are run by line commands. Nearly all Unix computers at Princeton are connected to the Princeton campus network, and through it to the Internet, an international network of networks. All of CIT's computers use the same file server. This means that your files are always available to you, whether you log in to arizona or sesamest, or a SPARCstation (but you will need to explicitly move files on to and off of your departmental machines). Unix provides extensive on-line help files in the form of manual pages --"man pages" for short. The man command lets you read detailed descriptions of each Unix command.

For instance, to get help on the man command itself, type man man and press the Return key. (You nearly always have to press Return to "enter" a command, that is send it to the computer.) To get help on the cat command, you would enter man cat. The manual pages for the command will be displayed one screen at a time; to see the next screen, press the space bar. To go back one screen, press the letter b key. If you just want to creep forward one line at a time, press Return.

 

File and Directory Structures in Unix

Files on Unix computers are organized into directories. A directory can contain files as well as other directories. Each directory, in turn, can contain files and directories. Unix associates a particular directory, called a home directory, with each user. Your home directory (or directories you create within it) is where you store your files. Whenever you log in, you start out in your home directory. Unless you specify otherwise, any files you create will be stored there.

The directory in which you are working is called the current directory. If you refer to a filename without specifying a directory, Unix assumes you want to find or put the file in the current directory. If you decide to work on a project, say designing widgets, you may want to use the mkdir (make directory) command: mkdir widgets to create a subdirectory within your home directory to hold your widget work. You would move to it with the cd (change directory) command: cd widgets.

Basic Unix Commands

ls

The ls command lists the contents of a directory. If you just enter ls, it will show you the contents of the current directory. If you have a subdirectory named foo in your current directoryand you enter ls foo, it will show you the contents of foo.

pwd

The pwd command (short for "print working directory") shows you the name of the current directory. If your userid is gwdoe and you are in the src subdirectory of your home directory, pwd might show you something like /auto/u/gwdoe/src. The /auto/u/ part is where your home directory happens to be stored and you only need /u/... to get to it. (This may vary depending on what kind of computer you are using.)

cd

The cd command lets you change your current directory. If you enter cd src (and there is a directory called src in your current directory), you move down into the src directory and it becomes your current directory. To back up one level, you can type cd .. (read "c-d space dot dot"). The two periods are a notation for "parent directory of the current directory." One period is a notation for the current directory itself, but obviously in this case cd. isn't very useful. Typing cd without specifying a directory takes you to your home directory.

mkdir

The mkdir command allows you to make a new directory. For instance, if you are in directory food in your home directory, and you enter mkdir fruit, a new directory called fruit is created as a subdirectory of food. (The mkdir command doesn't change your current directory; if you want to move to fruit you have to explicitly say cd fruit after creating it.)

rm

The rm command allows you to erase ("remove") a file or files. If you want to erase the file foo in your current directory, you can say rm foo. The default initialization files you get with your account are set up to tell rm to ask for confirmation before erasing files, so unless you have modified them, you will be asked rm: remove foo? If you enter y, the file will be erased; if you have changed your mind you can enter n.

rmdir

The rm command allows you to remove files, but it doesn't work on directories. To remove a directory, first delete all of the files (and/or subdirectories) in it, and then use the rmdir command. Note that rmdir isn't set up to ask you for confirmation. (Since you can only remove directories that are empty, though, this isn't a real problem.)

cp

The cp command makes a new copy of a file. If you have a file that you call green and you enter cp green green.backup, you will create a new file called green.backup with the same contents as green. You can then edit each file separately. Note that if there is already a file called green.backup, it will be replaced and its old contents will be lost.

mv

The mv command lets you move or rename a file. If you have a file called friends and you enter mv friends enemies, the file is renamed to enemies. You can also use the mv command to move a file from one directory to another. If you have a file called friends and a directory called notes in your current directory, typing mv friends notes will move the file from the current directory into the directory notes.

chmod

The chmod command sets the permissions of a directory or a file on the UNIX server. To set the permission for a file xyz.fil to 644, enter "chmod 644 xyz.fil". When you are setting the permissions for multiple files, you can use the wild card "*". For example, if you want to set the permission of all of the files with the extension .html to 644, enter "chmod 644 *.html". You have to set permissions correctly in order to make files readable by other users. For more information use man command.

 

Setting Permission for your HTML documents

If you want to make your HTML documents world-readable, or accessible on the World Wide Web, you must set the permission of your directory and your files correctly. The WebFactory Courseware automatically creates a public_html directory in the user's account and sets these permissions if they do not already exist.

First, the permission of your directory must be set. If you do not have a public_html directory, in which the HTML documents will be stored, you must execute:

/usr/princeton/bin/wwwpublic

on your Unix account. This command creates public_html directory in your home directory and set the permission correctly. The permission of your home directory must also be set to be world-readable. To set this permission, execute

cd ..

chmod 755 [your net ID]

cd

from your own home directory.

Next, you have to make your HTML documents world-readable. To do so, the permission of the documents must be set to 644. In the public_html directory (or other directory in which the HTML documents are stored), execute

chmod 644 *.html

Note that the permission of the files created on Web Factory is set to 644 by default.

 

Shortcuts for Typing Commands

To repeat the last command, type !! and press Return.

 

VI - A Unix Editor

To create a new file or edit an existing file, enter vi filename where filename is the name of the file you wish to edit. Try to use only letters, numbers, the period (.) and the underscore (_) in your filenames. At the top left of the screen, you will see the cursor, a blinking line or square that shows you where the next character you type will appear. The left side of the screen contains a column of tildes (~). If the file already exists, its beginning should appear on the screen, with the cursor at the top.

 

Moving Around in a File

To use vi effectively, you must be able to move around in the file. On many terminals, the arrow keys enable you to move one space left or right or one line up or down. The Return key and the plus and minus keys on the numeric keypad also may be useful. Below are several other keys you can use to move around in a file:

h

Moves the cursor one space left in the file.

j

Moves the cursor down one line.

k

Moves the cursor up one line.

l

Moves the cursor one space right. (The space bar also does this.)

Control-f

Moves forward one screen.

Control-b

Moves back one screen.

Adding Text to a File

When you begin working on the file, you are in command mode. In command mode, vi interprets every character you type as a command. To add text, you must go into input mode by issuing an appropriate command. Below are commands you can use to get into input mode:

i

Insert text before the cursor

a

Insert text after the cursor

O

Insert text on a new line above the current line

o

Insert text on a new line below the current line

I

Insert text at the beginning of the current line

A

Append text at the end of the current line.

Changing a File

You can change your file in several ways. Some of the most useful are listed below:

dd

Delete a line

dw

Delete to the end of a word

x

Delete the character at the cursor

u

Undo the last change made on a line. Pressing u twice undoes the first undo

U

Undo all the changes made to the last line that was changed. Pressing U twice results in an unchanged file.

Leaving VI

You can use several commands to leave vi. Press Return after typing each command -- except ZZ:

:q

To quit from an unchanged file

:q!

To quit from a changed file without saving the changes.

:wq

To quit from a changed file and save the changes in the original file.

ZZ

To quit from a changed file and save the changes in the original file.

:wq filename

To quit from a changed file and save the changes in a new file.