CS 246 Lab 1
Typing Commands Efficiently

Throughout this lab you will be previous commands, but with minor changes. You can bring back the previous command with an up-arrow and then edit it by moving with the left and right arrow keys. You can delete the character to the left of the cursor with delete (or backspace) and delete the character under the cursor with ctrl-d. You can delete to the end of a line with ctrl-k.

    The ls Command
  1. Log in and open a terminal window.
  2. Use the ls command to list the files in your home directory
  3. Do ls -a and observe that files whose names begin with . are shown
  4. Do ls -l to get a long listing.
  5. Do man ls to read the manual page for ls. Find the option to reverse the order of the listing and try it out.
  6. Use pwd to print your current working directory.
  7. File Arguments, Standard Input, and Redirection
  8. Use the cd command to change your directory to /home/mathcs/courses/cs246.
  9. Use ls to list the files. There should be a file called passages and a file called war-and-peace.
  10. Print passages to the screen using cat passages.
  11. Try the cat command with no arguments. It will read from standard input and write to standard output. Type in a few lines to see what happens. You can terminate input by doing ctrl-d (at the beginning of a line).
  12. Do cat again. This time terminate the program by doing ctrl-c. This sends an interrupt signal to the program which will usually terminate it.
  13. Do cat < passages. Since cat is given no file arguments, it will read from standard input. However, < passages redirects input by reassigning standard input to the file passages.
  14. Now we'll try to redirect output. Use cat passages > foobar. Oops. This won't work because you don't have permission to write to this directory.
  15. Do cat passages > ~/foobar. Since ~ refers to your home directory, this will redirect output by reassigning standard output to the file foobar in your home directory.
  16. Use ls ~ to list the files in your home directory. You should now see the file foobar.
  17. To see what cat actually does, do cat passages ~/foobar. This will concatenate the contents of the two files, writing the output to standard output. Scroll up to see that there are two copies of the file.
  18. Do cat passages no-such-file. Since the second file doesn't exist, you will see an error message.
  19. Do cat passages no-such-file > ~/foobar. Observe that standard output is redirected. However, the error message is not, because it is written to standard error rather than standard output.
  20. Do cat passages no-such-file >& ~/foobar. >& redirects both standard output and standard error.
  21. Do cat ~/foobar to see that the error message is in the file.
  22. Pipelines
  23. Now we're going to get a list of the words in the file passages. The tr command transliterates characters. tr a A will change lower case a's to uppercase a's. tr a-z A-Z will change all lowercase characters to uppercase. Try this command, redirecting standard input to passages. Note that tr does not take file name arguments, so you must use redirection.
  24. Now we will separate words onto separate lines. tr ' ' '\n ' will change all spaces to newlines. Try it out, redirecting standard input to passages.
  25. Now we will combine the commands. Try the command tr a-z A-Z < passages | tr ' ' '\n'. This is called a pipeline. P1 | P2 connects standard output for program P1 to standard input for program P2. You could redirect the output from P1 into a file and then redirect the input for P2 to the same file, but a pipeline is much more convenient.
  26. You can connect many programs in a sequence using pipelines. Use tr a-z A-Z < passages | tr ' ' '\n' | sort to get a sorted list of words.
  27. The uniq command removes duplicate consecutive lines. Add it to the pipeline to get a list of words with no duplicates.
  28. If the first string given to tr is longer than the second, then all characters matching the extra characters in the first string are changed to the last character in the second string. For example tr abcd xy will change a to x, but also b, c, and d to y. Use this fact to remove all of the punctuation from the word list. Also, observe that you don't actually need two tr commands in your pipeline. Make it work with only one.
  29. The file war-and-peace contains the text to the novel War and Peace by Leo Tolstoy. Use the pipeline you have developed to get a list of words from War and Peace. However, since War and Peace has many, many words, pipe the output through the less command. This displays a screenful of data and waits for you to hit the spacebar to go to the next page. You will need to add in some additional characters to be transliterated to newlines.
  30. Do the same command again, but this time instead of piping it through less, pipe it through head. Then pipe it through tail to see the end of the list.
  31. Emacs

    Emacs is a very powerful text editor.

  32. Start up emacs (the command is emacs) and start the tutorial with ctrl-h t. Work through the tutorial.