Making Directories and Files

Two commands cover almost all of it.

mkdir name creates a directory.

touch name creates an empty file, or quietly updates the timestamp if it already exists.

Both take multiple arguments, so touch a.txt b.txt c.txt creates three files in one go.

The -p flag on mkdir is the one worth memorizing. By default mkdir a/b/c fails if a does not already exist, because it will only create the final level. mkdir -p a/b/c creates every missing level along the way, and does not complain if some of them already existed.

That second property is why scripts almost always use -p: running the script twice should not be an error.

SANKOFA SANDBOX
learner:~$

Try mkdir without -p on a nested path first, so you see the failure before you see the fix.

Try it → Run `mkdir writing/drafts` and read the error. Then run `mkdir -p writing/drafts` and `ls writing`. Then run the same -p command again and notice it does not complain.

✦ echo with a redirect is the fastest way to make a file with content

touch creates an empty file. To create one with something in it, echo some text and send it to a file with a greater-than sign: echo hello > note.txt. Redirection gets a full module later, but this one form is too useful to wait for.

Mark this lesson complete

Signed in, your progress follows you to every device.

Next: Removing Things, and the Command That Deserves Respect →
← Back