Solutions

What Does sort -n Do?

The -n flag specifies a numeric sort, rather than alphabetical.

What Does >> Mean?

In the first example with >, the string "hello" is written to testfile01.txt, but the file gets overwritten each time we run the command.

We see from the second example that the >> operator also writes "hello" to a file (in this casetestfile02.txt), but appends the string to the file if it already exists (i.e. when we run it for the second time).

Appending Data

Option 3 is correct. For option 1 to be correct we would only run the head command. For option 2 to be correct we would only run the tail command. For option 4 to be correct we would have to pipe the output of head into tail -2 by doing head -3 animals.txt | tail -2 >> animalsUpd.txt

Piping Commands Together

Option 4 is the solution. The pipe character | is used to feed the standard output from one process to the standard input of another. > is used to redirect standard output to a file. Try it in the data-shell/molecules directory!

What Does < Mean?

< is used to redirect input to a command.

In both examples, the shell returns the number of lines from the input to the wc command. In the first example, the input is the file notes.txt and the file name is given in the output from the wc command. In the second example, the contents of the file notes.txt are redirected to standard input. It is as if we have entered the contents of the file by typing at the prompt. Hence the file name is not given in the output - just the number of lines. Try this for yourself:

wc -l
this
is
a test
Ctrl-D # This lets the shell know you have finished typing the input

3

.

Why Does uniq Only Remove Adjacent Duplicates?

sort salmon.txt | uniq

.

Pipe Reading Comprehension

The head command extracts the first 5 lines from animals.txt. Then, the last 3 lines are extracted from the previous 5 by using the tail command. With the sort -r command those 3 lines are sorted in reverse order and finally, the output is redirected to a file final.txt. The content of this file can be checked by executing cat final.txt. The file should contain the following lines:

2012-11-06,rabbit
2012-11-06,deer
2012-11-05,raccoon

.

Pipe Construction

cut -d , -f 2 animals.txt | sort | uniq

.

Which Pipe?

Option 5. is the correct answer. If you have difficulty understanding why, try running the commands, or sub-sections of the pipelines (make sure you are in the data-shell/data directory).

Wildcard Expressions

ls *A.txt
ls *B.txt

The output from the new commands is separated because there are two commands. When there are no files ending in A.txt, or there are no files ending in B.txt.

Removing Unneeded Files

  1. This would remove .txt files with one-character names
  2. This is correct answer
  3. The shell would expand * to match everything in the current directory, so the command would try to remove all matched files and an additional file called .txt
  4. The shell would expand *.* to match all files with any extension, so this command would delete all files