Classwork 2: More working with Linux

Tuesday, July 10, 2012     


[Previous Classwork] [Next Classwork]


This assignment will introduce the script command, and let you gain experience with different ways of creating, moving, and renaming both files and directories.

Complete the following:

  1. Ensure the machine is booted into Linux
  2. Login to Linux
  3. Open a termimal from the top left Applications menu -> System Tools -> Terminal
  4. Start a script, this will create a log, a file that will contain all of the commands you run, and the output you see while doing this assignment. This command will start running and continue running until you type exit at the end of this classwork.

    linux1[17]% script
    Script started, file is typescript

  5. Create a directory to keep all of the work for this assignment

    linux1[1]% mkdir cw02

  6. Type ls and verify your cw02 directory is there.

    linux1[2]% ls
    bin       cw02     Documents  lect  Music     Public     typescript  www
    cs104s12  Desktop  Downloads  Mail  Pictures  Templates  Videos
    

  7. Find out where you currently are with the pwd command.

    linux3[4]% pwd
    /afs/umbc.edu/users/d/s/dsheets/home
    

  8. Move into the cw02 directory you just created

    linux3[5]% cd cw02
    

  9. After moving into your new cw02 directory, use pwd again to verify your new location

    linux3[6]% pwd
    /afs/umbc.edu/users/d/s/dsheets/home/cw02
    

  10. Use echo to write a line of text to a file by entering the following command:

    linux3[7]% echo All work and no play blah blah > file1.txt
    

  11. Use ls to verify the file exists, and then use cat to display the contents of the file as shown below.

    linux3[8]% ls
    file1.txt
    linux3[9]% cat file1.txt
    All work and no play blah blah
    

  12. Create a copy of the file by using the cp command. cp takes two arguments (arguments are the term used to refer to the set of options we pass to the commands in Unix - Note that arguments are separated by spaces, so you will normally not use spaces in unix commands or filenames. It can be done, it is just more difficult). The first argument is the file you are copying FROM, the second is the file you are copying TO. So to copy from file1.txt to file2.txt we do:

    linux3[10]% cp file1.txt file2.txt
    

  13. We can now verify that both files exists with ls and verify file2.txt has the same content as file1.txt with cat

    linux3[11]% ls
    file1.txt  file2.txt
    linux3[12]% cat file2.txt
    All work and no play blah blah
    

  14. We will now make another directory, inside our cw02 directory called mydir

    linux3[13]% mkdir mydir
    

  15. Once again, ls allows us to make sure we know what files are in our current directory

    linux3[14]% ls
    file1.txt  file2.txt  mydir
    

  16. Lets create a file3.txt that will have much better contents then either file1.txt or file2.txt, once again we'll use echo

    linux3[15]% echo I voted for Kodos > file3.txt
    

  17. ls lets us verify that our file was created

    linux3[16]% ls
    file1.txt  file2.txt  file3.txt  mydir
    

  18. Now we will rename file3.txt to have a more appropriate name using the mv command. Like cp, this command takes two arguments the first is the file to move (or rename) and the second argument is the new location or name of the file. If the second argument is a directory name, then the file will be moved into the directory. If the second argument is a filename (or the name is something that doesn't exist) the file will be renamed.

    linux3[17]% mv file3.txt kodos.txt
    

  19. After renaming the file again use ls to verify the new directory contents.

    linux3[18]% ls
    file1.txt  file2.txt  kodos.txt  mydir
    

  20. You need to be careful, lets now overwrite our file2.txt by renaming kodos.txt to have the name file2.txt. You will notice that the mv command will overwrite an existing file without asking, so if you are not careful you can lose work. So we'll rename kodos.txt and the use cat and ls to verify the files in our directory and their contents as seen below.

    linux3[22]% mv kodos.txt file2.txt
    linux3[23]% cat file2.txt
    I voted for Kodos
    linux3[24]% ls
    file1.txt  file2.txt  mydir
    

  21. Now lets move file2.txt to the mydir directory we created a while ago.

    linux3[25]% mv file2.txt mydir
    linux3[26]% ls
    file1.txt  mydir
    

  22. We can move into mydir with the cd command and then use ls again to make sure file2.txt is now in there, and use pwd to make sure of where we are.

    linux3[28]% cd mydir
    linux3[29]% ls
    file2.txt
    linux3[30]% pwd
    /afs/umbc.edu/users/d/s/dsheets/home/cw02/mydir
    

  23. And we can thengo back up a directory, to our cw02 directory, using cd and passing the special argument .. which indicates the parent

    linux3[32]% cd ..
    linux3[33]% pwd
    /afs/umbc.edu/users/d/s/dsheets/home/cw02
    

  24. Now lets try to remove the directory we created using the rm command

    linux3[34]% rm mydir
    rm: cannot remove `mydir': Is a directory
    

  25. The rm command is kind enough to remind us that is isn't normally used to remove directories. The rmdir command should be used, so let's try that.

    linux3[35]% rmdir mydir
    rmdir: failed to remove `mydir': Directory not empty
    

  26. The rmdir command is being careful, and tells us that it won't normally remove a directory that still contains files. So now I want you to use cd to go back into the mydir directory, and then use mv to move the file2.txt from the mydir directory and into the parent directory (remember, the parent directory is always called ..)

    See if you can figure out the commands to use, ask for help if you need it
    

  27. Once the file is moved, use ls and pwd to verify where you are and what files are there. You should end up with file1.txt and file2.txt in the cw02 directory
  28. Once file2.txt is in the right location, also try renaming file2.txt, call it final.txt
  29. Also go and cleanup the mydir directory now that it is empty. Use the rmdir command to remove the now empty mydir directory.
  30. Once file1.txt and final.txt are in the same directory, lets end the script command we were running at the start of this exercise. We use the exit command to end a script session. The log will let me see what commands you ran. So execute the following commands:

    linux3[37]% exit
    exit
    Script done, file is typescript
    linux3[11]% ls
    bin       cw02     Documents  lect  Music     Public     typescript  www
    cs104s12  Desktop  Downloads  Mail  Pictures  Templates  Videos
    

  31. Now lets use mv to move AND rename the log, currently called typescript, that was created. Run the following command to do so:

    linux3[12]% mv typescript cw02/log.txt
    linux3[13]% cd cw02/
    linux3[14]% ls
    file1.txt  final.txt log.txt
    

  32. I want you to submit three files for this classwork, file1.txt, final.txt, and your log.txt. Do this by typing the following command

    linux3[26]% submit cs104 cw02 file1.txt final.txt log.txt
    

  33. After submitting your files, you can verify your file was submitting by using the submitls command.
  34. MAKE SURE YOU LOGOUT AFTER SUBMITTING THE ASSIGNMENT AND BEFORE LEAVING CLASS!