1. What are the two file viewing programs that were discussed in the lecture recordings? Specifically list the ones that let you interactively navigate the file.
Solutions

Two programs we discussed were less and more. The two are essentially the same as features have been added to more over time, so you can use whichever one you like the name of more.

  1. Suppose we have a program named MyProgram.java in the current directory. What is the command to run the compiled version of this program and have it read standard input from a file?
Solutions
java MyProgram < file.txt

Note that we specified that we wanted to run the compiled version. If you wanted to compile and run in one step, you could run java MyProgram.java < file.txt.

  1. In your own words, describe the difference between running:
    grep "a" letters.txt
    
    and
    cat letters.txt | grep "a"
    
    Would you expect these two commands to have the same or different output? Why or why not?
Solutions

The output is the same, but how it runs is slightly different.

  • grep "a" letters.txt uses letters.txt as a command-line argument
  • cat letters.txt | grep "a" uses the content of letters.txt (generated by running the cat command) as standard input that the grep command reads from.

This is an important distinction, and we discussed in class how grep does this by looking at the SimpleGrep.java program we wrote.

  1. Fill in the blanks below as to what “type” of object should go in each blank based on the type of operations we are performing. For each blank, use options:

    • file for a file name
    • command for another command
    • file_or_command for a file or command


    command | _______
    _______ | command
    command < _______ > _______
    command < _______ | _______
    command < _______ | _______ > _______
    

Solutions

Solutions shown in CAPS

command | COMMAND
COMMAND | command
command < FILE > FILE
command < FILE | COMMAND
command < FILE | COMMAND > FILE
  1. What command would tell us how many files (including hidden files) exist in our parent directory?
Solutions

ls -a .. | wc -l

Bonus: how would you count only the hidden files in your parent directory?

Solutions
  • One way that includes any file with a . in it (i.e. includes false positives):
    • ls -a .. | grep "\." | wc -l
  • After looking at the examples in the man page, using the ^ operator:
    • ls -a .. | grep "^\." | wc -l
  • Using cut to only look at the first character:
    • ls -a .. | cut -c1 | grep "\." | wc -l
  1. What command would tell us how many lines include the string "System.out.println" in all files ending in .java in our directory?
Solutions

grep "System.out.println" *.java | wc -l

  1. Suppose that I had a file named MyOtherProgram.java in the current directory. What do you think would happen if I were to run java MyOtherProgram.java > MyOtherProgram.java?
Solutions

This tries to run MyOtherProgram.java and save its output to the file name MyOtherProgram.java. This means you will overwrite your Java file!