Note: The exercises here are meant for Linux and macOS. That doesn't mean that they cannot be executed in a Windows operating system but it might require some adaptation of the syntax.

From fairest creatures we desire increase,

That thereby beauty’s rose might never die,

But as the riper should by time decease,

His tender heir might bear his memory;

But thou, contracted to thine own bright eyes,

Feed’st thy light’s flame with self-substantial fuel,

Making a famine where abundance lies,

Thyself thy foe, to thy sweet self too cruel.

Thou that art now the world’s fresh ornament

And only herald to the gaudy spring,

Within thine own bud buriest thy content,

And, tender churl, mak’st waste in niggarding.

Pity the world, or else this glutton be,

To eat the world’s due, by the grave and thee.

Redirecting and appending

Exercise 1: Answer

Using echo and >, make files called line_1.txt and line_2.txt containing the first and second lines of Sonnet 1, respectively. Sonnet 1 can be found at the end of the page

echo "From fairest creatures we desire increase," > line_1.txt , echo "That thereby beauty's rose might never die," > line_2.txt

Exercise 2: Answer

Replicate the original 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

echo "From fairest creatures we desire increase," > sonnet_1_copy.txt , echo "That thereby beauty's rose might never die," >> sonnet_1_copy.txt , diff sonnet_1.txt sonnet_1_copy.txt

Exercise 3: Answer

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

cat line_2.txt line_1.txt > sonnet_1_reversed.txt

Listing

Exercise 1: Answer

What’s the command to list all the files (and directories) that start with the letter s?

ls s*

Exercise 2: Answer

What is the command to list all the files that contain the string onnet, long-form by reverse modification time?

ls -rtl *onnet*

Exercise 3: Answer

What is the command to list all files (including hidden ones) by reverse modification time, in long form?

ls -artl

Renaming, copying, deleting

Exercise 1: Answer

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.

echo "hello, world" > foo.txt , cp foo.txt bar.txt , diff foo.txt bar.txt

Exercise 2: Answer

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

cat foo.txt > baz.txt

Exercise 3: Answer

Create a file called quux.txt containing the contents of foo.txt followed by the contents of bar.txt.

cat foo.txt bar.txt > quux.txt

Exercise 4: Answer

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

rm nonexistant asks for confirmation, rm -f nonexistant does not ask for comfirmation

Summary

Exercise 1: Answer

By copying and pasting the text at the end of this file, use echo to make a file called sonnet_1_complete.txt containing the full (original) text of Shakespeare's first sonnet.

echo "FRom faireſt creatures we deſire increaſe, That thereby beauties Roſe 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." > sonnet_1_complete.txt , cat sonnet_1_complete.txt

Exercise 2: Answer

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

touch foo, mv foo bar, cp bar baz

Exercise 3: Answer

What is the command to list only the files starting with the letter b?

ls b*

Exercise 4: Answer

Remove both bar and baz using a single call to rm.

rm ba*