terminal.cab

Terminal start Lesson 4 of 12

guestLevel 1Badges and progress0/100 XP, XP to the next levelStreak0

Creating folders and files

8 minutes

Learning goals

A new folder: mkdir

mkdir (make directory) creates a folder. Like cd, it says nothing when it works, so look with ls:

mkdir music

Purpose: Creates a new, empty folder.

Syntax: mkdir [OPTION]... FOLDER...

Options:

  • -p create missing parent folders too, no error if the folder exists

Example:

mkdir music
ls
Documents  Downloads  Pictures  music  notes.txt  projects

in the glossary: mkdir

You can also give a path: mkdir Documents/taxes creates taxes inside Documents. If the folder already exists, mkdir refuses with File exists and changes nothing.

Many folders at once: mkdir -p

mkdir projects/app/src fails, because app does not exist yet and mkdir will not create it on its own. With -p (parents) it creates every missing folder along the way:

mkdir -p projects/app/src

Purpose: Creates a whole chain of folders in one go.

Syntax: mkdir -p PATH/TO/FOLDER

Example:

mkdir -p projects/app/src
ls projects/app
src

in the glossary: mkdir

A nice side effect: mkdir -p does not complain if the folders are already there.

An empty file: touch

touch creates an empty file if the name is new. If the file exists, touch only sets its time to now and leaves the content alone.

touch Documents/ideas.txt

Purpose: Creates an empty file, or updates the time of an existing one.

Syntax: touch FILE...

Example:

touch Documents/ideas.txt
ls -l Documents/ideas.txt
-rw-r--r-- 1 lerner lerner 0 Oct 17 09:00 Documents/ideas.txt

in the glossary: touch

The size is 0: the file exists, but it is empty.

A preview: text into a file

echo prints text. With > behind it, the text does not appear on the screen but goes into a file:

echo "Buy bread" > shopping.txt

Purpose: Writes a line of text into a file. Replaces whatever was in it.

Syntax: echo TEXT > FILE

Example:

echo "Buy bread" > shopping.txt
cat shopping.txt
Buy bread

in the glossary: echo

> creates the file if it is new. If it already exists, everything in it is replaced without a question. The lesson on chaining commands covers this in depth; for now, keep it for new files.

Practice

Terminal task 1/3

Create a folder called music in your home folder.

Task 1/3: Create a folder called music in your home folder.

Create a folder called music in your home folder.

Loading terminal …

lerner@terminal: ~bash
lerner@terminal:~$

Esc, then Tab leaves the terminal

Not done yet

Terminal task 2/3

Create the folder src inside projects/app. The folder app does not exist yet. Use one command.

Task 2/3: Create the folder src inside projects/app. The folder app does not exist yet. Use one command.

Create the folder src inside projects/app. The folder app does not exist yet. Use one command.

Loading terminal …

lerner@terminal: ~bash
lerner@terminal:~$

Esc, then Tab leaves the terminal

Not done yet

Terminal task 3/3

Create an empty file ideas.txt in the folder Documents.

Task 3/3: Create an empty file ideas.txt in the folder Documents.

Create an empty file ideas.txt in the folder Documents.

Loading terminal …

lerner@terminal: ~bash
lerner@terminal:~$

Esc, then Tab leaves the terminal

Not done yet

Find the error

This command answers "invalid option". Tap the part that is wrong.

Parts of the command

Check works once you have tapped a part.

Build the command

Build the command that creates the folders 2026/summer inside Pictures, even though 2026 does not exist yet.

Your command

    Tap the blocks below to build the command.

    Building blocks

    Check works once your command has at least one block.

    Next lesson: looking inside files, from short notes to long logs.

    Quiz

    1. Question 1 of 4notes.txt already exists. What does touch notes.txt do?

      Check works once you have chosen an answer.

    2. Question 2 of 4shopping.txt contains three lines. What is in it after echo "Milk" > shopping.txt?

      Check works once you have chosen an answer.

    3. Question 3 of 4Why does mkdir projects/game/levels fail when projects/game does not exist?

      Check works once you have chosen an answer.

    4. Question 4 of 4Which commands can never destroy existing content? Choose TWO.

      Check works when exactly 2 are chosen. Chosen: 0.

    Cards

    1. Card 1 of 4

      Tap the card or press Space to turn it over.

    2. Card 2 of 4

      Tap the card or press Space to turn it over.

    3. Card 3 of 4

      Tap the card or press Space to turn it over.

    4. Card 4 of 4

      Tap the card or press Space to turn it over.

    End of the lesson

    Still open: 5 exercises, 4 quiz questions

    Next: lesson 5, Looking into files: cat, less, head and tail (10 min)

    What you can do now: mkdir music, mkdir -p projects/app/src, touch Documents/ideas.txt, echo "Buy bread" > shopping.txt

    +0 XP in this lesson

    1. Lesson 1: open
    2. Lesson 2: open
    3. Lesson 3: open
    4. Lesson 4: done
    5. Lesson 5: open
    6. Lesson 6: open
    7. Lesson 7: open
    8. Lesson 8: open
    9. Lesson 9: open
    10. Lesson 10: open
    11. Lesson 11: open
    12. Lesson 12: open
    1 of 12

    Next: lesson 5, Looking into files: cat, less, head and tail (10 min) That's enough for today

    Path complete

    You finished all 12 lessons of Terminal start.

    On to Linux Basics: lesson 1, Finding your way around a strange server (8 min) Back to the path

    Cheat sheet