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
pwd, cd, and ls down now!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
mkdir command. It stands for "Make Directory" and creates new directories (folders).Basic Usage
$ mkdir practice
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)
lesson1 inside practice...-p option creates parent directories along the way.$ mkdir -p practice/lesson1/exercises
practice, lesson1, and exercises are all created at once!touch - Create Empty Files
touch command. It creates empty files.Basic Usage
$ touch memo.txt $ ls
documents downloads memo.txt practice pictures
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 can create multiple files at once by separating filenames with spaces.
echo - Output Text
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's real power 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
Warning: Using > (overwrite) completely replaces the file contents. If you want to keep existing content, always use >> (append).
cat - View File Contents
cat command. Short for "concatenate" (meaning "to join together"), it displays file contents.echo!Basic Usage
$ cat memo.txt
First line memo Adding second line
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
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
Common Pitfalls and Solutions
Pitfall 1: Typos in Directory Names
mkdir documets by mistake... I wanted "documents."mv command. Make it a habit to verify with ls.Pitfall 2: Confusing Redirection Operators
echo "more" > memo.txt but I accidentally overwrote everything...>> (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
touch doesn't automatically create directories. First run mkdir newdir, then touch.Pitfall 4: Confusing Files and Directories
cat and got an error.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
mkdir?Today's 3-Line Summary
mkdircreates directories;-pcreates multiple levels at oncetouchcreates empty files;echo "content" > filewrites to filescatdisplays file contents; use>>for appending