Download Learn Enough Command Line to Be Dangerous _ Learn Enough Command Line to Be Dangerous _ Softcover PDF

TitleLearn Enough Command Line to Be Dangerous _ Learn Enough Command Line to Be Dangerous _ Softcover
File Size1.1 MB
Total Pages46
Document Text Contents
Page 1

LEARN ENOUGH TO BE DANGEROUS

Log In
Sign Up

Learn Enough Command Line to Be Dangerous
Michael Hartl

Learn Enough Command Line to Be Dangerous | Learn Enough Comma... http://www.learnenough.com/command-line

1 of 46 9/22/2015 9:58 PM

Page 2

An introduction to the command line

DRAFT


Free!

Want to be notified when this is ready?

Get updates Contact Author
Follow author to receive email updates about new content

Learn Enough Command Line to Be Dangerous

Learn Enough™ Command Line to Be Dangerous

Michael Hartl

Learn Enough™ Command Line to Be Dangerous is an introduction to the command line for complete
beginners, the first in a series of tutorials designed to teach the common foundations of “computer magic”
(Box 1). It is aimed both at those who work with software developers and those who aspire to become
developers themselves. Learn Enough™ Command Line to Be Dangerous assumes no prerequisites other
than general computer knowledge (how to launch an application, how to use a web browser, etc.), which
among other things means that it doesn’t assume you know how to use a text editor, or even what a text editor
is. Indeed, this tutorial doesn’t even assume you know what a command line is, so if you’re confused by the
title, you’re still in the right place. Finally, even if you already have some familiarity with the command line,
you might enjoy following this tutorial (and doing the exercises) to brush up on the basics.

Box 1. The magic of computer programming

Computer programming may be as close as we get to magic in the real world: we type incantations into a
machine, and—if the incantations are right—the machine does our bidding. To perform such magic,

computer witches and wizards rely not only on words, but also on wands, potions, and an ancient tome or
two. Taken together, these tricks of the trade are known as software development: computer programming,
plus tools like command lines, text editors, and version control. Knowledge of these tools is perhaps the main
line between “technical” and “non-technical” people (or, to put it in magical terms, between wizards or
witches and Muggles). The present tutorial represents the first step needed to cross this line and learn enough
software development to be dangerous—to be able to cast computer spells, and get the machine to do our
bidding.

My name is Michael Hartl, and I am perhaps best known as the creator of the Ruby on Rails Tutorial, a book
and screencast series that together constitute one of the leading introductions to web development. (You may
also know me, in my more mathematical mode, as the founder of Tau Day and author of The Tau Manifesto.)
One of the most frequently asked questions about the Rails Tutorial is, “Is the Rails Tutorial good for
complete beginners?” The answer is, “Not really.” While it is possible for complete beginners to learn web
development with the Ruby on Rails Tutorial (and an impressively large number have), it can be challenging
and occasionally frustrating, and I don’t generally recommend it. Instead, I recommend starting here.

Learn Enough Command Line to Be Dangerous | Learn Enough Comma... http://www.learnenough.com/command-line

2 of 46 9/22/2015 9:58 PM

Page 23

Figure 13: The terseness of Unix commands can be a source of confusion.

The answer is that Unix dates from a time when most computer users logged on to centralized servers over
slow connections, and there could be a noticeable delay between the time users pressed a key and the time it
appeared on the terminal. For frequently used commands like listing files, the difference between list and
ls or remove and rm could be significant. As a result, the most commonly used Unix commands tend to be

only two or three letters long. Because it makes them more difficult to memorize, this can be a minor
inconvenience when learning them, but over a lifetime of command-line use the savings represented by, say,

mv really add up.

Exercises

Use the 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.

1.

By combining the cat command and the redirect operator >, create a copy of foo.txt called baz.txt
without using the cp command.

2.

Create a file called 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.

3.

How do rm nonexistent and rm -f nonexistent differ for a nonexistent file?4.

2.4 Summary

Important commands from this section are summarized in Table 2.

Command Description Example

> Redirect output to filename $ echo foo > foo.txt

>> Append output to filename $ echo bar >> foo.txt

Learn Enough Command Line to Be Dangerous | Learn Enough Comma... http://www.learnenough.com/command-line

23 of 46 9/22/2015 9:58 PM

Page 24

cat <file> Print contents of file to screen $ cat hello.txt

diff <f1> <f2> Diff files 1 & 2 $ diff foo.txt bar.txt

ls List directory or file $ ls hello.txt

ls -l List long form $ ls -l hello.txt

ls -rtl Long by reverse modification time $ ls -rtl

ls -a List all (including hidden) $ ls -a

touch <file> Create an empty file $ touch foo

mv <old> <new> Rename (move) from old to new $ mv foo bar

cp <old> <new> Copy old to new $ cp foo bar

rm <file> Remove (delete) file $ rm foo

rm -f <file> Force-remove file $ rm -f bar

Table 2: Important commands from Section 2.

Exercises

