Learn Enough SocietyCertificate of Course Completion
This page certifies that mhartl has completed Learn Enough Command Line to Be Dangerous! 🎉
echo 'hello(with a mismatched single quote), and then get out of trouble using the technique from Box 1.3.
echoon your system?
By reading the man page for
echo, determine the command needed to print out “hello” without the trailing newline, and verify using your terminal that it works as expected. Hints: To determine the placement of the command-line option, it may help to refer to Figure 1.5. By comparing your result with Listing 1.5 and Listing 1.6, you should be able to verify that you’ve used the option properly. (Note: This exercise may fail when using the default terminal program on some older versions of macOS. In this case, I recommend installing iTerm (which isn’t a bad idea anyway).)
⌃E, arrow keys, or Option-click to change the occurrences of the short s to the archaic long s “ſ” in order to match the appearance of the original (Figure 1.11). In other words, the argument to
echoshould read “FRom faireſt creatures we deſire increaſe,”. Hint: It’s unlikely that your keyboard can produce “ſ” natively, so either copy it from the text of this tutorial or Google for it and copy it from the Internet. (If you have trouble copying and pasting into your terminal, I suggest applying the ideas in Box 1.4 to figure out how to do it on your system.)
echo 'hello', and then exit.
Use "man echo", including the quotes; i.e., take care not to print out
Use man echoinstead. Hint: Use double quotes in the inner string, and wrap the whole thing in single quotes.
man sleep, figure out how to make the terminal “sleep” for 5 seconds, and execute the command to do so.
>, make files called
line_2.txtcontaining the first and second lines of Sonnet 1, respectively.
sonnet_1.txt(containing the first two lines of the sonnet) by first redirecting the contents of
line_1.txtand then appending the contents of
line_2.txt. Call the new file
sonnet_1_copy.txt, and confirm using
diffthat it’s identical to
sonnet_1.txt. Hint: When there is no diff between two files,
diffsimply outputs nothing.
catto combine the contents of
line_2.txtin reverse order using a single command, yielding the file
sonnet_1_reversed.txt. Hint: The
catcommand can take multiple arguments.
echocommand and the redirect operator
>to make a file called
foo.txtcontaining the text “hello, world”. Then, using the
cpcommand, make a copy of
bar.txt. Using the
diffcommand, confirm that the contents of both files are the same.
catcommand and the redirect operator
>, create a copy of
baz.txtwithout using the
quux.txtcontaining the contents of
foo.txtfollowed by the contents of
bar.txt. Hint: As noted in Section 2.1.1,
catcan take multiple arguments.
rm -f nonexistentdiffer for a nonexistent file?
echoto make a file called
sonnet_1_complete.txtcontaining the full (original) text of Shakespeare’s first sonnet. Hint: You may recall getting stuck when
echowas followed by an unmatched double quote (Section 1.3 and Box 1.3), as in
echo ", but in fact this construction allows you to print out a multi-line block of text. Just remember to put a closing quote at the end, and then redirect to a file with the appropriate name. Check that the contents are correct using
foo, rename it to
bar, and copy it to
bazusing a single call to
rm. Hint: If those are the only two files in the current directory that start with the letter “b”, you can use the wildcard pattern from the previous exercise.
curl -I https://www.learnenough.com/to fetch the HTTP header for the Learn Enough website. What is the HTTP status code for the address? How does this differ from the status code for
ls, confirm that
sonnets.txtexists on your system. How big is it in bytes? Hint: Recall from Section 2.2 that the “long form” of
lsdisplays a byte count.
-h(“human-readable”) option to
ls, list the long form of the sonnets file with a human-readable byte count.
wc, confirm that (like
tailcommand outputs 10 lines by default.
man head, learn how to look at the first
nlines of the file. By experimenting with different values of
n, find a
headcommand to print out just enough lines to display the first sonnet in its entirety (Figure 1.11).
tail(with the appropriate options) to print out only the 14 lines composing Sonnet 1. Hint: The command will look something like
head -n <i> sonnets.txt | tail -n <j>, where
<j>represent the numerical arguments to the
tail -fto view a file that’s actively changing. This is especially common when monitoring files used to log the activity of, e.g., web servers, a practice known as “tailing the log file”. To simulate the creation of a log file, run
ping learnenough.com > learnenough.login one terminal tab. (The
pingcommand “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 -fyou should use the technique from Box 1.3 to get out of trouble.)
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.
grep All sonnets.txt | wc. (We’ll learn about
grepin Section 3.4.)
/(“slash”), find the sonnet that begins with the line “Let me not”. Are there any other occurrences of this string in the Sonnets? Hint: Press
nto find the next occurrence (if any). Extra credit: Listen to the sonnet in both modern and original pronunciation. Which version’s rhyme scheme is better?
less, we are now in a position to search man pages interactively. 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? Hint: Use
ls -rtlas a model.
man grepfor “line number”, construct a command to find the line numbers in
sonnets.txtwhere the string “rose” appears.
less sonnets.txt. Hint: Recall from Table 3.1 that
1Ggoes to the top of the file, i.e., line 1. Similarly,
17Ggoes to line 17. Etc.
historycommand prints the history of commands in a particular terminal shell (subject to some limit, which is typically large). Pipe
lessto examine your command history. What was your 17th command?
wc, count how many commands you’ve executed so far.
historyis to grep your commands to find useful ones you’ve used before, with each command preceded by the corresponding number in the command history. By piping the output of
grep, determine the number for the last occurrence of
!!(“bang bang”) to execute the previous command. Similarly,
!nexecutes command number
n, so that, e.g.,
!17executes the 17th command in the command history. Use the result from the previous exercise to rerun the last occurrence of
Loptions in Listing 3.1 mean? Hint: Pipe the output of
lessand search first for the string
-Oand then for the string
/Users/bill/sonnets, what is the home directory? What is the username? Which directory is deepest in the hierarchy?
bill, how do
~/sonnetsdiffer (if at all)?
~/foo/barwith a single command? Hint: Refer to the man page for
fooand, within it, the directory
~/foo/bar) with a single command.
grep, list everything in the home directory that contains the letter “o”.
head, print out the first (and only the first) line in
sonnets.txtcontaining “rose”. Hint: Use the result of the second exercise in Section 3.2.2.
grep -i rose. Write a command confirming that the number of lines matching “Rose” but not matching “rose” is equal to the expected 2. Hint: Pipe the result of
grep -v, and then pipe that result to
wc. (What does
-vdo? Read the man page for
cd ~differ (or do they)?
text_files, then change to
second_directoryusing the “one directory up” double-dot operator
text_filesusing whatever method you wish.
nilfrom the previous exercise using a different path from the one you used before. (In other words, if you used the path
~/text_filesbefore, use something like
foowith a subdirectory
bar, then rename the subdirectory to
text_files, with directory, into
text_files, without directory, into
fooand everything in it using a single command.
foo, change into it, create a file
barwith content “baz”, print out
bar’s contents, and then
cdback to the directory you came from. Hint: Combine the commands as described in Box 4.2.
rm -rf /is unbelievably dangerous, and why you should never type it into a terminal window, not even as a joke.
Get free access to all 10 Learn Enough courses (including the Ruby on Rails Tutorial) for 7 days!
We require a credit card for security purposes, but it will not be charged during the trial period. After 7 days, you will be enrolled automatically in the monthly All Access subscription.
BUT you can cancel any time and still get the rest of the 7 days for free!
All Learn Enough tutorials come with a 60-day 100% money-back guarantee.