How to Use mkdir, touch, and echo - Create Files in Linux

How to Use mkdir, touch, and echo - Create Files in Linux

What you'll be able to do

  • Create directories with `mkdir`, and nested levels with `-p`
  • Create empty files with `touch`, one or many at a time
  • Explain the difference between `echo >` and `echo >>`, and check the result with `cat`

Prerequisites (read these first)

What You'll Learn

Conclusion: mkdir, touch, echo, and cat cover the full cycle of creating and checking files.

  • Create new directories with mkdir
  • Create empty files with touch
  • Output text with echo. You can also write it into a file
  • Display file contents with cat

Target Audience: Readers who already know pwd, cd, and ls. Readers ready for the next step in file operations.

Words used in this article

The mkdir, touch, and cat commands in this article never delete a file. But >, used with echo, replaces a file's contents. The "Write to Files" section explains that. At the very end, the article also uses rm to remove the practice directory.

The virtual terminal on this site is for learning. Your own computer will not break, so try things freely.

Introduction: Lina's Next Step

Conclusion: After pwd/cd/ls, learn to create files and write content using four commands.

Lina: Linny-senpai! I've got pwd, cd, and ls down now!
Linny-senpai: Awesome! Let's move to the next step then. This time, we'll learn how to "create files."
Lina: Create files... On Windows, I'd right-click and select "New." Can I do that with commands too?
Linny-senpai: You can. Today we'll learn four commands: mkdir (create directories), touch (create files), echo (output text), and cat (display files).
Linny-senpai: With those four, you can create a file on your own. You can write content into it too.

mkdir - Create Directories

Conclusion: mkdir creates directories; use -p for nested paths and ls to confirm the result.

Linny-senpai: First up is the mkdir command. It stands for "Make Directory". It creates a new directory.
Lina: A directory is like a box that holds files, right?
Linny-senpai: Exactly. Let's create a practice directory first.

Basic Usage

$ mkdir practice
Lina: Huh? Nothing showed up. Is that okay?
Linny-senpai: It is fine. In Linux, commands usually say nothing when they succeed.
Linny-senpai: Use ls when you want to check the result.
$ ls
documents  downloads  pictures  practice

Key Point: Make it a habit to check mkdir results with ls. In Linux, silence means success. If there is no error, nothing is displayed.

Create Multiple Levels at Once (-p option)

Lina: I want to create lesson1 inside practice.
Linny-senpai: Great question. Use the -p option. It creates the parent directories along the way.
$ mkdir -p practice/lesson1/exercises
Lina: So practice, lesson1, and exercises are all created at once.
Linny-senpai: Right. Without -p, the command fails when practice does not exist yet. -p tells mkdir that missing parents are fine to create.

touch - Create Empty Files

Conclusion: touch creates empty files; space-separate names to create multiple at once.

Linny-senpai: Next is the touch command. It creates empty files.
Lina: "Touch" means to touch something, right? Touching a file...?
Linny-senpai: Its original purpose is to update a file's timestamp. A timestamp is the date and time a file was last modified.
Linny-senpai: But it creates the file when that file does not exist yet. That is why it is often used to create files.

Basic Usage

$ touch memo.txt
$ ls
documents  downloads  memo.txt  pictures  practice
Lina: memo.txt was created! But it's empty inside, right?
Linny-senpai: Right. To write content, we'll use the next command: echo.

Create Multiple Files at Once

$ touch file1.txt file2.txt file3.txt
$ ls
file1.txt  file2.txt  file3.txt  memo.txt  ...

Key Point: touch accepts several filenames separated by spaces. It then creates all of them at once.

echo - Output Text

Conclusion: echo outputs text; use > to overwrite and >> to append content to files.

Linny-senpai: The echo command outputs text strings. As the name suggests (like an echo), it returns whatever you input.

Display Text on Screen

$ echo Hello
Hello
$ echo "Hello, Linux World!"
Hello, Linux World!
Lina: The text I type just appears on screen. That alone does not seem very useful.
Linny-senpai: I thought you might say that. The real power of echo is writing to files.

Write to Files (Redirection)

$ echo "This is a memo" > memo.txt
Lina: What's the > symbol?
Linny-senpai: That is called redirection. It sends text that would normally appear on screen into a file instead.
Linny-senpai: Think of pointing a water faucet somewhere else. > means overwrite, and >> means append.
$ echo "First line memo" > memo.txt
$ echo "Adding second line" >> memo.txt

cat - View File Contents

Conclusion: cat displays file contents at once; -n adds line numbers for multi-line files.

Linny-senpai: Last is the cat command. It is short for "concatenate", which means to join together. It displays file contents.
Lina: I want to see what I wrote with echo.

Basic Usage

$ cat memo.txt
First line memo
Adding second line
Lina: Both lines are showing.
Linny-senpai: cat displays the whole file at once. That makes it handy for short files.

Lina Gets Stuck: Two Lines Became One

Lina: I wanted a third line, so I ran echo "Third line memo" > memo.txt. Then cat showed only one line.
$ echo "Third line memo" > memo.txt
$ cat memo.txt
Third line memo
Lina: Wait, where did the first two lines go? I meant to add a line.
Linny-senpai: > means overwrite. It emptied the file first, then wrote the new line.
Lina: So the lines were not lost by a typo. One symbol changed the whole meaning.
Linny-senpai: Exactly. Use >> when you want to append. Remember it as "two symbols means adding".
Lina: That makes sense. I will also check the file with cat before I write.
$ echo "First line memo" > memo.txt
$ echo "Adding second line" >> memo.txt
$ echo "Third line memo" >> memo.txt
$ cat memo.txt
First line memo
Adding second line
Third line memo

