Skip to Content

Unix Help Notes

Below are old notes I had about the basics of Unix. These probably need edited quite a bit…

Files and Directories

In Unix, all data, programs, and anything else is stored in files. Almost all other computer systems use files, so this should be familiar to you. In Unix, a file (and a directory) can have almost any name, and it can use uppercase, lowercase, numbers, and periods (dots). It can use more than one dot, and can have more than three letters or numbers after a dot. Other symbols can be used as well, but we advise against it. Especially don’t use the * and ? symbols! One common symbol that is used is the underscore (_).

Files are organized into collections, called directories. If you have used Windows or Macs, these are like folders. A directory can contain files, and also other directories. These sub-directories can contain more files and even further subdirectories. Directories, then, form trees, with the subdirectories being branches off of their _parent _directory. Every directory uses the special name .. (yes, that’s dot-dot) to refer to its parent (and they use the special name . to refer to itself).

Unlike PCs running Windows, Unix doesn’t make you say what disk you are using. All disks are merged together into one big directory tree. The base of this tree is called the root, and is named /. All directories are below the root (in Unix, trees grow down!). Each file exists in a directory that has a _path _from the root to where it is. This path is the names of all the parent directories from where the file is to the root, separated by /’s. For example, I have a directory called /home/others1/jcook – this is the directory jcook in the directory others1 in the directory home, which is in the root directory. A sample directory tree is shown below (it’s just a tiny part of the whole Unix directory tree).

When you are using a terminal window, or even window applications, you are always somewhere in the directory tree. That is, your application is using some directory as its current working directory. In a terminal window, the command pwd__ will print the current working directory. The command cd __is used to move around in the directory tree. You can cd .. to move to the parent of the current working directory, or cd mysubdir to move to a subdirectory called mysubdir. Doing a cd / will move you to the root directory.

A file can be referred to in three ways. By just using its name, such as MyReport.text_, _you are referring to a file in the current working directory. You can use a _relative _pathname, such as Docs/MyReport.text, which specifies the file MyReport.text in the subdirectory Docs, or ../Docs/MyReport.text, which specifies the file in the directory Docs in the parent of the current working directory. Finally, you can use an _absolute _pathname, which always begins with /, and might be something like /home/others1/jcook/MyReport.text. This gives all the directories in the path from the root to the file.

An important directory for you to know about is your home directory. The name of my home directory is /home/others1/jcook – that is, it is the directory named jcook in the directory others1 in the directory home off of the root directory. If you type the command cd __without specifying a directory, it will move you to your home. So, if you get lost in the directory tree, just do a cd to get back home. Doing the commands cd and pwd in order will move you to your home and print out the whole pathname of your home directory.

There is a special name for your home directory, and that is ~ (tilde). For each user, ~ is a shortcut for saying “My home directory”. For me, ~/MyReport.text refers to the file /home/others1/jcook/MyReport.text.

You can even refer to other people’s home directories, using ~username. For example, you can refer to my home as ~jcook.

Questions and Tasks

  1. What is the absolute pathname of your home directory? How did you find out?

  2. Do cd , then do pwd , then do cd ../.. and finally do pwd again. What is printed out at each pwd? What happened between the two? Do cd again to move back home.

Move on to …

Basic Unix Commands

This page presents some of the basic Unix commands that you will use much of the time. The command (or sometimes two) is given on a typescript line, and then a paragraph of description follows. An argument to a command is shown as <arg> – the brackets just indicate that this is an argument, where you put in an actual word, or filename, or whatever. The brackets are not part of the real argument.

man <command>
man -k <topic>

man displays an information (or __man__ual) page about what a command does and how it is used. For example, man cp will tell you all about how to copy files. man man will tell you how to use man. The second form, man -k, will try to list the man pages that are relevant to the keyword topic that you asked for.

cp <file1> <file2> 

__cp c__o__p__ies the file named by <file1> to the file named <file2>. If there is already a file named <file2>, that file is destroyed and is replaced by the copy of <file1>. If <file2> is a directory name, then <file1> is copied to a file named <file1> in the directory <file2>.

mv <file1> <file2>

