File Creation Basics - Learn mkdir/touch/echo/cat
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
Table of Contents
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" (pronounced con-CAT-en-ate, 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.
Show Hint
Use mkdir, cd, and pwd in sequence.
Show Solution
$ mkdir my-project $ cd my-project $ pwd
/home/user/my-project
Note: The /home/user part will differ based on your username. As long as your username appears, you're good!
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.
Show Hint
Use touch to create the file, echo > to overwrite, echo >> to append, and cat to display.
Show Solution
$ 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.
Show Hint
Use mkdir -p to create multiple levels at once, and touch can create multiple files at once.
Show Solution
$ 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. Check and confirm.
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
What to Learn Next
cp, mv, and rm, you'll have mastered the basics of file operations. Check out File Operations Basics for details!
Master File Operations Through Practice
Once you've learned these four commands, solidify your knowledge by practicing hands-on challenges on Penguin Gym Linux.