More Files and Directories in Unix


All files and directories in Unix are organized in a tree-like structure similar to the one shown below. The directory at the top of the tree (labelled "/") is called the root directory.

All files in a Unix system can be reached by specifying a path (similar to a relative pathname) starting from the root directory. For example, a file motd in the directory etc in the root directory can be accessed using the name /etc/motd. Pathnames of this form (i.e., starting with the "/") are called absolute pathnames.

The pwd command

The pwd command shows you the absolute pathname of the current working directory.
 
  • Enter the command: pwd 

  •  

     

    pwd is a command that prints the absolute pathname of your current working directory. Since you were in your home directory when you typed the command, the output that you see is the absolute pathname of your home directory. Note that the first character in an absolute pathname is a "/". The "/" tells the system to start from the root directory. In contrast, a relative pathname does not start with the "/" character, and hence the system uses the current working directory as the starting point for the pathname.

    Almost all Unix commands that accept file or directory names will work with absolute and relative pathnames. For instance, you could change directly to the directory Testdir2 from your home directory. Can you guess the command to do this?
     
  • Go ahead and enter the command in the xterm now. 
  • Enter the command: pwd 

  •  

     

    Compare the output to what you got earlier when you typed the command from your home directory. 

    Going to your home directory

    No matter which directory you are in, you can get back to your home directory by just typing cd by itself.
     
  • Enter the command: cd 
  • The rmdir command

    The command to remove directories is rmdir. A directory must be empty before it can be removed.
     
  • Enter the command: rmdir Testdir1 

  •  

     

    Note that the commands fails because the directory is not empty. Can you come up with the command to remove the directory Testdir2 without changing your working directory? 

  • Enter the command in the xterm. Check the contents of the directory Testdir1, to verify that the directory Testdir2 has indeed been removed. 
  • The cp command

    Most of the commands that we have seen so far work with directories. Let us learn some commands that manipulate files. You'd normally create files using an editor like XEmacs. Instead of creating a new file, let us copy an existing file.
     
  • Enter the command: cp /etc/motd Testdir1 

  •  

     

    cp is the Unix command for copying files. In the command above, you tell the system to copy the file motd in the directory /etc (note that this is an absolute pathname), to the directory Testdir1 (this is a relative pathname). cp leaves the original file intact. If you list the contents of the directory Testdir1, you'll find that it now has the file motd

    You could also use the cp command to make a copy of a file within the same directory. Let us make a backup copy of the file motd that we just copied. We'll call the new copy motd.backup

  • Enter the command: cp Testdir1/motd Testdir1/motd.backup 

  •  

     

    The files motd and motd.backup in the directory Testdir1 are now replicas of each other. 

    The more command

    You can look at the contents of a file using the more command. more displays the contents of a file one screen at a time.
     
  • Enter the command: more Testdir1/motd 

  •  

     

    The file /etc/motd actually serves a very useful purpose on Unix systems. Unix displays the contents of this file each time you login to the system. It is used by systems administrators to keep users informed of important events.  (In fact, motd stands for "message of the day".)

    The file motd is probably so small that it fits on one screen. If a file is more than one screen in length, more displays the file one screen at a time. It allows you to move to the next screen by pressing the space key. You can press 'q' to quit at any time. 

    To understand how more behaves with a large file, try using it to look at the file /etc/magic

    The rm command

    You can delete files using the rm command. Let us delete the file motd.backup that we created earlier.
     
  • Enter the command: rm Testdir1/motd.backup. If you get a prompt, type 'y' to confirm that you want the file removed. 
  • The mv command

    You can move files from one directory to another using the mv command. Let us move the file motd to the home directory.
     
  • Enter the command: mv Testdir1/motd . 

  •  

     

    Note that the directory Testdir1 no longer contains the file motd. The mv command can also be used to rename files. Let us change the name of the file motd to motd.new

  • Enter the command: mv motd motd.new 

  •  

     

    If a file by the name motd.new already exists, it is overwritten. Let us now delete the file motd.new

    Enter the command: rm motd.new



     
    Previous Table of Contents Continue


    Last modified: Tue Jan 6 20:29:31 EST 1998