Table of Contents Previous Chapter

CHAPTER 3 Working With Files


Computer files can be thought of as folders inside a file cabinet. Each file is labeled with a name. Files are quite important allowing information storage for use at a later time. After you have finished your computer session, the computer forgets what you have been doing, unless your work is saved in a file.

UNIX files are organized in directories, similar to the file folders discussed above. Directories are files, but instead of containing data or text as other files do, the directory contains information about other files. Directories are organized like inverted trees. The root directory named / is at the top. Branching out from the root directory are the rest of the directories. Any directory can contain both directories and other files.

All directories together are referred to as a file system. The SLAC UNIX file system is a single file system in one sense, that is, all directories originate directly and indirectly from a single root directory at the top. However, in another sense, we have two file systems: the Network File System (NFS) and the Andrew File System (AFS) that share that common root directory as shown in Figure 2. SCS started with NFS support and began moving toward AFS because it offers significant advantages over NFS.

The most notable difference between NFS and AFS is how file access permissions (that is, who is allowed to read, write, execute, etc. a given file) are implemented. The discussion "Handling File Security: File Access Permissions" on page 25 assumes NFS, not AFS. How file access permissions work in AFS is described in the document SLAC AFS Users' Guide. Hard copies are available at the SCS Help Desk. Be aware that even if your home directory is in NFS, you may still need to become familiar with AFS because most collaboration data space is in AFS and SLAC's production WWW space is also in AFS.


Figure 2. Sample Directory Structure

The Root Directory

The / (slash) at the top of Figure 2 is the root directory. Within this root directory the following files are shown: afs, bin, etc, nfs, quark. These files just happen to be directories, which contain their own files. These directories-within-a-directory are called subdirectories. Thus, afs and nfs are subdirectories of /. Every file on the system is contained in the root directory or in one of its subdirectories.

Absolute Pathnames

To identify a certain file in the file system, you can specify an absolute pathname by telling UNIX where the file is in relation to the root directory. For example, to refer to the file sum.f above, use


The first / signifies the root directory. Each subsequent directory name is separated by an additional /.

Your Current Working Directory

Whenever you are using a UNIX system, you are in a specific directory. That directory is called the current working directory. For example, if you were in the /afs directory, your working directory would be /afs. If you were in the /quark/jeff directory, your working directory would be /quark/jeff. Commands that operate on directories use the working directory, unless specified otherwise.

Relative Pathnames

Besides identifying a file by its absolute pathname, you can use a relative pathname. That is, you can tell UNIX where the file is in relation to the working directory. Suppose the working directory is /quark, as shown in the example above. To refer to the file sum.f, you could specify its relative pathname mary/george/source/sum.f. This tells the computer to look in the working directory for a subdirectory named mary. Having found the mary subdirectory, the computer looks for the george subdirectory. In the source subdirectory, the computer should look for the file sum.f. Notice that there is no / preceding mary. If a / preceded mary, the computer would have looked in the root directory for a subdirectory named mary. Relative pathnames never begin with / because their origin is the working directory, not the root directory.

Your Home Directory

When you first login to a UNIX system, you are in the directory that holds your personal files, known as your home directory. Your home directory takes the form /u/gg/username or more simply ~username, where gg is your group code and username is your UNIX userid. Because SCS is currently supporting both NFS and AFS file systems, some users have their home directory in AFS; others still have their home directories in NFS. If you are not sure whether your home directory is in NFS or AFS, enter the command: ypmatch username homes Substitute your UNIX username for username. For example, user ilse's home directory is in AFS:

ilse@odysseus $ ypmatch ilse homes


but user billie's home directory is in NFS:

ilse@odysseus $ ypmatch billie homes


Periods as Shorthand

When you want to use the working directory as an argument for a command you can use the period (.) as a shorthand notation. Suppose the present working directory is /quark/paul; you could refer to it with . as an argument for the copy command cp:

cp /quark/mary/george/source/sum.f .  
In this case, the file /quark/mary/george/source/sum.f would be copied into the working directory /quark/paul. See "Copying Files" on page 21 for more information about the cp command.