__mv m__o__v__es a file named <file1> to a file named <file2>. It basically just renames <file1> to be <file2>. If there is already a file named <file2>, that file is destroyed and is replaced by the <file1>. If <file2> is a directory name, then <file1> is moved to a file named <file1> in the directory <file2>.

rm <file> ...

rm __r__e__m__oves the file specified as <file>, and will remove as many files as are given as arguments. Be careful, because there is usually no way to get a file back once it is removed.

more <file>
less <file>

more and less both display a file on the screen, and pause the file display at each screenful, waiting for you to hit a key to display more. The difference is that more is only one-way, you cannot back up in a file; once you’ve seen a part and it has moved off the screen, you can’t go back. In less, you can. Usually, <space> moves forward in a file a whole screenful (a page), and <return> will move forward one line. In less, the arrow keys should let you move backward and forward through the file. More will automatically quit and return a command prompt at the end of the file, while in less you need to hit q. [The name less is simply a play on the name more – many people use the command alias more less in their startup (we’ll get to that), so that when they type more, they get less!]

cd <directory-name>

__cd c__hanges the current working __d__irectory to that named by <directory-name>. Some special directory names are ‘..’ for the parent directory, ‘~’ for your home directory. If you don’t give an argument to cd, it will change to your home directory.

pwd

__pwd p__rints your current __w__orking __d__irectory.

ls <many arguments>

ls l__ist__s a directory for you, so that you can see what files and subdirectories it contains. With no arguments, it just lists your current working directory. With an argument ‘-l’, it produces a ’long’ listing, which shows the file sizes, permissions, owners, date of creation, and other things. ‘ls -l’ is like the ‘dir’ command in DOS/Windows. To see the many ways ls can be used, do a man ls.

mkdir <dirname>

__mkdir m__a__k__es a new __dir__ectory, named <dirname>. <dirname> can be a simple name, which is created from the current working directory, a relative pathname, which will make a directory on the path specified that is relative to the current working directory, or it can be an absolute pathname (beginning with /) that will make a directory in the absolute path specified.

rmdir <dirname>

__rmdir r__e__m__oves the __dir__ectory specified. It will only remove it if it is empty, so you need to remove all files and subdirectories first.

quota -v

quota displays your disk storage limits and how much you’ve used so far.

echo <any arguments>

echo simply prints out all of the arguments it is given. Sounds useless? Well, no, because it can help you figure out what files match a pattern (we will get to patterns in a bit).

passwd

passwd lets you change your password. You should change your passwd any time you think someone else might know it, and also once or twice a semester just to be safe.

Questions and Tasks

  1. Do cd to make sure you are in your home directory. Then do the command mkdir cs171. Then do ls. What is printed?

  2. Do cd cs171 then do pwd What is printed?

  3. Do cp ~jcook/public_html/Classes/CS171/index.html syllabus.html

  4. Do ls. What is printed? Then do ls -l What is printed?

  5. Do more syllabus.html What happens? What does the file look like?

  6. Do cd then do rmdir cs171. What is printed? Do ls. What is printed?

  7. Do rm cs171/syllabus.html , then do rmdir cs171 What happened this time (hint: do ls)?

  8. Change your password. (Don’t write it down on your answer sheet!)

Move on to …

The Shell

NOTE: Because all of the undergrad computers are now Linux-based, the instructions on this page about changing your shell are obsolete. You should not attempt to change your shell. Still, it is useful to understand what the shell is, so you should read this page and answer the questions at the bottom.

Your __shell __is what your command line (terminal) window runs. It is the program that lets you type in commands, and it executes them for you.

By default, you have the __csh __shell. While this shell is nice, there is a new and improved shell that acts just like csh but only better. It is tcsh. With tcsh, you can use the arrow keys to bring back previous commands, and edit command lines. You can also use the <tab> key to let tcsh automatically finish typing command names and file names. This can really save on typing! [note: on Linux, the csh is the same as tcsh, so all of the capabilities described here can be used.]

For example, if I have a file named acm_application.text, and I type

more acm&lt;tab&gt;

tcsh automatically finishes the file name and displays

more acm_application.text

If more than one file begins with what you typed (in this example, acm), then tcsh will either beep or show you a list of the matching files, depending on how you have it configured.

