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
- DirectoryA container that organizes files. Same idea as a "folder" on Windows or macOS.: the same thing Windows and macOS call a folder. "Folder" and "directory" are two names for one thing.
- TerminalAn interactive program that reads the commands you type and runs them.: the screen where you type commands. "Shell", "console", and "command line" refer to almost the same thing.
- Redirection: a way to send a command's output into a file instead of the screen. It uses the symbols
>and>>.
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.
pwd, cd, and ls down now!mkdir (create directories), touch (create files), echo (output text), and cat (display files).mkdir - Create Directories
Conclusion: mkdir creates directories; use -p for nested paths and ls to confirm the result.
mkdir command. It stands for "Make Directory". It creates a new directory.Basic Usage
$ mkdir practice
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)
lesson1 inside practice.-p option. It creates the parent directories along the way.$ mkdir -p practice/lesson1/exercises
practice, lesson1, and exercises are all created at once.-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.
touch command. It creates empty files.Basic Usage
$ touch memo.txt $ ls
documents downloads memo.txt pictures practice
memo.txt was created! But it's empty inside, right?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.
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!
echo is writing to files.Write to Files (Redirection)
$ echo "This is a memo" > memo.txt
> symbol?> means overwrite, and >> means append.$ echo "First line memo" > memo.txt $ echo "Adding second line" >> memo.txt
> clears the file before writing
With >, the contents of that file are replaced completely. Unlike a GUI, the old contents do not go to a trash folder. You cannot bring them back easily.
Remember three habits that keep this safe.
- Create a practice directory and work only inside it (for example,
mkdir -p ~/file-practice && cd ~/file-practice) - Run
cat filenamebefore writing, to see what is there now - Use
>>(two symbols) instead of>(one symbol) when you want to keep the existing lines
The virtual terminal on this site is for learning. No file on your own computer is deleted here, so try things freely.
cat - View File Contents
Conclusion: cat displays file contents at once; -n adds line numbers for multi-line files.
cat command. It is short for "concatenate", which means to join together. It displays file contents.echo.Basic Usage
$ cat memo.txt
First line memo Adding second line
cat displays the whole file at once. That makes it handy for short files.Lina Gets Stuck: Two Lines Became One
echo "Third line memo" > memo.txt. Then cat showed only one line.$ echo "Third line memo" > memo.txt $ cat memo.txt
Third line memo
> means overwrite. It emptied the file first, then wrote the new line.>> when you want to append. Remember it as "two symbols means adding".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.
$ 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
Common Pitfalls and Solutions
Conclusion: Know the common pitfalls: typos, wrong redirect operator, and mixing files/dirs.
Pitfall 1: Typos in Directory Names
mkdir documets by mistake. I wanted "documents".mv command.ls.Pitfall 2: Confusing Redirection Operators
echo "more" > memo.txt, but I overwrote everything.>> (two symbols) to append and > (one symbol) to overwrite.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
touch does not create directories for you. Run mkdir newdir first, then run touch.Pitfall 4: Confusing Files and Directories
cat and got an error.cat shows file contents. For directory contents, use ls.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
mkdir?Review
mkdir makes directories and touch makes files.-p, the parent levels are created too.echo. > overwrites and >> appends. My file kept one line because I used >.cat before you write. That one habit prevents most of these mistakes.Cleanup uses rm, and rm is different from everything else here
A file removed with rm does not go to a trash folder the way it does in a GUI. It is gone immediately and cannot be recovered.
-r: removes a directory together with its contents-i: asks about each item before removing it
With -i, a confirmation appears for every target. Type y and press Enter to remove it, or type n to keep it.
Before deleting, always check the pathA string that describes the location of a file or directory. and contents with ls.
$ cd ~ $ ls ~/file-practice $ rm -ri ~/file-practice
The virtual terminal on this site is for learning. Running this here deletes no file on your own computer, so try it freely.
Today's 3-Line Summary
Conclusion: mkdir, touch, echo, and cat: a 3-line recap to lock in the four core commands.
mkdircreates directories, and-pcreates the parent levels at oncetouchcreates empty files, andecho "content" > filewrites into them>overwrites and>>appends. Check the file withcatbefore you write