Computer Science 12

Brief introduction to Unix files, directories, and permissions

Copyright © 2001, 2002, 2004, 2009 by Dorothy L. Nixon. All rights reserved


CONTENTS OF THIS TUTORIAL

  1. Files, directories, and permissions on a Unix system
  2. Creating directories
  3. Copying files, and more about pathnames


  1. Files, directories, and permissions on a Unix system.
  2. An operating system is a program which runs all the other programs on a computer, manages the computer's memory, etc. The machine you will be using, cs12.cs.qc.cuny.edu, has an operating system called Unix. You will access cs12.cs.qc.cuny.edu from personal computers with the operating system Windows.

    Log on to your cs12.cs.qc.cuny.edu account, as instructed on Joe Svitak's CS 12 site: Unix account, including "How to Construct Your UNIX Account's Username and Initial Password," "Logging on to your Unix account" (via Windows or Macintosh), and (if you're working at home) Installation of the WinSCP program. To access the Unix command prompt, select "Open Terminal" from the "Commands" menu and then log in again as instructed.

    A computer stores information in files on a disk. Because a disk may have millions of files on it, the files must be organized somehow, to make them easier to find. Files are organized into directories, also known as folders. A directory can contain both files and other directories. Each user on a Unix system has a home directory containing the files and directories which belong to that user.

    When you are logged on to cs12.cs.qc.cuny.edu via "Open Terminal," you should see a blank text field next to a prompt which says "Enter command":

    In the text field, type:

    	pwd
    

    an abbreviation for "present working directory," and then press [Enter]. (You should likewise press [Enter] after all the Unix commands you'll be told about in this and the next section.) Note that Unix is case-sensitive, and the above command must be typed in lower case. The computer will respond with something like this:

    	/home/fa11/cs12/username
    

    with your actual user name in place of "username." Your user name is the name of your home directory (not to be confused with the directory whose name is "home"). Your present working directory is now your home directory (i.e. the directory whose name is your user name), which is inside another directory with the name cs12, which in turn is inside a directory named fa11, which in turn is inside a directory named home, which in turn is inside the root directory, the one big directory which contains all the other directories on the cs12.cs.qc.cuny.edu's hard disk. (Note again that the directory named home is not your home directory, whose name is your user name.) When you type "pwd," the computer responds with a pathname, indicating the current directory and all the directories which contain it, up to and including the root (which is indicated by the initial slash, rather than by spelling out the word "root").

    If you make a mistake while typing commands, use the [Backspace] key to erase the most recently typed character.

    To see the files and directories in your present working directory, type:

    	ls
    

    (i.e. lowercase L, followed by lowercase S), an abbreviation for "list." You should see at least the public_html directory.

    Note that the listing is very incomplete. For one thing, it doesn't tell you which of the listed items are files and which are directories. For another, it doesn't list quite all the files. To see a listing of all the files, type:

    	ls -a
    

    where the 'a' stands for "all." You should now see a listing somewhat like the following:

    .   .bash_history  .bash_profile  .emacs    public_html
    ..  .bash_logout   .bashrc        .mozilla  .xemacs
    

    (Don't worry if your listing isn't exactly identical to the above.) The files whose names begin with a dot are called hidden files because you don't see them as part of a normal directory listing. They have been put there automatically to enable you to access software on the computer. They are hidden because, normally, you won't be doing anything with them. However, they are very important files, affecting how your account works. Make sure you don't delete those files! Also, you should never modify them until you've become very proficient with Unix.

    But the above listing still doesn't tell you which of the listed items are files and which are directories. To see that, plus some more information about the files and directories, type:

    	ls -la
    

    where the 'l' (that's lower-case 'L', not number '1') stands for "long," and the 'a' stands for "all." You should now see something like the following, with your own username in place of "dnixon" and some other group in place of "faculty":

    total 80
    drwx--x--x  5 dnixon faculty 4096 2009-11-20 12:54 .
    drwx--x--x 29 root   root    4096 2009-11-17 15:08 ..
    -rw-------  1 dnixon faculty  310 2009-11-20 14:58 .bash_history
    -rw-r--r--  1 dnixon faculty   33 2009-02-23 09:46 .bash_logout
    -rw-r--r--  1 dnixon faculty  176 2009-02-23 09:46 .bash_profile
    -rw-r--r--  1 dnixon faculty  124 2009-02-23 09:46 .bashrc
    -rw-r--r--  1 dnixon faculty  500 2009-02-23 09:46 .emacs
    drwxr-xr-x  4 dnixon faculty 4096 2009-02-23 09:46 .mozilla
    drwx--x--x  3 dnixon faculty 4096 2009-11-20 14:50 public_html
    drwxr-xr-x  2 dnixon faculty 4096 2009-02-23 09:46 .xemacs
    

    The filenames you saw before are now in the rightmost column. To the left of each filename is the date and time when the file was last modified. To the left of the date is the size of the file, in bytes.

    At the extreme left, for each file or directory, is a string of 10 characters which may be 'd', 'r', 'w', 'x', or '-' (hyphen). The leftmost character indicates whether the listed item is a file or a directory, 'd' for "directory" and '-' for "file." The remaining 9 characters are the file permissions, which determine who can access the file or directory, and how. The first three of these 9 characters pertain to the owner of the file or directory (in most cases, the owner is you). The next three characters pertain to the other members of a group of users which includes the owner, and the last three characters pertain to all the other users on the system.

    For each of the three categories of users, the corresponding three characters are as follows: The first character is either 'r' or '-'. If it is 'r', then users in the relevant category can read the file or directory. If it is '-', then they cannot read it. The second character is either 'w' or '-' and determines whether users in the relevant category can write to the file or directory, i.e. modify it. The third character is either 'x' or '-' and determines whether users in the relevant category can execute the file or directory. (If the file is a program, executing it means running the program. In the case of a directory, executing it means accessing files within the directory.)

    Normally, only you should be allowed to write to files in your directory. But it may be necessary to allow other users to read and/or execute at least some of your files and directories, including your home directory. This is necessary if, for example, you have a website. However, you might not want other users to be able to read and execute your homework files, e.g. if you are taking course which has you doing your homework on a Unix system. Later, you will be asked to experiment with creating files and directories that only you can read.

    Look again at your latest file listing. In the column of filenames at the right, note that the first two listed directories are "." and "..". The single dot stands for the directory whose contents are being listed (in this case, your home directory), and the double dot indicates its parent directory, i.e. the directory which contains the current directory.

    The second column from the left, after the file permissions, indicates the number of files contained in the listed item.. For files, this number is 1. For directories, it may be more than 1.

    The third column from the left indicates the owner of the file or directory. In most cases, the owner is you. The parent directory (..) of your home directory is owned by root, i.e. it is owned by the system administrator.

    The fourth column from the left indicates the group of users to which the owner belongs. As of the time this tutorial was written, it appears that nearly all the users are in one big group called users.

    To see a detailed listing of files and directories other than the normally hidden files, type:

    	ls -l
    

    (i.e. ls followed by a space, then a hyphen, and then another lowercase L). Below, you will sometimes be asked to repeat commands you typed earlier. To save yourself the trouble of typing them all over again, you can use the up-arrow key to repeat previously-typed commands. Pressing the up-arrow key once makes the most recent command reappear. Pressing it again makes the second most recent command appear, and so on. Press [Enter] when the command that you want to repeat reappears. Experiment now with the up-arrow key for a while, and then continue.


  3. Creating directories.  Create a new directory by typing:

    	mkdir mystuff
    

    where "mkdir" means "make directory," and "mystuff" is the name of the new directory you are creating. Then, so that other students can't read or copy files from your mystuff directory, type:

    	chmod 700 mystuff
    

    where "chmod" means "change mode," and the exact significance of the number 700 will be explained at some later time. For now, simply note that the number 700, next to a chmod command, means you are setting permissions so that you can read, write, and execute the indicated file or directory, but that no one else (except the system administrator) can access the file or directory at all. To make sure you've set the permissions correctly, type again (or use the up-arrow to repeat):

    	ls -la
    

    (where, as usual, both instances of the character 'l' are lowercase L, not digit 1). For the new mystuff directory, the file permissions should be:

    	drwx------
    

    The permissions for all other files and directories should be the same as they were before.

    Let's now work inside your mystuff directory. Type:

    	cd mystuff
    

    where "cd" means "change directory." Now type (or repeat):

    	pwd
    

    and observe that your present working directory is now your mystuff directory, which is inside your home directory (the directory with your user ID as its name). Now type (or repeat):

    	ls -la
    

    and observe that there aren't yet any files in your mystuff directory. All that's listed, so far, is your current directory (mystuff), indicated by a single dot in the rightmost column, and its parent directory (your home directory), indicated by the double dot. You are the owner of both these directories.

    Now log out and log back in, as instructed in the Tutorial on Telnet, E-mail, and FTP.

    When you log in, the directory you always access first is your home directory, as you can confirm by typing the pwd command again. To get into your mystuff directory again, type:

    	cd mystuff
    

    and then confirm you are there by typing:

    	pwd
    

    Inside your mystuff directory, create another directory

    	mkdir invisible
    

    and then go into it by typing:

    	cd invisible
    

    and then confirm you are there via the pwd command.

    As you change directories, the prompt also changes. On cs12.cs.qc.cuny.edu, the prompt will contain the name of your present working directory, but not the complete pathname. Other Unix systems may be set up to have different prompts.

    If you need to go back to your home directory for any reason, type:

    	cd ~
    

    where the tilde ('~') indicates your home directory. (To type a tilde, hold down the [Shift] key and press the key at the upper left corner of the main alphanumeric portion of your keyboard.) Then, type the following to get back to the imvisible directory from your home directory:

    	cd mystuff
    	cd invisible
    

    Later, if you need to take a break before you finish this tutorial, log out and then, after you log back in, type the above pair of cd commands to bet back to your invisible directory from your home directory.


  4. Copying files, and more about pathnames.
  5. You will now be asked to copy some files from one of Ms. Nixon's directories to one of your own. Make sure your present working directory is your invisible directory (inside your mystuff directory). Then type. at the "[username@ invisible]$" prompt:

       cp ~dnixon/public_html/* .
    

    (Be sure to include the dot ('.') and the end of the command. The dot should be preceded by a space.)

    The tilde ('~') means that what follows it is the name of someone's home directory, in this case Ms. Nixon's. The dnixon directory contains a directory named public_html. The asterisk ('*') indicates ALL of the files in Ms. Nixon's public_html directory. The asterisk is followed by a space, which in turn is followed by a dot, which indicates your present working directory (which should be your own invisible directory). The command tells the Unix operating system to copy all the files from Ms. Nixon's public_html directory to your present working directory (indicated by the single dot).

    Use the ls command, in your invisible directory, to verify that you copied the correct files. They should include:

       index.html
       Tutorial-Unix.html
    

    Note that whenever you use the cp (copy) command, it must always be followed by two pathnames. In the above case, the two pathnames were ~dnixon/public_html/* and the single dot.

    A pathname indicates a file or directory (or a set of files and directories) and how it can be accessed. For example, the pathname ~dnixon/public_html/* indicates all files inside a directory named public_html, which in turn is inside the home directory of user dnixon. This pathname starts at dnixon's home directory. Similarly, the pathname font face="Courier New" size=-1>~someuser/mystuff/invisible/* indicates all files inside a directory named invisible, which in turn is inside a directory named mystuff, which in turn is inside the home directory of user someuser. This pathname starts at someuser's home directory.

    There are two kinds of pathnames: absolute pathnames and relative pathnames.

    An absolute pathname starts at the root directory. For example, when you type the pwd command, the system responds with the absolute pathname of your present working directory, which should now be /home/fa09/xyzqc/mystuff/invisible if you are user xyzqc.  An absolute pathname always begins with a slash ('/') indicating the root directory.

    A relative pathname starts somewhere other than the root directory. For example, ~dnixon/public_html/* starts at dnixon's home directory. Below are some other examples of possible relative pathnames and where they start:

    Pathname What it indicates Where it starts
    ~ Your home directory. Your home directory
    ~/mystuff

    A directory named
    mystuff inside
    your home directory.
    Your home directory.

    ~/mystuff/invisible/myfile.txt





    A file myfile.txt,
    inside directory
    invisible, inside
    directory mystuff,
    inside your home
    directory.
    Your home directory.





    ./myfile.txt


    A file myfile.txt,
    inside your present
    working directory.
    Your present working
    directory.

    myfile.txt



    A file myfile.txt,
    inside your present
    working directory.
    (Same as above.)
    Your present working
    directory.


    *


    All the files inside
    your present working
    directory.
    Your present working
    directory.

    *.html




    All the files, in
    your present working
    directory, which
    have filenames
    ending in .html.
    Your present working
    directory.



    keep/my.txt




    A file named my.txt,
    inside a directory
    named keep, inside
    your present working
    directory.
    Your present working
    directory.



    ..


    The parent directory
    of your present
    working directory.
    The parent directory
    of your present
    working directory.
    ../*.txt





    All the files, in the
    parent directory
    of your present
    working directory,
    with filenames
    ending in .txt.
    The parent directory
    of your present
    working directory.



    ../..




    The parent directory
    of the parent
    directory of your
    present working
    directory.
    The parent directory
    of your present
    working directory.


    ../../public_html/*







    All the files inside
    a directory named
    public_html, inside
    the parent directory
    of the parent
    directory of your
    present working
    directory.
    The parent directory
    of your present
    working directory.





    (The examples in the above table are presented as illustrations only. You do not need to use them right now. But you should make sure you understand them, in preparation for the next quiz.)

    As mentioned earlier, the cp (copy) command must be followed by two pathnames. Of these two pathnames, the first must be the pathname of either a single file or a set of files (using the '*' in the latter case). If you are using the cp command just to copy one or more files without changing their names, then the second pathname must be the pathname of an already-existing directory into which the files will be copied. The cp command can also be used to copy a file and gove the copy a new name, in which case the second pathname should be the pathname of the new file, with the new filename, inside the target directory.


Back to: