Cabrillo College

Objectives

The goal of this lab is to become proficient with system commands for copying, moving, renaming, creating and removing files within your home directory.

Forum

If you get stuck, have a question or want to share something you learned with this lab then use the CIS 90 Forum at http://simms-teach.com/forum/viewforum.php?f=9

Procedure

Log on to the Opus server so that you have a command line shell at your service. Be sure you are in your home directory to start this lab. We are going to reorganize the files in our home directory. This will involve making new subdirectories and moving files around. The questions asked during this procedure are for your clarification only. You will be graded on correctly performing the procedure. At the end of the lab you will submit your new layout by entering the command:

submit


Making Directories

  1. Display a listing of the files in your home directory using the ls -F command.
  2. Now lets make some new directories using the mkdir command
  3. Now try to make a subdirectory in a directory that doesn't yet exist by entering the following command:

  4. mkdir class/labs
    What happens? Try the same command using the -p option:
    mkdir -p class/labs
    Verify that it worked this time.
  5. Now that the class directory is made, you can make a second subdirectory

  6. called exams. Use the command: mkdir class/exams
    Notice how you did not need the -p option. Why not?

Renaming Files

Now that you have new subdirectories made, let's rename some of the existing directories. To do this, we use the mv command.
  1. Let's rename the Miscellaneous directory to just plain old misc.

  2. mv Miscellaneous misc
    Notice how the syntax is: mv original-name new-name
    Does it work?
  3. Use the same command to change the name of Poems to poems with a lowercase p.
  4. You can also rename a file using the same command. Unix doesn't distinguish between renaming directories and renaming ordinary files. Try renaming proposal1 to MarkTwain. Did it work?

Moving Files

Now we will actually move some files from one directory to another, renaming some of them as we do it.
  1. To move a file from one directory to another, we use the same syntax:

  2. mv filename directory
    The filename will be moved to the specified directory, keeping its same name.
    Let's try it: Use the ls -i command to look at the inode number of MarkTwain
    Now move that file to the docs directory we made earlier:
    mv MarkTwain docs
  3. Change directory to the docs directory and look at the inode number of MarkTwain

  4. What do you notice? The inode number is the same; the file hasn't moved, only the name has been moved from one directory to another.
  5. Now change back to your home directory, and let's move proposal2 into the docs directory and change its name to magna_carta at the same time.

  6. mv proposal2 docs/magna_carta
    By specifying a new name after the destination directory, we can rename the file at the same time we move it.
  7. See if you can do the same thing by moving the file, timecal, to your bin subdirectory and renaming it to datecal.

  8. (Make sure you are in your home directory.)
  9. Move proposal3 to the docs directory renaming it to policy.
  10. Now let's put a couple commands together to accomplish the following task:
  11. Good job! Did you try using one mv command for all three files?

  12. You can. The mv command has a third syntax:
    mv file1 file2 file3 ... filen directory
    The last argument must be a directory name, and all the preceding files will be
    moved into that directory - keeping their original names. Let's try it. Move the files: text.err, text.fxd, small_town, and spellk into the edits directory using the command:
    mv text.* small_town spellk edits
    Check the results by listing the contents of the edits directory. Pretty fancy?
  13. What happens when you move a file onto a file that already exists?

  14. Let's try it. Enter the command: ls -l letter bigfile
    Notice the sizes of the two files. (You may want to head them to remind you what they are.)
  15. Now move letter onto bigfile by entering:

  16. mv letter bigfile
    Did you get any error message? Any kind of message?
  17. Look at bigfile now. How big is it? What are its contents?

  18. Don't worry, we'll get bigfile back later.
    Move bigfile back to letter
  19. Now try doing the following moves:

Copying Files

