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 Learn

  • Create new directories (folders) with mkdir
  • Create empty files with touch
  • Output text and write to files with echo
  • Display file contents with cat

Target Audience: Those who've learned pwd/cd/ls, ready for the next step in file operations

Introduction: Lina's Next Step

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: Absolutely! Today we'll learn four commands: mkdir (create directories), touch (create files), echo (output text), and cat (display files). Once you master these, you'll be able to create files and write content to them.

mkdir - Create Directories

Linny-senpai: First up is the mkdir command. It stands for "Make Directory" and creates new directories (folders).
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: In Linux, commands typically "say nothing when successful." Use ls to verify.
$ ls
documents  downloads  practice  pictures

Key Point: Make it a habit to verify mkdir results with ls. In Linux, "silence means success" - if there's no error, nothing is displayed.

Create Multiple Levels at Once (-p option)

Lina: I want to create lesson1 inside practice...
Linny-senpai: Great question! The -p option creates parent directories along the way.
$ mkdir -p practice/lesson1/exercises
Lina: So practice, lesson1, and exercises are all created at once!

touch - Create Empty Files

Linny-senpai: Next is the touch command. It creates empty files.
Lina: "Touch" means to touch something, right? Touching a file...?
Linny-senpai: Originally, its purpose is to "update a file's timestamp (last modified date)." But since it creates a new file if one doesn't exist, it's commonly used for file creation.

Basic Usage

$ touch memo.txt
$ ls
documents  downloads  memo.txt  practice  pictures
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 can create multiple files at once by separating filenames with spaces.

echo - Output Text

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. But this alone doesn't seem very useful...
Linny-senpai: I thought you might think that. But echo's real power is "writing to files."

Write to Files (Redirection)

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

Warning: Using > (overwrite) completely replaces the file contents. If you want to keep existing content, always use >> (append).

cat - View File Contents

Linny-senpai: Last is the cat command. Short for "concatenate" (meaning "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 entire file at once, making it handy for checking short files.

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: When you specify multiple files with cat, it concatenates and displays them. That's where the name "concatenate" comes from.

Display with Line Numbers (-n option)

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

Mini Challenges - Practice Now

Linny-senpai: Let's try some challenges using these four commands.

Challenge 1: Create a Practice Directory and Navigate Into It

Task: Create a directory called "my-project", navigate into it, and confirm your current location.

$ mkdir my-project
$ cd my-project
$ pwd
/home/user/my-project

Challenge 2: Create a README File and Write Content

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

$ 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

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.

$ mkdir -p docs/notes
$ touch docs/notes/memo1.txt docs/notes/memo2.txt
$ ls docs/notes
memo1.txt  memo2.txt
Lina: Done! I'm starting to understand the flow: create a directory, create a file, write content, then verify!
Linny-senpai: Perfect! This workflow is something you'll use constantly in real work, so make it second nature.

Common Pitfalls and Solutions

Pitfall 1: Typos in Directory Names

Lina: I typed mkdir documets by mistake... I wanted "documents."
Linny-senpai: That's a common mistake. You can recreate it or rename it later with the mv command. Make it a habit to verify with ls.

Pitfall 2: Confusing Redirection Operators

Lina: I wanted to append with echo "more" > memo.txt but I accidentally overwrote everything...
Linny-senpai: That's a really common mistake. >> (two) for append, > (one) for overwrite. When in doubt, remember "two for adding."

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 doesn't automatically create directories. First run mkdir newdir, then 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. With ls -la, entries starting with "d" are directories, and those starting with "-" are files.

Pitfall 5: Using Non-ASCII Characters in Filenames

Lina: Can I use non-ASCII characters like Japanese with mkdir?
Linny-senpai: Technically yes, but it's best to avoid it. It can cause encoding issues or make scripts harder to work with. Stick to alphanumeric characters, hyphens, and underscores for filenames.

Today's 3-Line Summary

  • mkdir creates directories; -p creates multiple levels at once
  • touch creates empty files; echo "content" > file writes to files
  • cat displays file contents; use >> for appending

Next Reading

Lina: I understand how to create files now! What should I learn next?
Linny-senpai: Next up is "copying," "moving," and "deleting" files. Once you learn cp, mv, and rm, you'll have mastered the basics of file operations. Check out File Operations Basics for details!