To change your default shell, then, you use the chsh__ __command. Entering this command (no arguments are needed), it then prompts for your password, and then asks you what shell to change to. Here you must tell chsh the absolute pathname of the shell you want to use – you cannot just simply type in tcsh. The absolute pathname for tcsh is /local/common/bin/tcsh. To see all of the allowable shells on the system, do more /etc/shells.

Once you have changed your shell, the next time you login, your shell will be the new one. The login session that you are in will still be running your old shell.

NOTE: On our Linux computers, if __csh __is your shell, you are already using tcsh, so you do not need to change your shell.

Questions and Tasks

  1. Do “setenv | grep SHELL”. What shell does it say you are using?

  2. Do “set | grep version”. What version of the shell are you using, and when was it created?

Move on to …

Permissions on Files and Directories

Unix, unlike most PC systems, is multi-user. Many people are using the machines, and different parts of the directory tree and its files are owned by different people.

Many of the files that a person has need to be private; that is, they should not be readable by others. When I make an exam, I don’t want the students to be able to see it before it is given. When a student writes an assignment, they don’t want other students to be able to copy it. Also, if I write a file (such as this Web page), I may want other people to be able to read it, but I don’t want them to be able to change it.

Unix solves this by associating certain permissions with each file and directory. You can see the permissions on a file by using the command ls -l &lt;file&gt;. For example, here’s a sample:

`Permissions L Owner           Size Date&Time    Filename`
`-rw-r--r--  1 jcook           2629 May 13 10:17 acm_application.txt`

This line shows us alot of things, including the owner, the size, the date and time the file was created (or last changed), and the name of the file. The first field is the permissions. It is a series of letters (or a dash in place of a missing letter). The first dash is the _type _of the entry; a dash just means its a plain file. This might be a ‘d’ for a subdirectory, or an ‘l’ for a link.

The rest of the nine letters and dashes are groups of three permissions each, where a letter means the permission is granted and a dash means the permission is denied. The first three are the permissions that the owner of the file has. In this case, they are “rw-”, which means the owner has read and write permission on the file. The last dash would be an ‘x’ if this file was executable, and the owner would then be allowed to execute it. (Note that the permission ‘x’ just means you are allowed to execute it. It doesn’t guarantee that the file really can be executed!) The next three (r--) are the same permissions, but for the _group _that the owner is in. In this case, any user that is in the same group can only read this file, not write or execute it. Finally, the last three (r--) are the permissions for everyone else (all users on the system, better known as the world). In this case, everyone is also allowed to read the file.

Usually all of the students are in the same group, so denying read access to just the world won’t stop other students from reading your files. In our system, your group is almost as big as the world.

To change permissions on a file or directory, you use the command chmod. Its first argument is the permissions to be changed, and the second argument is the filename. More arguments are simply used as other files to change permissions on.

The permissions are specified as N+M or N-M, where N is one (or more) of u, g, or o, and which stands for user (the file owner), group, and other (the world). M is one (or more) of r, w, or x, which stand for read, write, and execute, respectively. The + means “add this permission”. The - means “take it away”.

On my file above, I could remove world-read permission by using the command

chmod o-r acm_application.txt

Or I could remove everyone’s read permission but my own by doing

chmod go-r acm_application.txt

I can give back read, and add write, permissions to everyone by doing

chmod go+rw acm_application.txt

For directories, the permissions letters are the same (r,w,x), but mean things a little different. Permission ‘r’ means that you (or group, or other) can read the directory, ‘w’ means you can write files into the directory, and ‘x’ means you can search the directory (i.e., you can do an ls and see the files).

When doing your assignments, you should at least have each assignment file unreadable, unwriteable, and unexecutable by anyone but you. You should probably also have the directory that your class files are in also unreadable, unwriteable, and unsearchable. It is easiest to do this in a subdirectory of your home directory. It’s nice to keep your home directory readable by others in case you want to share some files. For example, you may want to create a subdirectory called cs171 under your home directory, make it unreadable, unwriteable, and unsearchable by everyone but you, and then do all of your class work in that directory.