The shorthand term for the directory one level above the working directory is two consecutive periods (..). For example, if your present working directory is /quark/paul, .. would refer to the directory /quark. You could use .. as an argument for the change directory command:

cd ..  

Pressing RETURN

The previous sample sessions reminded you to press RETURN after typing a command and its arguments, but the sample sessions in the rest of this document assume that you know to press RETURN to enter a command.

Symbolic Links and the sl (Show Symbolic Links) Command

You will undoubtedly come across instances of symbolic links, which are basically pointers in one location of the file system to the actual location of a file in another part of the file system. For example, in my home directory (/afs/slac/u/sf/ilse) I have the symbolic link afs-slac that points to /afs/ From there I can quickly get to the SLAC WorldWideWeb (WWW) production space when I want to work on WWW files.

Given a symbolic link, if you want to know the absolute pathname of the file, use the sl (show chain of symbolic links) command.

Example 1:

ilse@odysseus $ sl afs-slac


afs-slac -> /afs/slac

/afs/slac ->

Symbolic links to absolute pathnames (absolute pathnames start with a slash (/); relative pathnames don't) start over at the left margin. Symbolic links to relative pathnames are aligned vertically with the path element they replace. So the easiest way to read this sl output is to substitute in the last line for the path element slac directly above it in the last and only absolute path /afs/slac yielding an absolute pathname of

/afs/ .

Example 2:

ilse@odysseus $ sl /usr/local/bin/environ


/usr/local/bin -> -> /a/juno/usr/local/

/a/juno/usr/local/ -> -> ../bin.shared/environ.main


To decode the absolute pathname in this example, substitute the last line,bin.shared/environ.main,

for the path element in the last absolute path /a/juno/usr/local/, so that it reads:


Handling Directories

This section covers identifying the working directory; finding out what files and subdirectories are contained in a directory; changing directories; creating directories; and removing directories.

Identifying the Working Directory

Again, the directory you are presently using is called the (current) working directory. Most commands that operate on directories use the working directory, unless you specify otherwise. Therefore, it is important to know what the working directory is; to find out, use the print working directory (pwd) command.

cd /nfs/juno/work  Changes the current working directory to /nfs/juno/work                
pwd                Prints the working directory, that is, lists the name of the working   
                   directory  on the screen.                                              
/a/juno/work       The pwd command almost never shows the pathname exactly as             
                   you would use it on the cd command.  In this case                      
                   /nfs/juno/work is a symbolic link to /a/juno/work (see section         
                   "Symbolic Links and the sl (Show Symbolic Links) Command" on           
                   page 18).                                                              
Note: When this guide shows you a pathname that is different from the version shown by the pwd command, please use the name shown in this guide.
WARNING!! Never use a pathname that starts with "/a/" because "/a/" is temporary and may disappear in five minutes. Use /nfs/ in place of /a/ at the start of the absolute pathname.

Finding Out What Is in a Directory

List Directory Command
The ls command is used for listing the files and subdirectories contained in a directory. As shown in Figure 3, different information displays depending on what arguments you use.

Note: The commands shown in this chapter have additional options. For more information, give the man command (see section section "More About the Manual" on page 10).
Dot Files
Within your home directory are some files whose names begin with a dot (or period). Most users do not need to access dot files often. They do not show up on the list supplied by the computer when you type the ls command (assuming you do not use the option -a). Because they are not listed on the screen of your terminal, you can find other files more easily. Common dot files are listed in Table 3 below.

TABLE 3. Common Dot Files 
.cshrc    The system runs the commands in this file each time you start the C        
          shell. This varies, though, depending on the shell you use. Usually a      
          good place for alias commands. (See section 4.2.)                          
.forward  Contains address(es) where mail for this account should     be forwarded   
          (e.g., or simply ilse@slacvx).               
.login    The system runs these commands when you login. (See section 4.1.)          
          This varies, though, depending on the shell you use.                       
.logout   The system runs these commands when you logout.This varies,                
          though, depending on the shell you use.                                    
.plan     Information you leave in this file displays when your username is fin      
          gered. (See section 8.2.)                                                  
.project  Information you leave in this file displays when your     username is fin  
          gered. (See section 8.2.)                                                  
ls (which is a SLAC alias for ls -F) lists the names of files or subdirectories in the working directory. UNIX alphabetically orders files and directories with initial uppercase letters first, then with initial lowercase letters.

Library/  Mailboxes/  memos/  
ls -a lists all files, including dot files. The character / after a name indicates a directory; * after a name indicates it is an executable program.

./      .cshrc   .newsrc   Mailboxes/  
../     .kshrc   .profile  memos/      
.NeXT/  .login   Library/              
ls /usr lists the names of files or subdirectories in a directory other than the current one, here, /usr.

adm/   etc/          local/     prman/  standalone/  
apps/  filesystems/  man/       pub/    template/    
bin/   games@        netware/   share@  tmp/         
cad/   include/      nfs/       shlib/  ucb/         
dict/  lib/          preserve/  spool/               
For more information about each file, use the -l option. ls -l lists each file or directory in the working directory, along with its permissions (security information), creator, size in bytes, and date of last modification.

$ ls -l
total 12

drwxr-xr-x 9 ilse 2048 Aug 10 1994 Library/

drwxr-xr-x 2 ilse 2048 Apr 17 1995 Mail/

drwxr-xr-x 42 ilse 4096 Oct 20 1995 Mailboxes/

-rw-r--r-- 1 ilse 885 Apr 27 1994 datasave.opt

Figure 3. ls Command

Changing Directory

$ pwd                 Prints working directory (i.e., lists the name of the working     
/a/juno/u8/sf/cathie  directory on the screen).                                         
$ cd ..               Changes to the directory one level above.  A double period is a   
                      special name for the directory one level above.                   

Creating Directories

For organization purposes, you may want to create a new subdirectory to hold a group of related files. The mkdir command is used to make a new directory. Once the new directory is created, you can either create files in that directory or move existing files into it. For details on moving files into a directory, see section "Copying Files" on page 21.

 $  mkdir test  Directory name is test. You can specify the      
                directory by its absolute or relative pathname.  

Removing Directories

If you need to remove a directory, make sure it is empty. You can use rmdir to remove a directory only if the directory contains no files. To remove ordinary files, see "Removing Files Permanently" on page 22.

$ rmdir test  In this example, the directory to remove is test.  You can specify the direc  
              tory by its absolute or relative pathname.                                    

Handling Files

This section covers copying files; moving files or changing their names; creating files; and removing files permanently.

Copying Files

The cp command copies files. The first argument is the source file (i.e., the file you are copying). The second argument is either the name you want for the new copied file or the destination directory. If a directory is given as the second argument, the file is copied into that directory with the same filename. (For a discussion of arguments, see "Beginning UNIX" on page 5.) Groups of files can be copied by using special characters (see "Referring to Groups of Files" on page 36).

After the source file is copied, it remains intact in the directory where it was located.

 $ cp sum.f sample.bak       Copy the file sum.f.  The name of the new copied     
                             file is sample.bak.                                  
  $ cp /quark/paul/.cshrc .  Copy the .cshrc file to the working directory. The   
                             second period indicates the working directory.       
WARNING!! When you use the cp command without the -i option, the computer does not tell you whether a file already has the name you chose for the destination file. If that file name already exists, the computer removes the existing file before it copies the file you specified.

Moving and Renaming Files

The mv command is used to move a file from one directory to another or to rename a file. The first argument is the source file (i.e., the file you are moving or renaming). The second argument can be the destination directory (the directory where the file should be moved) or the new name of the file (use the absolute or relative pathname). If a directory is given as the second argument, the file is moved into that directory with the same filename. (For a discussion of arguments, see "Beginning UNIX" on page 5.)

Unlike cp, if you move a file or change its name, the source file is deleted from its directory after the procedure is finished.

$    mv sum.f                                                          
     Gives the file sum.f the new name (i.e., renames the file).       
$  mv /usr/smith/cases/test.bak                                        
      Moves the file to the file test.bak in the subdirectory cases.   

Creating Files

When you use a program, such as a text editor, you create a file to save your work. That way, the file can be opened later to allow for editing, printing, or adding to its contents. (See "Editors" on page 39 for information on creating a file during text editing.) There are other ways of creating files. Programs sometimes generate files, and a file may be created to store the output of a command.

Removing Files Permanently

WARNING!! You cannot recover a file that has been removed by this method!
The command rm is used to remove files for good. To protect against unintentional destruction of a file, you can use the option -i with the rm command. The computer will ask whether you are sure you want to remove this file before proceeding. Use the absolute or relative pathname of the file you want to remove.

$ rm            Removes the file permanently.                      
$ rm -i         Remove the files; requests confirmation.           
rm: remove y   Type y for yes or n for no.                                  
$ rm -i test*             A more instructive example of the use of the -i option is    
                          this case, where multiple files begin with `test', some of   
                          which you want to remove and some of which you don't.        
rm: remove y                                                                
rm: remove n                                                               

Browsing Through Files

There are three common ways to browse through a file: looking at a file a screenful at a time, looking at the beginning or end of a file, and using an editor (see "Editors" on page 39.)

Looking at Files One Screenful at a Time

You can use the more command to display a screenful of a file at a time. To display the next screenful, press the space bar. The more command scrolls the text as it displays the next screenful.

$ more memo               Scrolls the contents of the file memo, screen by screen. Press the space      
                          bar to display the next screenful.  To return to the prompt, type q or hold   
                          down the CTRL key and type c.                                                 
<contents of memo>                                                                                

Looking at the Beginning or End of a File

Sometimes you want to see just a few lines at the top or bottom of a file to check on its contents. The head command displays the first ten lines of a particular file, and the tail command displays the last ten lines of a file. Both allow you to specify the exact number of lines to display with the -n option (where n is the number of lines to display) (see Figure 4).

Searching and Comparing Files

Searching for Text

Use the grep command to search for text strings or consecutive words in a file. You supply the string to search for and the file(s) to search in. If the string you are looking for contains spaces, you must put the string in single or double quotes. With the grep command, any lines in the file containing the string display on the screen.

$ grep Computing test.doc                                                                                  Searches test.doc for text string Computing.  
to carry out the SSCL mission.  This plan introduces the  Computing and The Computing and Communications                                                 
Organization within Computing and Communications to concentrate efforts in these areas                                                                   

Comparing Files

The diff and comm commands allow you to compare two files to see whether they are identical. The diff command displays the actual lines in each file that differ. The comm command compares two alphabetically sorted files and produces a 3-column output: column1 shows lines only in file1; column 2 those lines only in file2; and column 3 lines in both files.

$ diff rose1 rose2              Displays differences between files.                          
< Violets are blue           Line in rose1 differs from rose2.                            
> Violets are green          Line in rose2 differs from rose1.                            
$ ilse@odysseus $ comm sg1 sg2  Comparison results of  files sg1 and sg2 are shown in        
           abh                  three columns. The columns are offset by tabs. Column1       
                bobcook         shows lines only in sg1 ; column2 only those lines in sg2;   
                esr             column3 lines in both files.                                 
$ head /usr/local/doc/policies/password    Displays the first 10 lines of the file  

This is file /usr/local/doc/policies/password.

This article on choosing good passwords is by Lionel Cons of group CN/SW at CERN. It appeared in CERN Computer Newsletter 210. Mr. Cons kindly allowed us to adapt it for SLAC users.



A good password:

$ head -5 /usr/local/doc/policies/password  Displays the first 5 lines of the file   
This is file /usr/local/doc/policies/password.

This article on choosing good passwords is by Lionel Cons of group CN/SW at CERN. It appeared in CERN Computer Newsletter 210. Mr. Cons kindly allowed us to adapt it for SLAC users.

$ tail /usr/local/doc/policies/password   Displays the last 10 lines of the file  

together with a punctuation character between them (or a digit if you can only

use alphanumeric characters). For example: 'dog+F18' or 'comP7UTer'. Note that

'dog', 'F18' or 'computer' are in dictionaries but as the passwords use

punctuation or digits, mixed-case character, they are really hard to guess.

Remark: if you use mixed-case characters, do not use the following methods:

- all lowercase or all uppercase

- only the first or the last character in uppercase

- only vowels in uppercase

- only consonants in uppercase.

$ tail -5 /usr/local/doc/policies/password    Displays the last 5 lines of the file   
Remark: if you use mixed-case characters, do not use the following methods:

- all lowercase or all uppercase

- only the first or the last character in uppercase

- only vowels in uppercase

- only consonants in uppercase.

Figure 4. Head and Tail Commands

Handling File Security: File Access Permissions

The following discussion applies to files in the NFS system. While file access permissions in the AFS file system work differently, AFS does not ignore the nine UNIX bits described below entirely. Therefore, even if you do not have to deal with files in the NFS file system, it is still a good idea to become familiar with the way file access permissions work under NFS.

UNIX Security

UNIX allows you to protect your work as you see fit. You decide what you can do to it, what the group you belong to can do to it, and what others can do to it. With the UNIX security system, you can determine who can see, alter, and use your files and directories.

About Security

In the UNIX environment, those who may access a particular file or directory are divided into three classes as shown in the following chart.

Class of User  Definition                                                                     
user           The user who owns the file/directory.                                          
group          The group of users who share access of a file. (Every user belongs to one or   
               more groups. To see what groups you are in, type groups at the prompt.)        
               Each file and directory "belongs to" one user and one group.                   
others         All other users.                                                               
Each file or directory has a set of permissions that control access to it. For each class of users above, there are three permissions that may be granted to that class for each file or directory. These are shown in the following chart.

Permission    File                               Directory                                               
   read (r)   Allows class to read the file.     Allows class to list the names of files and subdi       
                                                 rectories in the directory.                             
write (w)     Allows class to  modify a file     Allows class to create and remove files and subdi       
              (e.g., to append to it or to trun  rectories from the directory.                           
              cate it).                                                                                  
 execute (x)  Allows class to execute a file.    Allows class to access files in the directory if they   
                                                 know their names.                                       
When you create a file or directory, it is assigned a default set of permissions, which you can change. With ls -lg, you can see the permissions associated with a particular file or directory as the first 10 characters on each line. (Be sure to use the correct absolute or relative pathname for the file or directory.)

$ ls -lg                                                        
drwxr-xr-x  3  ilse  sf  1536 Sep 24 10:15 Telecom/             
-rw-r--r--  1  ilse  sf  830 Aug 23 12:39 Unix-guide-questions  
-rw-rw-r--  1  ilse  sf  353280 Sep 27 10:34 Unix-guide-        
-rw-rw-rw-  1  ilse  sf  353280 Sep 29 11:00 Unix-guide-        
The -g option shows the group to which the file or directory belongs; in this example, the group is sf. Note, however, that ls -lg means show the group on a Sun, but ls -l means show the group on an RS/6000.

The first character indicates whether this file is a directory (d) or just a regular file (--). The next three groups, consisting of three characters each (nine characters total), are the permissions granted for the user, group, and others classes, respectively. A permission is granted if the letter is present and not granted if the (--) is there instead. Thus, in the example above, the file Unix-guide-questions is a regular file (--) that allows the user reading and writing privileges (rw- ), but limits the group and others to only reading the file (r- -).

Making Security Changes

The chmod command allows you to change the permissions associated with a file or directory that you own.

$ ls -lg

total 12

-rw-r--r-- 1 bobcook sf 2847 Sep 6 11:42 groupfile

drwxr-xr-x 2 bobcook sf 32 Sep 6 11:41 privatedir/

-rw-r--r-- 1 bobcook sf 664 Sep 6 11:42 prog

$ chmod a+x prog

$ chmod g+w groupfile

$ chmod go-xr privatedir

$ ls -lg

total 12

-rw-rw-r-- 1 bobcook sf 2847 Sep 6 11:42 groupfile

drwx------ 2 bobcook sf 32 Sep 6 11:41 privatedir/

-rwxr-xr-x 1 bobcook sf 664 Sep 6 11:42 prog*


Backing Up and Retrieving Files

Backup and retrieval of NFS files and AFS file are handled differently. See the UNIX Backup page at URL http://www.slac.stanford/comp/unix/unix-bkup/unixbkp.htm for details.

Table of Contents Next Chapter