Creating files
Folders were mkdir. Files have two flavors of creation, depending on whether you want them
empty or already containing something.
The empty way: touch
The simplest:
touch hello.txt
Run ls. There’s a new file called hello.txt. It’s empty — zero bytes, nothing in it.
touch is named for what it does in old Unix folklore: it touches a file. If the file
doesn’t exist, it’s created empty. If it already exists, its “last modified” time is bumped
but the contents stay untouched.
You’ll use touch mostly when you need a placeholder file to exist — maybe a tool expects to
find a file there, even an empty one.
You can make several at once:
touch readme.md notes.txt todo.txt
Three empty files, one keystroke.
The “with something in it” way: echo >
More useful: create a file that already has content. Try:
echo "Hello, terminal." > greeting.txt
Two new ideas are crammed into that line, and they show up everywhere from here on:
echoprints whatever you give it. Tryecho "hi"on its own — it just printshiback at you.>is the redirect arrow. It says “don’t print the result to my screen — write it into this file instead.”
So that command is: “Print Hello, terminal., but instead of showing it on screen, save it
to greeting.txt.”
Run ls and you’ll see greeting.txt. To peek inside, use cat:
cat greeting.txt
That prints the contents straight to your screen. (You’ll learn more file-reading tools in Level 2.)
The footgun nobody warns you about
The single > overwrites the file. If greeting.txt already had something in it, that
something is gone. Forever. No undo. No “are you sure?” No trash bin to recover from.
Use >> (two arrows) to append instead — add to the end without erasing what’s there:
echo "And another line." >> greeting.txt
Now cat greeting.txt shows both lines. The rule:
>overwrites>>adds to the end
This is one of the rules that bites everyone once. After the first time, you never forget.
What you’ve learned
touch <file>— make an empty file (or update its modified time).echo "text" > <file>— create a file with text in it. Overwrites.echo "text" >> <file>— add text to the end of a file.
You can now create files and folders. The next three lessons cover the three things you’ll do with them every day: copy, move, delete.