There is a command called umask that lets you set the default permissions to be used every time you create a file or directory. If you enter umask with no arguments, it will print out your current umask value. umask only takes old-style permissions, which are numbers rather than things like “g+w”. We won’t explain all the numbers, but only offer two suggestions here. The command umask 22 makes all files you create readable by everyone, but not writable. This is the default setup that you will have. The command umask 66 makes all files created by you unreadable and unwriteable by you. I always keep my umask to be 22, and make things unreadable at the directory level (a file readable by others but in an unreadable and unsearchable directory will be unreadable).

If you want to learn about permissions, what the numbers mean, and other related stuff, do man chmod.

Questions and Tasks

  1. Do cp ~jcook/public_html/Classes/CS171/index.html syllabus.html What are the permissions on the file? Who owns it?

  2. Do chmod u-r syllabus.html Then do more syllabus.html What happened?

  3. What would the command be to restore read permission for yourself?

  4. Does anyone else have permission to read the file? If so, how would you remove those permissions? Do it, and write down the permissions the file now has.

Move on to …

The .cshrc File

Your shell, if it is csh or tcsh, reads a special startup file when it first starts up. This file lets you configure your shell to your own environment and how you want it.

This file is .cshrc in your home directory, also known as ~/.cshrc, since ~ is your home. This and other dotfiles don’t show up normally when you just do an ls, but remain hidden, unless you do an ls -a. Try doing ls -a in your home directory – you’ll see many dotfiles.

This file is very important because it lets your shell know where different programs are that you might want to use, such as Netscape, Framemaker, ML, or the Java programming language.

You already have a default .cshrc file that was set up when your account was created, and it should have everything you need for now. But if you want to customize your environment or add more tool access, you’ll have to change your .cshrc file. To understand your setup a bit more, look at this annotated version of the default.

For an alternative style, you can look at a sample .cshrc file here . If you use it (and we don’t recommend that you do yet), you should save it as a text file (not named .cshrc_ _yet), mv your old one to a different name (like .cshrc.bak), and mv this saved one to be .cshrc

Questions and Tasks

  1. Do cd to make sure you are in your home directory. Do ls -a What prints out?

  2. Nothing here!

Move on to …

EMACS and XEMACS

The text editor that you will use on the Unix computers is emacs. It is a very powerful text editor that has many, many, many features. But we will just stick to the basics. All we need to do is to create, edit, and save text files. xemacs is a version of emacs that is probably more user-friendly, so in the beginning you should use xemacs.

From a terminal (command-line) window, you can start emacs by typing xemacs & – the ampersand tells the shell not to wait for this command to complete. After a second or two, a new window should appear on your screen. This is emacs.

As with most editors, emacs has a set of menus at the top, including File, Edit, Window, etc. Notice also that the called Buffer. We’ll get to that in a minute. If you bring down the File menu, you will see the familiar Open, Save, Save as selections. Select Open – what happens? If you are running xemacs, a dialog box opens up to let you select a file (use the middle mouse button to select a file name). If you are running plain emacs, this doesn’t happen, but notice that at the bottom of the window, there is a prompt waiting for you to type in a file name. Once you type in a filename and hit &lt;enter&gt;, the file is opened and appears in the window. This is plain emacs’ idea of a dialog box.

On many editors, you can only be editting one file at a time. You must close the file you currently have open and then open another. In emacs, you can edit many files at the same time. Each file is kept in its own __buffer. __Usually, you can only see one buffer in the emacs window at a time. The Buffer menu will show you all the open buffers, and let you switch between them. You can even split the viewing window between multiple buffers, but I don’t suggest that you try that yet. Just stick with using the menus to switch between buffers, and to open and close files. Sometimes xemacs will split your window even when you don’t ask. To get back to viewing just one file, use the “Un-split” command on the File menu.

Pointing and clicking the left mouse button will allow you to position the cursor in the text. Holding the left button down and dragging will select a region of text. The middle button can then be used to paste this text in, whereever you are pointing to. There is a scrollbar on the right side that you can use to scroll through the file. The arrow keys will do the standard things, as well as page-up and page-down.

