Terminal start Lesson 4 of 12
Creating folders and files
8 minutes
Learning goals
- Create a folder with mkdir, and several nested folders at once with mkdir -p.
- Create an empty file with touch.
- Write a short text into a file with echo and >, and know why > needs care.
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:
Example:
mkdir music
ls
Documents Downloads Pictures music notes.txt projectsYou 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
srcA 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.txtThe 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> 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.
Task complete
Solution
mkdir music Why: mkdir gives you a place for new things before you have them. Order first, content later.
Another way
ls
mkdir music One line more, but you see first that the name is still free.
See the solution? You get fewer XP for this task.
Start over? Files, folders and output go back to the start. Your hints stay.
Loading terminal …
Esc, then Tab leaves the terminal
Not done yet
Repeat: you have solved this task before.
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.
Task complete
Solution
mkdir -p projects/app/src Why: Real projects have deep folder structures. mkdir -p sets them up in one line.
Another way
mkdir projects/app
mkdir projects/app/src Works too, but needs one command per level. With deep paths, -p saves a lot of typing.
See the solution? You get fewer XP for this task.
Start over? Files, folders and output go back to the start. Your hints stay.
Loading terminal …
Esc, then Tab leaves the terminal
Not done yet
Repeat: you have solved this task before.
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.
Task complete
Solution
touch Documents/ideas.txt Why: An empty file is a placeholder. Some programs only start working once a certain file exists.
See the solution? You get fewer XP for this task.
Start over? Files, folders and output go back to the start. Your hints stay.
Loading terminal …
Esc, then Tab leaves the terminal
Not done yet
Repeat: you have solved this task before.
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
Building blocks
Check works once your command has at least one block.
Next lesson: looking inside files, from short notes to long logs.
Quiz
-
Check works once you have chosen an answer.
-
Check works once you have chosen an answer.
-
Check works once you have chosen an answer.
-
Check works when exactly 2 are chosen. Chosen: 0.
Cards
-
Card 1 of 4
Tap the card or press Space to turn it over.
-
Card 2 of 4
Tap the card or press Space to turn it over.
-
Card 3 of 4
Tap the card or press Space to turn it over.
-
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
- Lesson 1: open
- Lesson 2: open
- Lesson 3: open
- Lesson 4: done
- Lesson 5: open
- Lesson 6: open
- Lesson 7: open
- Lesson 8: open
- Lesson 9: open
- Lesson 10: open
- Lesson 11: open
- Lesson 12: open
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
-
mkdir <folder>create a folder -
mkdir -p a/b/ccreate a whole path of folders -
touch <file>create an empty file -
echo "text" > <file>write text into a file, replaces its content