Introduction to UNIX
Filesystem Exercises


These exercises will familiarize you with the basic UNIX commands for working with files and filesystems.

Setup

  1. Open a new window or use an existing open window. To open a new window, press and hold the right mouse button. A "pop up" menu will appear. Still holding the right mouse button, drag the pointer to the selection which says "New Window". When it is selected (the item will appear highlighted or raised), release the right button. A new window will appear in a second or two.

  2. Change to this exercise's subdirectory by using the command:
     
         cd ~/Filesystem 
         

  3. Begin the Filesystem exercises with the ls exercise below.

ls

  1. Read/scan the man page for ls with the command:
    
         man ls 
         

  2. Use ls without any arguments to display this directory's contents. How many files do you see?

  3. Now use ls with the -a option. How many files do you see this time? Notice that the "new" files all begin with a "dot", which indicates they are "hidden" files.
    
         ls -a
         

  4. This command is useful for distinguishing between directories, ordinary files, and executable files. Notice how its output differs from ls without arguments.
    
         ls -F
         

  5. Use the command ls -l to obtain a "long" listing of your files. Sample output from this command and an explanation of the information it provides appears below.
    
    -rwxr-xr-x   1 jsmith   staff         43 Mar 23 18:14 prog1
    -rw-r--r--   1 jsmith   staff      10030 Mar 22 20:41 sample.f
    drwxr-sr-x   2 jsmith   staff        512 Mar 23 18:07 subdir1
    drwxr-sr-x   2 jsmith   staff        512 Mar 23 18:06 subdir2
    drwxr-sr-x   2 jsmith   staff        512 Mar 23 18:06 subdir3
        1        2   3        4           5       6          7
    
    1 = access modes/permissions
    2 = number of links
    3 = owner
    4 = group
    5 = size (in bytes)
    6 = date/time of last modification
    7 = name of file  
         

  6. Recursive listings can be very useful. Try both of the commands below. What does the output tell you?
    
         ls -R
         ls -Rl
         

  7. Try three options together:
    
         ls -lFa
         

more

  1. Read/scan the man page for more with the command:
    
         man more 
         

  2. Use the more command to read a file:
    
         more nasa.94
         

  3. As you are reading, notice the "More" prompt at the bottom of the page.

  4. Try pressing the return key - what happens?

  5. Try pressing the space bar once - what happens?

  6. Type the letter b - what happens?

  7. Use the search forward feature to find the word "MARCH" by entering the command:
    
         /MARCH 
         

  8. Now use the search backward feature to find the words "black hole" by entering the command:
     
         ?black hole 
         

  9. more will continue until the end of the file is reached or until you type q for quit. Try typing q to quit.

head and tail

  1. Read/scan the man page for head with the command:
    
         man head 
         

  2. Display the top of a file with the command below. How many lines do you count?
    
         head nasa.94 
         

  3. Now try this command and note how many lines are displayed this time?
    
         head -5 nasa.94 
         

  4. Read/scan the man page for tail with the command:
    
         man tail 
         

  5. Display the same file with the tail command. How is it different and how many lines do you count?
    
         tail nasa.94 
         

  6. Now try this command and note how many lines are displayed this time?
    
         tail -5 nasa.94 
         

cat

  1. Read/scan the man page for cat with the command:
    
         man cat 
         

  2. Use this command to display the contents of a file. What happens?
    
         cat nasa.94
         

  3. Now try this command notice the difference. How many lines are in the file?
    
         cat -n nasa.94 
         

  4. The cat command is more often used for purposes other than just displaying a file. Try these commands to "concatenate" two files into a new, third file:
    
         cat file1                  - first, show file1 
         cat file2                  - then, show file2 
         cat file1 file2 > newfile  - now do the actual concatenate 
         cat newfile                - finally, show the result 
         

cp

  1. Read/scan the man page for cp with the command:
    
         man cp 
         

  2. Copy an existing file in your current directory to another file in the current directory and then list your directory to prove that it was done:
    
         cp sample.f  sample2.f 
         ls
         

  3. Copy a file from a different directory to your current directory and then list your directory to prove that it was done:
    
         cp /etc/environment my.environment 
         ls
         

  4. Use the copy command with the "inquire" option. What happens?
    
         cp -i sample.f  sample2.f 
         

  5. Use the recursive option to copy an entire subdirectory to a new subdirectory and then list both directories to prove that it worked:
    
         cp -R subdir1 subdir4 
         ls subdir1 subdir4
         

  6. The copy command accepts "wildcard" characters. Try the command below. What did it do? List the subdir1 subdirectory to find out.
    
         cp samp* subdir1 
         ls subdir1
         

  7. If you are copying a file from another location to the current directory and want its name to remain the same, you can use the shorthand "." to indicate the current directory. Try this for example and note what happens:
    
         cp /etc/environment  . 
         ls
         