Emacs used to be all keyboard-based, so all the commands have keyboard equivalents. For example, opening a file is &lt;Ctrl-X&gt;&lt;Ctrl-F&gt;, saving a file is &lt;Ctrl-X&gt;&lt;Ctrl-S&gt;, etc. Most of the menu selections will list the key equivalents, and if you go to the emacs tutorial, listed on the Help menu, you can learn more than you ever wanted to know about emacs.

Some more information on emacs is here, if you want to look at it (I suggest not for now).

Questions and Tasks

  1. Start xemacs. One way is to point the mouse pointer to the background area on the screen, and hold the left mouse button down. A menu should appear with an item called Applications. If you follow this item to the right, and sub-menu appears with emacs on it. Select that item, and an emacs window should appear. The other way is to simply enter xemacs & in your terminal window. The ampersand tells the command shell not to wait for xemacs to end, but to simply start it up and then immediately accept more commands.

  2. Type a few lines of text in xemacs. Then save it as a file, using the File menu and Save As. Use the file name myfile.txt

  3. Do ls -l in the terminal window. What prints out? Who owns the file myfile.txt? Who can read it? Who can write to it?

Move on to

Printing Files and Web Documents

Printing on the line printer

There is a large and fast line printer for your use in the lab. It prints out text on perforated paper feed. It does not print pictures or formatted files (like Postscript) that printers such as laser printers will.

The department does have laser printers, but since they are very expensive to operate, their use is restricted, and they are not available for your use in these classes. If you think you need to use a laser printer, ask the TA or instructor during the lab period, and if they agree, they will tell you how to use one.

When printing source code or other text files, you can simply do lpr &lt;filename&gt;, and the file will print on the line printer.

Printing Web documents on the line printer is a bit trickier, since Netscape and other Web browsers try to print a formatted document (Netscape produces Postscript).

The way to print Web documents on line printer is to save a Web page in the _text _format, and then print the file using lpr.

To do this in Netscape (when you are viewing the page you want to print), pull down the _File _menu, select Save As, change the format to text, and enter a filename to save it as (this can be anything, because you should just remove it after you print it). Then print the file using lpr &lt;filename&gt;

Questions and Tasks

  1. Print your file myfile.txt Go retrieve it from the printer, write your name on it, and hand it in with your lab answer sheets.

  2. Save this Web page as a text file, using File and Save As. Name it printing.txt Do more printing.txt If it looks like plain text, print it using lpr printing.txt If it looks different, call a lab assistant over.

Move on to …

COG

COG stands for Computer Operations Group. They are the ones who keep all the CS machines running. When there are problems, they solve them for us.

They also install the software that we need, and maintain information on what software is installed on our machines, and how to use it.

This information is available on-line at http://intranet.cs.nmsu.edu/COG. These pages are very valuable, and you should get used to finding things on them.

Notice that you need to enter your username and password when entering these pages. This is because these pages are meant for NMSU CS people only, and can be dangerous to others!

Important pages in the COG Web area are:

Questions and Tasks

None for this page

Move on to

X, startx, and other GUI stuff

On Unix machines, X is the GUI interface, like Windows is on the PCs and the Macintosh Finder is on the Apple Macintoshes.

On some machines, X is already running when you log on. If you logged in using a little window in the middle of a colored or textured screen, then X is already running, and you should have at least one terminal window open already, where you can type commands.

If you logged in on a screen that just looked like an old text-only monitor, and there’s no graphics or windows or anything, then you need to start X yourself. You can do this by running startx, or if the system complains that this isn’t found, try openwin.

In the X GUI system, the left mouse button does the most work, followed by the middle and then the right.

This is the end of the Introduction to Unix Web Pages. If you found any errors, or deficiencies, please send email to me (click here), and I’ll update them.

LABORATORY AND HOMEWORK ASSIGNMENT 1

To introduce a small set of basic Unix and editor commands. These commands will enable the student to do the following:

  1. Login and logout
  2. Perform basic file management operations
  3. Using the on-line manuals
  4. Print files
  5. Use the Emacs editor
  6. Send mail using the Emacs editor

Read through this handout before lab

All questions should be answered on a separate sheet of paper. Keep this handout for future reference.

none

Getting started: login and

logout

