Learn Enough Society
Certificate of Course CompletionThis 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.
echo
on 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).)
echo
each time.
⌃A
, ⌃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 echo
should 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 echo
instead. 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.
echo
and >
, make files called line_1.txt
and line_2.txt
containing 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.txt
and then appending the contents of line_2.txt
. Call the new file sonnet_1_copy.txt
, and confirm using diff
that it’s identical to sonnet_1.txt
. Hint: When there is no diff between two files, diff
simply outputs nothing.
cat
to combine the contents of line_1.txt
and line_2.txt
in reverse order using a single command, yielding the file sonnet_1_reversed.txt
. Hint: The cat
command can take multiple arguments.
echo
command and the redirect operator >
to make a file called foo.txt
containing the text “hello, world”. Then, using the cp
command, make a copy of foo.txt
called bar.txt
. Using the diff
command, confirm that the contents of both files are the same.
cat
command and the redirect operator >
, create a copy of foo.txt
called baz.txt
without using the cp
command.
quux.txt
containing the contents of foo.txt
followed by the contents of bar.txt
. Hint: As noted in Section 2.1.1, cat
can take multiple arguments.
rm nonexistent
and rm -f nonexistent
differ for a nonexistent file?
echo
to make a file called sonnet_1_complete.txt
containing the full (original) text of Shakespeare’s first sonnet. Hint: You may recall getting stuck when echo
was 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 cat
(Figure 2.2).
foo
, rename it to bar
, and copy it to baz
.
bar
and baz
using 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 learnenough.com
(without the https://
)?
ls
, confirm that sonnets.txt
exists on your system. How big is it in bytes? Hint: Recall from Section 2.2 that the “long form” of ls
displays a byte count.
-h
(“human-readable”) option to ls
, list the long form of the sonnets file with a human-readable byte count.
tail sonnets.txt
through wc
, confirm that (like head
) the tail
command outputs 10 lines by default.
man head
, learn how to look at the first n
lines of the file. 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 (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 <i>
and <j>
represent the numerical arguments to the -n
option.
tail
is running tail -f
to 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.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 1.3 to get out of trouble.)
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.
grep All sonnets.txt | wc
. (We’ll learn about grep
in Section 3.4.)
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? Hint: Press n
to 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?
man
uses 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 -rtl
as a model.
man grep
for “line number”, construct a command to find the line numbers in sonnets.txt
where the string “rose” appears.
less sonnets.txt
. Hint: Recall from Table 3.1 that 1G
goes to the top of the file, i.e., line 1. Similarly, 17G
goes to line 17. Etc.
history
command prints the history of commands in a particular terminal shell (subject to some limit, which is typically large). Pipe history
to less
to examine your command history. What was your 17th command?
history
to wc
, count how many commands you’ve executed so far.
history
is 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 history
to grep
, determine the number for the last occurrence of curl
.
!!
(“bang bang”) to execute the previous command. Similarly, !n
executes command number n
, so that, e.g., !17
executes the 17th command in the command history. Use the result from the previous exercise to rerun the last occurrence of curl
.
O
and L
options in Listing 3.1 mean? Hint: Pipe the output of curl -h
to less
and search first for the string -O
and then for the string -L
.
~/foo/bar
.
/Users/bill/sonnets
, what is the home directory? What is the username? Which directory is deepest in the hierarchy?
bill
, how do /Users/bill/sonnets
and ~/sonnets
differ (if at all)?
~/foo
and ~/foo/bar
with a single command? Hint: Refer to the man page for mkdir
.
foo
and, within it, the directory bar
(i.e., ~/foo/bar
) with a single command.
ls
to grep
, list everything in the home directory that contains the letter “o”.
grep
to head
, print out the first (and only the first) line in sonnets.txt
containing “rose”. Hint: Use the result of the second exercise in Section 3.2.2.
grep
for “Rose”.
grep rose
and 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
to grep -v
, and then pipe that result to wc
. (What does -v
do? Read the man page for grep
(Box 1.4).)
cd
and cd ~
differ (or do they)?
text_files
, then change to second_directory
using the “one directory up” double-dot operator ..
.
nil
in text_files
using whatever method you wish.
nil
from the previous exercise using a different path from the one you used before. (In other words, if you used the path ~/text_files
before, use something like ../text_files
or /Users/<username>/text_files
.)
foo
with a subdirectory bar
, then rename the subdirectory to baz
.
text_files
, with directory, into foo
.
text_files
, without directory, into bar
.
foo
and everything in it using a single command.
foo
, change into it, create a file bar
with content “baz”, print out bar
’s contents, and then cd
back 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.