Concatenate and Display Multiple Files

$ echo "Contents of file 1" > file1.txt
$ echo "Contents of file 2" > file2.txt
$ cat file1.txt file2.txt
Contents of file 1
Contents of file 2

Key Point: cat accepts several files. It prints them one after another in the order you list them. That is where the name "concatenate" comes from.

Display with Line Numbers (-n option)

$ cat -n memo.txt
     1  First line memo
     2  Adding second line
     3  Third line memo

Mini Challenges - Practice Now

Conclusion: Combine mkdir, touch, echo, and cat to practice the full file-creation workflow.

Linny-senpai: Let's try some challenges using these four commands.
Linny-senpai: First make a practice directory and stay inside it. That way you never touch your existing files.
$ mkdir -p ~/file-practice && cd ~/file-practice

Challenge 1: Create a Practice Directory and Navigate Into It

Task: Create a directory called "my-project". Move into it, then show your current location.

Show Hint 1 (Direction)

Split this into three actions: create, move, and check where you are. Each action uses a different command.

Show Hint 2 (Command name)

Create with mkdir. Move with cd. Show your location with pwd. Use them in that order.

Show Answer
$ mkdir my-project
$ cd my-project
$ pwd
/home/user/file-practice/my-project

The /home/user part changes with your username. Seeing your own username means it worked.

Challenge 2: Create a README File and Write Content

Task: Create an empty file called "README.txt". Write a project name and a creation date into it, then display the contents.

Show Hint 1 (Direction)

Split this into four actions: create the empty file, write the first line, add the second line, and view the result.

For the second line, pick the symbol that does not erase the first line.

Show Hint 2 (Command name)

Create with touch. Write with echo. Use > for the first line and >> for the second. View with cat.

Show Answer
$ touch README.txt
$ echo "Project Name: My First Project" > README.txt
$ echo "Created: 2026-02-02" >> README.txt
$ cat README.txt
Project Name: My First Project
Created: 2026-02-02

Writing the second line with > would erase the first line. That is the difference between > and >>.

Challenge 3: Create Subdirectories and Organize Files

Task: Create a two-level directory "docs/notes" in one command. Create "memo1.txt" and "memo2.txt" inside it, then list the files.

Show Hint 1 (Direction)

You do not need two commands for docs and notes. One option creates the parent level too.

You do not need two commands for the files either.

Show Hint 2 (Command name)

Use mkdir -p for the directories. Pass both filenames to touch, separated by a space. List them with ls.

Show Answer
$ mkdir -p docs/notes
$ touch docs/notes/memo1.txt docs/notes/memo2.txt
$ ls docs/notes
memo1.txt  memo2.txt
Lina: Done. I am starting to see the flow: create a directory, create a file, write content, then verify.
Linny-senpai: Perfect. You will use this flow constantly in real work, so keep practising it by hand.

Common Pitfalls and Solutions

Conclusion: Know the common pitfalls: typos, wrong redirect operator, and mixing files/dirs.

Pitfall 1: Typos in Directory Names

Lina: I typed mkdir documets by mistake. I wanted "documents".
Linny-senpai: That is a common mistake. You can create it again, or rename it with the mv command.
Linny-senpai: Make it a habit to check the result with ls.

Pitfall 2: Confusing Redirection Operators

Lina: I wanted to append with echo "more" > memo.txt, but I overwrote everything.
Linny-senpai: That is a really common mistake. Use >> (two symbols) to append and > (one symbol) to overwrite.
Linny-senpai: When in doubt, remember "two for adding". If you are unsure, run cat first.

Pitfall 3: Trying to Create a File in a Non-existent Directory

$ touch newdir/file.txt
touch: cannot touch 'newdir/file.txt': No such file or directory
Linny-senpai: touch does not create directories for you. Run mkdir newdir first, then run touch.

Pitfall 4: Confusing Files and Directories

Lina: I tried to view a directory's contents with cat and got an error.
Linny-senpai: cat shows file contents. For directory contents, use ls.
Linny-senpai: There is an easy way to tell them apart. In ls -la, a line starting with d is a directory and a line starting with - is a file.

Pitfall 5: Using Non-ASCII Characters in Filenames

Lina: Can I use non-ASCII characters like Japanese with mkdir?
Linny-senpai: You can, but it is best to avoid it. It can cause encoding problems. It also makes scripts harder to write.
Linny-senpai: Stick to letters, numbers, hyphens, and underscores in filenames.

Review

Lina: Let me sum up. mkdir makes directories and touch makes files.
Linny-senpai: Right. With -p, the parent levels are created too.
Lina: Writing uses echo. > overwrites and >> appends. My file kept one line because I used >.
Linny-senpai: Well remembered. Run cat before you write. That one habit prevents most of these mistakes.

Today's 3-Line Summary

Conclusion: mkdir, touch, echo, and cat: a 3-line recap to lock in the four core commands.

  1. mkdir creates directories, and -p creates the parent levels at once
  2. touch creates empty files, and echo "content" > file writes into them
  3. > overwrites and >> appends. Check the file with cat before you write

Next Reading

Conclusion: Next: learn cp, mv, and rm to complete the full set of basic file operations.

Lina: I understand how to create files now. What should I learn next?
Linny-senpai: Next up is copying, moving, and deleting files. That means cp, mv, and rm.
Linny-senpai: Those three complete the basics of file operations. File Operations Basics covers them in detail.
Share this article

Next steps