Only people who have accounts are able to login to the computer system. A person with an account has the following:

  1. a unique login name (usually first initial followed by most of the surname)
  2. a home directory on one of the file servers on the network
  3. a password to protect our system from intruders who enter through unsecure logins.

The workstation or terminal in front of you should display the prompt: login: If this is not the case press the return key one or more times. If nothing happens, the workstation does not function properly.

Otherwise respond to the prompt with your login name and wait for the password prompt password: Enter your password and note that the computer does not echo the password back to the screen. If either login-name or password is mistyped or entered incorrectly, the system rejects the user and responds with a message and expects a new response to login:

There are three reasons why a login attempt may be unsuccessful:

  1. the user is not properly registered with the Computer Operations Group
  2. the computer on the network which checks login names and passwords for all users of our network is broken down and there is no backup machine available
  3. if the file-server computer which contains the home directory and files of the user is broken down, the login process will not complete and the system will hang.

Try to login now. If login is successful, the operating system of the workstation prints some messages and displays a command-line prompt. Now change your password using the passwd command. Type passwd and enter your current password at the prompt Old password: enter the password that you want to use from now on (until you change it again) at the prompt New password: and again, at the next prompt: Retype new password:

Be sure that your new password is not too simple – you don’t want other people to be able to guess it. Your password has now been changed.

Introduction to Unix

What is Unix? In the narrowest sense, it is the kernel of a time-sharing operating system: a program that controls the resources of a computer and allocates them among its users. In a broader sense, Unix includes not only the kernel, but essential programs like compilers, editors, command languages, programs for copying and printing files and the like. In an even more broad sense, Unix may include programs developed by you or other users to be run on your system, such as tools for document preparation, statistical analysis routines, graphics packages, and telecommunications.

Programs (and users) communicate with the Unix kernel through special commands called operating system service calls. These are not very user-friendly, so Unix users have written several operating system command languages over the years. The interpreters of these languages are programs called shells. When the system prints the prompt and you type commands that get executed, it’s not the kernel talking to you, but a go-between – the shell.

The C-shell (csh) interprets commands in the UC Berkeley command language (strangely reminiscent of C) and translates them to appropriate service calls for the kernel. When you enter a command, the shell breaks the command line into parts that it analyzes. The first part is the command name, the other parts are options or parameters. The shell then decides what to do with the command. There are two possibilities. Some commands are internal to the shell; this means that the shell can interpret the command directly. If the command is not built into the shell – and most are not – the shell must execute the appropriate program, passing along the options and parameters.

Basic Unix File Management Commands

The Unix file system provides a mechanism for storing and organizing programs and data as well as the interface to the computer’s hardware devices. Unix file systems are hierarchical in structure, similar to a family tree. At the top of the file system is the root directory – the structural foundation for the file tree (seems logical that a tree should start with a root, doesn’t it). All other directories and files are built upon the root directory.

The basic file management commands that we will learn are:

  1. ls – list names of all files in the current directory
  2. cd directoryname – changes current directory to named directory (with no arguments changes to home directory)
  3. cp filel file2 – copy filel to file2, overwrite old file2
  4. mv filel file2 – move (rename) filel to file2, overwrite old file2
  5. rm filenames – remove named files, irrevocably
  6. mkdir directoryname – creates named directory as subdirectory of the current directory
  7. rmdir directoryname – removes named directory, irrevocably (it must be empty)
  8. pwd – prints the full pathname of the working directory
  9. quota -v – lists disc usage for working directory
  10. cat filenames – prints contents of all named files
  11. less filenames - allows user to page through contents of named files. Type q to exit the program. Type h to get a summary of commands.

There are two important differences between the commands mv and cp. First, cp creates file2 as a new copy of filel, while mv simply renames filel — thus turning it into file2. Second, after a mv, file2 has the same owner and creation date as the now non-existent filel, while after cp, file2 has a new creation date and owner (the user who executed the cp command).

Note: For many of the questions in this exercise, there is nothing that needs to be turned in. Just practice the commands as instructed.

Question 1.a:

Use the command pwd to see that you are in your home directory. In a pathname, directories are separated by the slash /. The initial slash indicates the root directory.

Question 1.b:

Use the command mkdir to create a subdirectory in your home directory. Name the subdirectory cs167 (type: mkdir cs167). Use the command cd cs167 to move into the directory that you have just created. Check the pathname of your working directory (using pwd) again to verify that you are in subdirectory cs167.

Question 1.c:

Use the ls command to list all files in the current directory (i.e., cs167) — there should be none. Now use the cp command to copy a file from my directory to yours. Type:

cp ~wiebe/Public167/lab1.c lab1.c

The argument ~wiebe/Public167/lab1.c is the name of the file that you are copying. ~wiebe tells the system that the file is in my home directory – everybody’s home directory is designated by ~loginName, where loginName is that user’s login name (e.g., wiebe).

Explain what the rest of the path name ~wiebe/Public167/lab1.c denotes.

Question 1.d:

Use the ls command to list the files in your directory now. To list all of the files use the -a option (ls -a). Note the unusual file name “..” – this stands for the parent directory of the current directory (in this case your home directory) The single “.” stands for the current directory – the one you are in right now.

Question 1.e:

Use cat lab1.c to see the contents of the file. Use less lab1.c to see the contents of the file; while in the program less, type h to see all the commands that you can use in less. Type q to exit less. Write down the third line of lab1.c.

Question 1.f:

Look at your quota and disk usage in the directory cs167 using the command quota -v. The numbers you see are the numbers of disc blocks - typically 512 or 1024 bytes each - of storage for each file. The value for a directory indicates how many blocks are consumed by the files in that directory and its subdirectories, including the directory itself.

Question 1.g:

Rename the filelab1.c toprg.c using the mv command.

Question 1.h:

Delete the fileprg.c using the rm command (type: rm prg.c). Now move back to your home directory by typing cd. You could also use cd .. in this case, because the parent directory is also your home directory. List the files in your home directory using the ls command. Remember that cs167 is a directory, and note how that is indicated. Remove that directory using the rmdir command. You would NOT have been able to remove that directory if it had contained any files.

NOTE: Don’t use the * or ? symbol in file names – they have a special meaning to the Unix operating system (see section 6 of this lab).

File Permissions

Every file has a set of permissions associated with it, which determine who can do what with the file. There are three types of permissions: read, write, and execute. A typical file may have the permission string -rw-r--r-- associated with it.

The first - indicates that it is an ordinary file. If it were a directory, there would be a d there.

The next three characters encode the file owner’s read, write, and execute permissions. rw- means that the owner may read or write, but not execute the file. An executable file would have an x instead of a dash. The next three characters (r--) encode group permissions. Anyone associated with the owner’s group (a special class of users) can read the file, but cannot write or execute it.

The next three (also r--) define the permissions for everyone else; the rest of the users on the system.

To see the permissions that a file has, use the command: ls -l filename in the directory where the file is. The command to change the permissions of a file or directory is

chmod permissions filenames

The permissions are specified by a three digit number. The first digit encodes the permissions for the user, the second digit those for the user’s group, and the third those for everybody else. The modes are specified by adding together a 4 for read, a 2 for write, and 1 for execute. For example, to change a permission so that everyone including the user can execute the file, but only the owner can read and write it, you would use the command: chmod 711 filename

You can also change the permissions for all three (owner, group and user) by using a ‘+’ to add permissions and a ‘-’ to remove permissions followed by the letter of the permission. For example, to change a file’s permission so that anyone can write it, use chmod +w filename This means that now anyone can change or remove your file!

Question 2.a:

Copy the file ~wiebe/cs167/public/lab1.c into your cs167 directory again, as described in Exercise 1.c. Change the permissions of your copy of lab1.c so that nobody including you can write it. Examine the changed permissions, then try to remove lab1.c. What happens? Change the permissions back.

Question 2.b:

Create a directory called temp in your home directory. Make a copy of lab1.c in the temp subdirectory and then change to that directory. At the prompt, enter chmod -w lab1.c

Try to delete lab1.c again. Is the result any different? Change the permissions back.

On-line Manuals

Many commands, such aschmod above, have a number of options. You can use the man command to find out more about a command. At the shell prompt, enterman commandname. You can use man man to find out more about the man command itself.

Question 3.a:

When using the less command, how do you page backward through the file you are looking at?

Question 3.b:

When using the command expr, how can you find the remainder of an integer division?

Printing Files

In principle, printing a file is similar to displaying a file on the screen. They both involve copying data to an output device. To print a file, use the lpr (line printer) command.

The syntax is the following (where `[ ]’ means optional):

lpr [-#num] [-P printername] filename 

The lpr command has many more options than shown above. The -# option allows you to specify how many copies are to be printed, and the -P option allows you to select the printer that will print the document. If you don’t specify the printer name, the file will be printed on your default printer. For example, to print two copies of the file memo on your default printer, you would enter lpr -#2 memo at the prompt.

To format a file for printing, use the pr command. The syntax for pr is

pr [-l pagelength] filename 

Unless you specify otherwise, pr will format and paginate according to certain defaults. It assumes that the length of the page is 66 lines (the number of lines that normally print on an 11-inch piece of paper). Of these lines, the first 5 are used as a header, the last 5 are used as a trailer. Thus, pr will use 56 lines per page for your output.

Normally, you will pipe the output of pr to the lpr program which will then print the file. For instance, to format the file memo and then print it, you would use the command pr memo | lpr The vertical bar | is called a pipe. When you connect two or more programs together with a pipe, you form a pipeline. The pipeline allows the output of the pr program to become the input of the lpr program.

Stopping a printout

The print command lpr puts the printouts to be output in a queue (a queue is a waiting list). If the printer is broken or if you sent a file to the printer by mistake, then queued print jobs can be terminated even while they are printing by the following two steps:

  1. inspect the printer queue with the command lpq [-PprinterName] where the -P option refers to the same printer to which the printout was previously sent with lpr. This command shows the printout status, user name, job number, file name and file size of all printouts queued for this printer. The job number is used by the next step.
  2. execute the command lprm -PprinterName job# where job# is the job number printed out for this printout in step 1. To save paper, it is always wise to preview a printout on the screen by pr theFile before piping it to the printer by pr theFile | lpr -PprinterName

Note: For many of the questions in this exercise, there is nothing that needs to be turned in. Just practice the commands as instructed.

Question 4.a:

Locate the nearest printer. What is its name? Check the queue of that printer.

Question 4.b:

Change directories to your temp directory and type: pr lab1.c

Question 4.c:

Make a printout of the file lab1.c in which the output is formatted using the pr command. Turn it in with your answers to the questions for this lab.

Regular expressions and file name expansion

When the shell analyzes, or parses the command line, any arguments containing metacharacters will be expanded. Metacharacters are special characters the shell uses to expand one filename into many – all that match the specified pattern. Two common metacharacters are ?, which matches any single character, and *, which matches zero or more characters.

When the command line is interpreted, any filename containing one or more of these characters will be expanded into a list of filenames that match the given pattern. Such pattern matching mechanisms have been studied by mathematicians for a long time. Mathematicians have developed specific rules for the handling of metacharacters and the expressions containing them.

They call character strings that are constructed according to these rules regular expressions. Suppose you are working on a book. Logically, you might have a directory called book which would contain the various chapters: ch.1, ch.2, etc.

If you wanted too look at them all, you could use the command less ch.1 ch.2 ch.3 ch.3 (notice that there are no commas between the arguments, only spaces). This works, but would rapidly get tiring.

An easier way is to use one of the metacharacters: less ch.? Because the ? matches any single character, this would print chapters 1 through 9 if they exist. But what if you wanted to look at all the chapters (assuming you had more than nine). You could use: less ch.* because * matches any number of characters.

As another example, you could delete all the files in your working directory by typing rm * at the prompt (you had better be very sure that that is what you wanted to do!) This may also happen accidentally if a blank space is carelessly inserted before, after or around the metacharacter * (see exercise 2 below).

You can explore the effects of shell file name expansion and other shell command line interpretation mechanisms by using the echo command. The echo command writes out (echoes) its arguments to the screen.

Question 8:

What are the differences between the following commands? (Hint: use the echo command to find out how the shell expands the command lines.)

  1. ls junk
  2. ls /
  3. ls *
  4. rm *.c
  5. rm * .c
  6. ls /*/man