Sequoia Hall
Home Academics Research Seminars Consulting Industrial Affiliates People
    
Computing Guide
Our Brochure
Contact Us
Sitemap
Links



8. Basic Unix Commands

Previous Computing Guide Contents Next

This section gives you a brief introduction to some commonly used Unix commands. For the sake of example, I will assume that your userid is joe.

Note that Unix is case-sensitive: cd is not the same as CD. When you log in, the operating system will deposit you in what is called your home directory such as /home/joe. The easiest way to visualize your home directory is to regard it as the start of a branch of a large tree. If you create a directory, you are creating more branches; if you create a file, you are creating more leaves (in the sense that these cannot be further extended).

If you are using the windowing system on our terminals, you can get a visual look of all your files by choosing Desktop -> Home Directory from the menus. Then you can drag and drop files or toss them in the dumpster to get rid of them, etc. In addition, if you use Emacs or XEmacs you can get do fancy things in the Dired mode.

Sometimes, however, it is useful to be able to do these things from the shell prompt. Here are some basic commands:

cd

Change directory. Just typing cd from anywhere will get you back to your home directory.

A tip. If you are currently in /home/joe/foo/bar and wish to go to the directory /home/joe/bar/foo you only need type cd ~/bar/foo to get there. To get to the home directory of user naras, you can type cd ~naras.

Another tip. If you are in a directory cd .. will take you up one level and cd . will take you to the current directory itself, that is, you'll stay put. So .. and . can be used to refer to the parent and current directories respectively.

ls

List directory. There are many options to this command and files can be listed in many formats. See the man page for this command; it is a must-read.

rm

Remove file. Note that rm -r foo (recursively remove) will actually remove the directory (or file) foo and everything beneath it if it is a directory. A bit dangerous to use in my opinion.

more

Show the contents of a file to screen. less is also another way of doing it, and maybe, even better.

These should get you started. For comprehensive information, please look at The Unix Help Page. This is a set of help files developed at the University of Edinburgh.

8.1 Controlling Access to Files
The following describes how to control access to files residing on the department machines. The Leland machines use a different method; see the introduction to AFS for more information.

Initially when userid joe logs in, and types ls -l followed by the ENTER key in the home directory, the following output will be shown.

rgmiller 1% ls -l
total 3
drwx------ 2 joe joe 512 Sep 5 19:40 private/
drwxr-xr-x 2 joe joe 512 Sep 5 19:40 public/
drwxr-xr-x 2 joe joe 512 Sep 5 19:40 public_html/

The output above shows three directories owned by joe with various permissions (r for read, w for write, and x for execute). We know they are directories because they are flagged by a d in the first column. There are no files now, but if joe did have them, they'd also have shown up too, without a d in the first column.

Take a moment to look at the groups of letters rwx. There are three sets of them. Each of the letters represents a binary digit 0 (representing off) or 1 (representing on). If a bit is on, the corresponding letter is shown, otherwise a - is shown. The three sets of three bits each apply to three different types of users.

The first group of three bits (after the d) applies to joe himself. In the above listing joe can read, write and and execute all directories. (Executing a directory means being able to get inside it.)

The second group of three bits (after drwx) applies to those who belong to the same group as joe. In our department, whenever a userid is created, a group having the same name as the userid is also created. Thus, there is a userid joe and also a group id joe. Initially, only userid joe belongs to the group joe, but joe could ask the superuser to add other userid's to his group to share his files.

The last set of three bits apply to everyone else---those who are not in the group joe. Note that they have no access to the directory private but read and execute access to the other two directories.

So how would one go about changing these bits? The command chmod is used for this purpose. In the examples that follow, we use a binary representation for the bits rwx, leading to a value between 0 (binary 000) and 7 (binary 111) for each group of of three.

  • To make a file or directory foo readable, writeable, executable only by me I use the command chmod 700 foo
  • To make a file or directory foo readable, writeable, executable by me as well as those in my group, I use chmod 770 foo.
  • To make a file readonly by everyone chmod 444.
  • To make a file readable and executable by everybody, but writeable only by me, chmod 755. By the way, these are the normal permissions of any program or directory under our default settings, unless you have changed a variable called umask that changes these defaults. The default permissions for ordinary files (everything else besides a program or directory) is 644.

Please see the man page for chmod, chown, chgrp and newgrp.

Frequently Asked Question
Userid joe has a directory foo with a file called junk in it with permissions shown below.

rgmiller 1% cd foo; ls -l
total 3
drwxrwx---    2 joe    joe        512 Sep  8 15:40 ./
drwxr-xr-x    7 joe    joe       1024 Sep  8 15:39 ../
-rw-r--r--    1 joe    joe         10 Sep  8 15:40 junk

The permissions of foo read drwxrwx--- (second line of output) which means that those in the group joe can write to this directory. Userid jen belongs to group joe. But when she tries to edit the file junk she cannot seem to do it. Why?

Note that joe has give write permission only on the directory foo and not on the file junk. So unless the permissions of junk read -rw-rw-r-- userid jen cannot change the file. Therefore, everytime user joe creates a new file foo and wishes everyone in his group to be able to manipulate it, he should proceed as follows.

rgmiller 1% chmod 664 foo

Recall that this is different from the default 644 permissions for an ordinary file.

Also note that if you use Emacs to edit the file, then the permissions on the file can change after editing. This is because of the way Emacs makes backups before editing. So check permissions after editing and alter them if neccessary.


Previous Computing Guide Contents Next


Contact  | Sitemap  | Directories  | Maps  & Directions  | Giving to Stanford
Copyright 2004Stanford University. All Rights Reserved. Stanford, CA 94305, (650) 723-2300
Terms of Use Copyright Complaints