Copying works just like moving except that a second file is created, the contents of which is exactly like the original.
  1. Copying files is useful for making backup copies. Let's make a backup of your letter file by typing the following command:

  2. cp letter letter.bak
    Notice that it has the same syntax as the mv command, but now you have two files.
    Run the ls -li command with arguments of letter and letter.bak
    Notice the sizes are the same, but they each have their own inode and their own data.
  3. Another reason to use the copy command is to copy a file from somewhere else on the system to your directory. In this case, the filename stays the same, but the second argument is a new directory into which we put the file. Try it with:
    cp /etc/hosts etc
    You have just copied the file hosts from root's /etc directory to your own personal etc directory. Verify that the file is in your etc directory.
  4. Copy the files lab01.answers and lab02.answers... files from /home/cis90/labs into your class/labs directory with the command:
    cp /home/cis90/answers/lab0?.* class/labs
    What does the questionmark (?) and asterisk (*) do?
  5. Now use the cp command to copy the file sonnet6 from guest's Shakespeare directory to your Shakespeare directory. (Notice that your Shakespeare directory doesn't have a sonnet6 in it.)
  6. Like the mv command, the cp command is also destructive. If the target file exists, it will be destroyed, and copied over. If you want the cp command to warn you about destroying a target file, then you must use the -i option. Try this in your home directory:
    cp -i letter letter.bak
    Notice how it asked you whether you wanted to overwrite the letter.bak file.
  7. The cp command may also be used to copy multiple files to a single directory in the same way you used the mv command.
    More useful is copying an entire directory. Let's copy the entire Shakespeare directory to a directory called Sonnets.
    First, cd to your poems directory. Now issue the following command:
    cp -r Shakespeare Sonnets
    Verify that it worked using the command: ls S*
    (the -r stands for recursive.)

Removing Files and Directories

Removing files is inherently destructive in nature. Unix does not give any warnings or opportunity to un-remove a file. So be careful in using this command.
  1. Make sure you are back in your home directory. What command did you use?
  2. The remove command is dangerous in it's simplicity. It takes one or more filenames as arguments. Try removing your empty file with the command:
    rm empty
    Try listing the file; it's gone - for good - no warning - poof.
  3. If you would like the remove command to ask you if you're sure, use the -i option. Try:
    rm -i letter.bak
    answer with anything but a 'y' or 'yes' and it will not remove the file.
  4. For some fun, change your directory to Lab2.0 and run the ls command.
    • Now run the command: rm *.*
      What was removed? Is it like DOS?
    • Try removing the file annual report
      Do you see why spaces in file names is not a good idea? How could you remove that file?
    • Finally, try this command: rm -i *
      Answer y to all questions. Are all the files gone? Change back to your home directory.
  5. From your home directory, try to remove the directory, Lab2.0
    The remove command doesn't work on directory files. For that we have the rmdir command. Try it:
    rmdir Lab2.0
    Why didn't it work?
  6. List the entire contents of the Lab2.0 directory using the ls -a command.
    Remove the hidden file .junk, and then try the rmdir command again.
  7. The lesson here is that rmdir removes only empty directories. There is a way to remove an entire directory and its contents. This is obviously a very dangerous command. From your home directory, run the following command:
    rm -r Lab2.1
    Notice that the directory and all its contents are gone, no warnings.
    Be careful when using this recursive option to the rm command.
  8. Remove your Sonnets directory and it's contents.

Linking Files

Linking files is a way of giving additional names to an existing file. You are not duplicating data, just adding another name into your directory.
  1. I told you I would get your bigfile back for you. The file is still in the /home/cis90/guest directory. Rather than copying it from there, let's do a link to it. Use the following commands from your home directory:
    ls -l ../guest/bigfile
    Notice the link count field, the owner and the size of the file. Now enter:
    ln ../guest/bigfile .
    You have just made a link to guest's bigfile in your home directory. Do a long listing of bigfile. what is the link count now? Who owns it?
    You now have access to guest's file, as if it is in your directory.
  2. Change directory to your etc directory and link the motd to the name greeting. Use the ln and the filenames as the two arguments. Use the ls -l command to verify your success.

You have now finished Lab 5. Your home directory is now reorganized. To submit your work to be counted for this lab, you must run the submit command from your home directory. This will take a picture of your home directory and send it to your instructor.