mv

  1. Read/scan the man page for mv with the command:
    
         man mv 
         

  2. The mv command can be used for renaming files. Try this command and then list your files to prove that the command worked:
    
         mv sample2.f  new.sample 
         ls
         

  3. mv can be used to rename directories also. Try this command and then list your files to prove that the command worked:
    
         mv subdir4  dir4  
         ls
         

  4. The mv command is also used for moving files. Use the command below to move new.sample into a new location, then list your files to prove that the command worked:
    
         mv new.sample  dir4  
         ls
         ls dir4
         

  5. Like many other UNIX commands, mv recognizes wildcard characters. Try the command below and then list your files to prove that the command worked:
    
         mv *env* dir4  
         ls
         ls dir4
         

rm

  1. Read/scan the man page for rm with the command:
    
         man rm
         

  2. Use the rm command to delete a file. List your directory after the command completes.
    
         rm sample.f
         ls
         

  3. cd to the subdir2 subdirectory. List the directory to view its contents. Then use the "*" wildcard to remove all of the files. NOTE: using rm in this manner can be dangerous! If you are in the wrong directory, you'll remove files you didn't mean to remove. You may want to use the -i option to protect yourself from accidents.
    
         cd subdir2
         ls
         rm -i *
         ls
         

  4. Get out of the subdir2 subdirectory by using the command cd .. Now try to use rm to remove a directory. What happens?
    
         cd ..
         rm subdir3
         

  5. This time, include the -r option when you try to remove a directory. What happens?
    
         rm -r subdir3
         ls
         

  6. Return to the tutorial to learn about the next section before proceeding.

file

  1. Read/scan the man page for file with the command:
    
         man file
         

  2. Use the file command to determine a file's type:
    
         file file1
         

  3. Now try it with a directory:
    
         file dir4
         

  4. Finally, try it with a wildcard character;
    
         file *
         

find

  1. Read/scan the man page for find with the command:
    
         man find
         

  2. Use the find command to find the file new.sample.
    
         find . -name new.sample -print
         

  3. Now use the find command to find all files with "file" as part of their name. Don't forget to put the wildcard specification in quotes - it won't work otherwise:
    
         find . -name 'file*' -print
         

  4. Try to find only directories with "file" as part of their name. Are there any?
    
         find . -name 'file*' -type d -print
         

diff and sdiff

  1. Read/scan the man page for diff with the command:
    
         man diff
         

  2. Use the diff command to determine the differences between two files:
    
         diff names1 names2
         

  3. Use the diff command to determine the differences between two directories.
    
         diff subdir1 dir4
         

  4. Try using sdiff to display the differences between names1 and names2:
    
         sdiff names1 names2
         

  5. Now try sdiff specifying that the screen width is 80 characters...not the default 130:
    
         sdiff -w 80 names1 names2
         

ln

  1. Read/scan the man page for ln with the command:
    
         man ln
         

  2. Create a link between two files in the same directory. Use ls -l to list your directory afterwards to confirm that the link was done:
    
         ln names1 linknames
         ls -l 
         

  3. Create a link between two files in different filesystems. Note that you'll need the -s option to accomplish this. Use ls -l to list your directory afterwards to confirm that the link was done. Notice how the link is identified in the listing.
    
         ln -s /etc/environment  myenv
         ls -l 
         

  4. Use the rm command to remove both of the links. Note that the original files will not be affected.
    
         rm linknames
         rm myenv
         ls -l
         

sort

  1. Read/scan the man page for sort with the command:
    
         man sort
         

  2. Use the cat command to look at an unsorted list of names. Notice that there are multiple columns of information and that the list is not alphabetically sorted.
    
         cat name.list
         

  3. Use the sort command to perform a basic sort of the list:
    
         sort name.list
         

  4. Do the same sort, but send the output to a file. Then use the cat utility to view the file:
    
         sort name.list > sorted.list ; cat sorted.list
         

  5. Sort the list by first names. This requires skipping over the first field, which is the last name:
    
         sort +1 name.list 
         

  6. Sort the list by department. This requires skipping over the first four fields and using the -b option to ignore blank characters:
    
         sort -b +4 name.list 
         

  7. Return to the tutorial to learn about the next section before proceeding.

