Files and Directories

Working With Unix
[Files and Directories |Directory Structure |Pathnames |Listing Directories |Concatenation |Paging |Removing |File and Directory Relationships A UNIX file system is made up of directories and files. The file system is like a tree. Each branch represents a directory and each leaf represents a file. You start with the root directory and have many other directories sprout off of it. Each directory can contain as many files and directories as there is space on the disk for.

Directory Structure

The directory structure in UNIX always starts with the root directory. It ends up looking something like:

Pathnames

Pathnames describe exactly where on a file system a certain file is. A "/" separates directory names. If a file name begins with a "/." The path from the root directory is used. If it does not, the path is from the current directory. A pathname has the directory names separated by slashes. Ending in the directory or filename:

dir/dir/dir/dir/filename
dir/dir/dir/dir/dir

Filenames beginning with a slash, start at the root directory:

/usr/local/bin

A "." represents the current directory, so:

./dir/filename dir/filename

are the same. A ".." represents the previous directory. In the C shell, the "~" represents your home directory. You can also prepend a "~" to a user name for another user's directory.

To find the pathname of the current directory, use the pwd command:

larry% pwd
/home/schallee
larry%

Listing Directories

The ls is the command to list the files in a directory. To get a listing of the files in the current directory:

larry% ls

fun                             mp.and.the.holy.grail.txt
hi
larry%

ls takes many options. You can get a long listing, including file permissions, owner, size, and the date last modified, by adding the -l. To see all filenames beginning with a "." include the "-a" option. The options can be stacked, so if you wanted to get a long listing including all of the "." files you could do both of these:

larry% ls -a -l

total 65
drwxr-xr-x  2 schallee      512 Jul 11 08:56 .
drwxr-xr-x 20 schallee     1536 Jul 11 08:57 ..
-rw-r--r--  1 schallee       22 Jul 11 08:47 fun
-rw-r--r--  1 schallee       23 Jul 11 08:46 hi
-rw-rw-rw-  1 schallee    60531 Jul 11 08:56 mp.and.the.holy.grail.txt
larry% ls -al
total 65
drwxr-xr-x  2 schallee      512 Jul 11 08:56 .
drwxr-xr-x 20 schallee     1536 Jul 11 08:57 ..
-rw-r--r--  1 schallee       22 Jul 11 08:47 fun
-rw-r--r--  1 schallee       23 Jul 11 08:46 hi
-rw-rw-rw-  1 schallee    60531 Jul 11 08:56 mp.and.the.holy.grail.txt
larry%

You can also include a pathname to get a listing of a certain file, or a different directory.

larry% ls /

bin                     mnt                     tmp
dev                     n                       tmp_mnt
etc                     sbin                    usr
home                    security.script         var
kadb                    security_script.log.185 vmunix
lib                     sys
larry% ls -al /bin/ls
-rwxr-xr-x  1 root        13352 Oct 14  1994 /bin/ls
larry%

To get information on all of the different ls options, get the man page on ls.

Concatenation

Eventually, you will want to display a file. One of the ways to do this is with the concatenation command cat. cat takes a list of file names, and prints them to the screen. For example, if you wanted to see what were in the file hi, you could

larry% cat hi

Hi there. How are you?
larry%

Or if you wanted to display both hi and fun, you could include them both on the command line:

larry% cat hi fun

Hi there. How are you?
UNIX is a lot of fun!
larry%

The one problem with cat is that it does not stop when it gets to the end of the screen. If you ran cat on mp.and.the.holy.grail.txt, which is 1417 lines long, it will just scroll by as fast as it can. Most people can't read this fast.

Paging a Text File

The solution to this problem is the more command. It works about the same way, except that it waits for the user to input something before it goes to the next screen full of text.

larry% more mp.and.the.holy.grail.txt

 
"The Script, of The Sound track, of The Movie, of the Python Presentation, of
The Retelling of the Great Classic, of The Myth, of The Legend, of The
Unproven Hearsay, of The See Webster's Dictionary for the Definition of the
Above - Better Known as...
 
                     (Alias) (Wik) *(Da Duh Da Daaaaaa)*
                     MONTY PYTHON AND THE MOST HOLY GRAIL
                           (and we really mean it)
                               (also also wik)
 
-- the strictly unofficial script of the movie, done in a fit of boredom
by a whole lot of people -- touched up again by Ralph the Wonder Llama
(How time flies), and Svend's Moose.
 
Assorted above silliness, commentary, and editing by Timothy 'Tronic a.k.a.
Andrew Mansfield
 
The Cast (in not too much of an order of appearance ):
KING ARTHUR : Graham Chapman
PATSY : Terry Gilliam 
GUARD #1 : Michael Palin
--More--(1%)

The line at the bottom of the screen tells you that there is more, and that you have read 1% of the file. Once you have read the screen, you can type a space to get the next screen full. Or you could hit the "Return" key, and get just the next line. If you are really bored already, and hate this movie, type the "q" key to quit and go back to the prompt.

More also has it's own online help. If you type a "?", more displays a list of all it's commands, and then waits for your input again:

Most commands optionally preceded by integer argument k.  Defaults in brackets.
Star (*) indicates argument becomes new default.
-------------------------------------------------------------------------------
<space>                 Display next k lines of text [current screen size]
z                       Display next k lines of text [current screen size]*
<return>                Display next k lines of text [1]*
d or ctrl-D             Scroll k lines [current scroll size, initially 11]*
q or Q or <interrupt>	Exit from more
s                       Skip forward k lines of text [1]
f                       Skip forward k screenfuls of text [1]
b or ctrl-B             Skip backwards k screenfuls of text [1]
'                       Go to place where previous search started
=                       Display current line number
/<regular expression>	Search for kth occurrence of regular expression [1]
n                       Search for kth occurrence of last r.e [1]
!<cmd> or :!<cmd>	Execute <cmd> in a subshell
v                       Start up /usr/ucb/vi at current line
ctrl-L                  Redraw screen
:n                      Go to kth next file [1]
:p                      Go to kth previous file [1]
:f                      Display current file name and line number
.                       Repeat previous command
-------------------------------------------------------------------------------
--More--(1%)

You can get more information on these by reading the man Removing Files If you keep creating files, eventually your run out of disk space, and have to delete some files. To remove a file, use the rm command. It removes the files that are on the command line:

larry% ls
fun                             mp.and.the.holy.grail.txt
hi
larry% rm fun
larry% ls
hi                              mp.and.the.holy.grail.txt
larry% 

Note:Unlike DOS, there is no undelete in UNIX. Once you delete a file, it is realisticly gone forever!

Exercises

  1. Find the pathname of the current directory:

    larry% pwd
    
  2. Get a regular listing of the current directory:

    larry% ls
    
  3. Get a long listing of the current directory, including "." files:

    larry% ls -al
    
  4. Display a file with the cat command:

    larry% cat fun
    
  5. Start to more a long file:

    larry% more mp.and.the.holy.grail.txt
    
  6. Get the help screen from more by typing "?".

  7. Get the next screen full from more by typing a space.

  8. Quit out of more by typing "q".

  9. Remove a file by using the rm command:

    larry% rm fun
    

[Back |Contents |Next ]

Edward B. Schaller (schallee@earthlink.net)