FRom faireſt creatures we deſire increaſe, That thereby beauties might neuer die, But as the
riper ſhould by time deceaſe, His tender heire might beare his memory: But thou contracted to
thine owne bright eyes, Feed’ſt thy lights flame with ſelfe ſubſtantiall fewell, Making a famine
where aboundance lies, Thy ſelfe thy foe,to thy ſweet ſelfe too cruell: Thou that art now the
worlds freſh ornament, And only herauld to the gaudy ſpring, Within thine owne bud burieſt thy
content, And tender chorle makſt waſt in niggarding: Pitty the world,or elſe this glutton be,
To eate the worlds due,by the graue and thee.

Figure 14: A copy-and-pastable version of Shakespeare’s first sonnet ( Figure 9).

By copying and pasting the text in Figure 14, use echo to make a file called sonnet_1_complete.txt

containing the full (original) text of Shakespeare’s first sonnet. : You may recall getting stuck
when echo was followed by an unmatched double quote (Section 1.2 and Box 4), 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 11).

1.

Type the sequence of commands needed to create an empty file called foo, rename it to bar, and copy
it to baz.

2.

What is the command to list only the files starting with the letter “b”? : Use a wildcard.3.
Remove both bar and baz using a call to rm. : Use the wildcard pattern from the previous
exercise.

4.

3 Inspecting files

Having seen how to create and manipulate files, now it’s time to learn how to examine their contents. This is
especially important for files too long to fit on a single screen. In particular, we saw starting in Section 2.1
how to use the cat command to dump the file contents to the screen, but this doesn’t work very well for
longer files.

3.1 Downloading a file

Learn Enough Command Line to Be Dangerous | Learn Enough Comma... http://www.learnenough.com/command-line

24 of 46 9/22/2015 9:58 PM

Page 45

defer these important topics to the follow-on tutorials to this one (Section 5), starting with Learn
Enough™ Text Editor to Be Dangerous. ↑
Image retrieved from https://commons.wikimedia.org
/wiki/File:Jazz_at_Knicks_man_to_man_defense.jpg on 2015-07-22 ↑

8.

There are also commands for moving one word at a time (ESC F and ESC B), but I hardly ever use
them myself, so it’s clear they are not required to be dangerous. ↑

9.

Note that the Original Pronunciation (OP) of Shakespearean English is different from modern
pronunciation. Generally speaking, Shakespeare’s sonnets include many word pairs that don’t rhyme in
modern English but do in OP. In the case of Figure 9, the word “memory” should be pronounced
“MEM-or-aye”, leading to a rhyme between lines 2 and 4 (ending in die and memory, respectively).
Image retrieved from https://commons.wikimedia.org/wiki/File:Sonnet_1.jpg on 2015-07-20 ↑

10.

Image retrieved from https://www.flickr.com/photos/tiffanyday/4076289684 on 2015-07-22 ↑11.

From the essay “What You Can’t Say” by Paul Graham (2004). As Graham notes:

The verb “diff” is computer jargon, but it’s the only word with exactly the sense I want. It
comes from the Unix diff utility, which yields a list of all the differences between two
files. More generally it means an unselective and microscopically thorough comparison
between two versions of something.



12.

A bit is one piece of yes-or-no information (such as a 1 or a 0), and a byte is eight bits. Bytes are
probably most familiar from “megabytes” and “gigabytes”, which represent a million and a billion
bytes, respectively. (The official story is a little more complicated, but level of detail here is certainly
enough to be dangerous.) ↑

13.

Technically, which locates a file on the user’s path, which is a list of directories where executable
programs are located. ↑

14.

Having known about ls -a and ls -rtl for a while—which together yield the suggestive command
ls -artl—one day I decided to add an “h”’ (for obvious reasons). This is actually how I accidentally
discovered the useful -h option some years ago. ↑

15.

On some systems, apparently they’re exactly the same program, so less really is more (or, more
accurately, more is less). ↑

16.

Image retrieved from https://commons.wikimedia.org/wiki/File:Tudor_Rose.svg on 2015-07-21 ↑17.
Though undated, most of Shakespeare’s sonnets were probably composed in the later years of the reign
of Queen Elizabeth, whose royal house adopted a rose (Figure 15) as its heraldic emblem. Given this
context, Shakespeare’s choice of floral imagery isn’t surprising, but in fact only a few commentators on
the Sonnets have noticed the seemingly obvious reference. ↑

18.

It comes from “ lobally search a egular xpression and rint”, but I told you it wasn’t important. ↑19.
Actually, “ROSE”, “RoSE”, “rOSE”, etc., all match as well, but “Rose” is the likeliest candidate.
Confirming this hunch is left as an exercise (Section 3.4.1). ↑

20.

My directory has a huge number of text files, ’cause that’s just how I roll, so the command I ran was
really find . -name *.txt | grep text_files, which filters out anything that doesn’t match the
directory being used in this tutorial. ↑

21.

Image retrieved from https://www.flickr.com/photos/whatleydude/13950241545 on 2015-07-22 ↑22.

◄ ▲ ►

Learn Enough Command Line to Be Dangerous | Learn Enough Comma... http://www.learnenough.com/command-line

45 of 46 9/22/2015 9:58 PM

Page 46

Sorry, content not available
x STAY UP TO DATE!

Joining the email list for this book will allow the author to contact you to let you know about special offers
and when updates for the book are available.

Learn Enough Command Line to Be Dangerous | Learn Enough Comma... http://www.learnenough.com/command-line

46 of 46 9/22/2015 9:58 PM

Similer Documents