pwd

  1. Read/scan the man page for pwd with the command:
    
         man pwd
         

  2. Issue the pwd command to display the name of your current working directory:
    
         pwd
         

  3. Change to several other directories and issue the pwd command between each change. Notice the different outputs:
    
         cd dir4
         pwd
         cd /usr/bin
         pwd
         cd /var/spool
         pwd
         cd ~/Filesystem
         pwd
         

mkdir

  1. Read/scan the man page for mkdir with the command:
    
         man mkdir
         

  2. First make sure that you are in your Filesystem directory. Then, create a new directory with the mkdir command. List your directory after the command completes to prove that it worked:
    
         cd ~/Filesystem
         mkdir newdir
         ls
         

  3. Now create some additional subdirectories within your newdir. List newdir after the command completes to prove that it worked:
    
         mkdir newdir/sub1  newdir/sub2  newdir/sub3
         ls newdir
         

  4. Try to create a directory in a location where you don't have permission. What happens?
    
         mkdir /etc/mydir
         

cd

  1. Read/scan the man page for cd with the command:
    
         man cd
         

  2. Change to your default, home directory:
    
         cd
         

  3. Change to a subdirectory within you home directory and list its contents:
    
         cd Filesystem/dir4/subdir1
         ls 
         

  4. Go up one level to the current directory's parent directory and list the contents:
    
         cd ..
         ls 
         

  5. Change to the root (top-most) directory and list the contents:
    
         cd /
         ls 
         

  6. Change to another student's directory and list the contents:
    
         cd ~studnt01
         ls 
         

  7. Change to another one of your subdirectories and list the contents:
    
         cd ~/Filesystem/newdir
         ls 
         

  8. Finally, change back to the exercise directory:
    
         cd ~/Filesystem
         

rmdir

  1. Read/scan the man page for rmdir with the command:
    
         man rmdir
         

  2. First make sure you are in the Filesystem directory. Then try to remove the newdir directory. What happens?
    
         cd ~/Filesystem
         rmdir newdir
         

  3. Recursively list the contents of newdir. Notice that its subdirectories are all empty. Remove all of the empty subdirectories within newdir and then list newdir again to confirm that they were removed:
    
         ls -R newdir
         rmdir newdir/*
         ls -R newdir
         

  4. Finally, remove the empty newdir directory:
    
         rmdir newdir
         

  5. Return to the tutorial to learn about the next section before proceeding.

Introduction to UNIX
Access Permissions Exercises


These exercises will familiarize you with the basic UNIX commands for working with file access permissions.

  1. Make sure that you are in your Filesystem exercise directory, and then do a long listing of your files:
    
         cd ~/Filesystem
         ls -al
         

  2. Notice the access permissions assigned to each file. Can you tell:

  3. Use the chmod command to change the permissions for some files. List the file before and after each change and determine how the command changes the file permissions.
    
         ls -l names1
         chmod o-r names1
         ls -l names1
    
         ls -l names2
         chmod g+w names2
         ls -l names2
    
         ls -l file*
         chmod go-r file*
         ls -l file*
         

  4. Try a chmod command with numerical access specifications:
    
         ls -l prog1
         chmod 754 prog1
         ls -l prog1
         

  5. Look in your ~/.cshrc file and find where your umask setting is specified. What is it and what does it do? How would you change it so that nobody except you could see your files?
    
         cd
         more .cshrc
         

  6. Return to the tutorial to learn about the next section before proceeding.

Introduction to UNIX
Standard UNIX File System Exercises


These exercises will familiarize you with the overall organization of your UNIX filesystem.

  1. Change directory to the root level and then list the contents.
    
         cd  /
         ls 
         
    How many files do you see which are mentioned in the tutorial as being common/standard UNIX files?

  2. Change to the /usr directory and list it, again noting how many files you see which are mentioned in the tutorial as being common/standard UNIX files?
    
         cd  /usr
         ls 
         

  3. Spend some time navigating about the upper levels of the filesystem.... see how lost you can get!! Try some of the other directories mentioned in the tutorial.

    Don't forget that you can always use the pwd command to tell you where you are, and the cd command to return to your home directory.

    This concludes the Filesystems exercises. Return to the Filesystems tutorial or to the Table of Contents.