Downloading a file

Exercise 1: Answer

Use the command curl -I www.learnenough.com to fetch the HTTP header for the Learn Enough website. What is the HTTP status code of the address? How does this differ from the status code of https://www.learnenough.com/

200 ok , status code of learnenough.com is 301 Moved Permanently

Exercise 2: Answer

Using ls, confirm that sonnets.txt exists on your system. How big is it in bytes? If it Doesn’t exist, you can create it from Sonnets page.

ls -l sonnets.txt , 96635 bytes

Exercise 3: Answer

Using the -h ("human-readable") option to ls, list the long form of the sonnets file with a human-readable byte count.

ls -l sonnets.txt , 96635 bytes

Exercise 4: Answer

Suppose you wanted to list the files and directories using human-readable byte counts, all, by reverse time-sorted long-form. Why might this command be the personal favourite of every engineer?

ls -hartl , hartl the author's last name

Making heads and tails of it

Exercise 1: Answer

By piping the results of tail sonnets.txt through wc, confirm that (like head) the tail command outputs 10 lines by default.

tail sonnets.txt | wc

Exercise 2: Answer

By experimenting with different values of n, find a head command to print out just enough lines to display the first sonnet in its entirety

head -n 18 sonnets.txt

Exercise 3: Answer

Pipe the results of the previous exercise through tail (with the appropriate options) to print out only the 14 lines composing Sonnet 1.

head -n 18 sonnets.txt | tail -n 14

Exercise 4: Answer

To simulate the creation of a log file, run ping http://learnenough.com > learnenough.log in one terminal tab. (The ping command "pings" a server to see if it's working.) In a second tab, type the command to tail the log file. (At this point, both tabs will be stuck, so once you've gotten the gist of tail -f you should use the technique from Box 4 to get out of trouble.)

ping learnenough.com > learnenough.log , tail -f learnenough.log , ctrl-c

Less is more

Exercise 1: Answer

Run less on sonnets.txt. Go down three pages and then back up three pages. Go to the end of the file, then to the beginning, then quit.

press spacebar x3, press ctrl-b x3, G(capital g), 1G , q

Exercise 2: Answer

Search for the string “All” (case-sensitive). Go forward a few occurrences, then back a few occurrences. Then go to the beginning of the file and count the occurrences by searching forward until you hit the end. Compare your count to the result of running grep All sonnets.txt | wc.

/All, press n, press N, 10 "Alls", typegrep All sonnets.txt | wc

Exercise 3: Answer

Using less and / (“slash”), find the sonnet that begins with the line “Let me not”. Are there any other occurrences of this string in the Sonnets?

less sonnets.txt, /Let me not, no

Exercise 4: Answer

By searching for the string “sort” in the man page for ls, discover the option to sort files by size. What is the command to display the long form of files sorted so the largest files appear at the bottom?

s -Slr

Grepping

Exercise 1: Answer

By searching man grep for “line number”, construct a command to find the line numbers in sonnets.txt where the string “rose” appears.

grep -n rose sonnets.txt

Exercise 2: Answer

You should find that the last occurrences of “rose” is (via “roses”) on line 2203. Figure out how to go directly to this line when running less sonnets.txt.

less sonnets.txt, 2203G

Exercise 3: Answer

By piping the output of grep to head, print out the first (and only the first) line in sonnets.txt containing “rose”.

grep -n rose sonnets.txt | head -n 1

Exercise 4: Answer

In previous exercise, we saw two additional lines that case-insensitively matched “rose”. Execute a command confirming that both of the lines contain the string “Rose” (and not, e.g., “rOSe”).

grep Rose sonnets.txt

Exercise 5: Answer

Write a command confirming that the number of lines matching “Rose” but not matching “rose” is equal to the expected 2.

grep Rose sonnets.txt | grep -v rose | wc

Summary

Exercise 1: Answer

Pipe history to less to examine your command history. What was your 17th command?

history | less , uname -r

Exercise 2: Answer

By piping the output of history to wc, count how many commands you’ve executed so far.

history | wc, 401

Exercise 3: Answer

By piping the output of history to grep, determine the number for the last occurrence of curl.

history | grep, 402

Exercise 4: Answer

Use the result from the previous exercise to re-run the last occurrence of curl.

!402, !401

Exercise 5: Answer

What do the O and L options in curl mean?

-O = remote-output = write output to a file named as the remote file, -L = location/follows redirects