Creating Files and Directories with touch and mkdir
Almost every task in a terminal starts with a place to put things. The touch command creates empty files, and the mkdir command creates directories. This lesson explains how each command works, what happens when the target already exists, and how to create a whole project layout with a single line.
Understanding the Concept
A file system stores two kinds of items that you create by hand: files, which hold data, and directories, which hold other items. Creating either one means adding an entry to a parent directory. If the parent does not exist, there is nowhere to add the entry, and the command fails. That one rule explains most of the error messages in this lesson.
| Command | Creates | Fails when |
|---|---|---|
touch name | An empty file (or updates the time of an existing one) | The parent directory does not exist, or you lack write permission there |
mkdir name | A directory | The name already exists, or the parent directory does not exist |
Both commands are external programs from the GNU coreutils package, so the options below are the GNU versions found on Linux. macOS ships BSD versions that accept the common options but differ in some details.
Creating Empty Files with touch
The simplest use of touch is to give it a file name:
touch notes.txt
ls -l --time-style=long-iso notes.txt
Output:
-rw-r--r-- 1 student student 0 2026-11-20 10:52 notes.txt
The file exists and its size is 0 bytes. touch never writes any content. Its real job is to set the timestamps of a file, and creating the file when it is missing is a side effect of that job.
What Happens When the File Already Exists
If the file exists, touch leaves its content alone and changes only the modification time to the current time. This makes touch safe to run on a file that already contains data. Nothing is erased.
You can set a specific time with -d:
touch -d "2026-01-15 09:30" notes.txt
ls -l --time-style=long-iso notes.txt
Output:
-rw-r--r-- 1 student student 0 2026-01-15 09:30 notes.txt
Build tools such as make compare modification times to decide which files are out of date, which is one practical reason to adjust a time deliberately. To copy the time from another file, use -r (reference):
touch -d "2025-12-01 08:00" ref.txt
touch -r ref.txt copy_time.txt
ls -l --time-style=long-iso ref.txt copy_time.txt
Output:
-rw-r--r-- 1 student student 0 2025-12-01 08:00 copy_time.txt
-rw-r--r-- 1 student student 0 2025-12-01 08:00 ref.txt
Here copy_time.txt did not exist, so touch created it and then gave it the time of ref.txt.
Creating Several Files at Once
touch accepts any number of names:
touch a.txt b.txt c.txt
ls
Output:
a.txt b.txt c.txt
Bash can generate the names for you with brace expansion, which is performed by the shell before touch even starts. The shell turns report_{1..3}.txt into three separate words:
touch report_{1..3}.txt
ls report_*
Output:
report_1.txt report_2.txt report_3.txt
The command that actually ran was touch report_1.txt report_2.txt report_3.txt. A later lesson covers brace expansion in detail.
Updating Only Existing Files
The -c option (no create) tells touch to do nothing when the file is missing:
touch -c ghost.txt
echo "status=$?"
ls ghost.txt
Output:
status=0
ls: cannot access 'ghost.txt': No such file or directory
The status is 0, which means success, because -c makes "skip missing files" a valid outcome. The file was not created, as the ls error confirms.
When touch Fails
touch cannot create a file inside a directory that does not exist:
touch nodir/x.txt
Output:
touch: cannot touch 'nodir/x.txt': No such file or directory
The message says cannot touch rather than cannot create, because from the command's point of view it was trying to update a timestamp. The fix is to create the directory first, which is the job of mkdir.
Creating Directories with mkdir
mkdir stands for "make directory":
mkdir projects
ls
Output:
projects
Like touch, it accepts several names, and each one is created independently:
mkdir one two three
ls
Output:
one projects three two
When the Directory Already Exists
Unlike touch, plain mkdir treats an existing name as an error:
mkdir projects
echo "status=$?"
Output:
mkdir: cannot create directory 'projects': File exists
status=1
The message says File exists even for a directory, because on Linux a directory is a special kind of file. The non-zero status matters in scripts, where a failed mkdir should usually stop the script.
Creating Parent Directories with -p
Without -p, mkdir creates only the last component of the path, and every earlier component must already exist:
mkdir a/b/c
echo "status=$?"
Output:
mkdir: cannot create directory 'a/b/c': No such file or directory
status=1
The option -p (parents) creates every missing directory along the path:
mkdir -p a/b/c
echo "status=$?"
ls -R a
Output:
status=0
a:
b
a/b:
c
a/b/c:
The option -p has a second effect that makes it very popular in scripts: it does not complain when the directory already exists.
mkdir -p projects
echo "status=$?"
Output:
status=0
So mkdir -p means "make sure this directory exists", which can be run any number of times with the same result. A script that behaves this way is called idempotent.
Seeing What Was Created with -v
The -v option (verbose) prints a line for each directory that mkdir creates:
mkdir -v v1 v2
mkdir -pv deep/er/still
Output:
mkdir: created directory 'v1'
mkdir: created directory 'v2'
mkdir: created directory 'deep'
mkdir: created directory 'deep/er'
mkdir: created directory 'deep/er/still'
With -p, you can see every level that was added, which is helpful when you are not sure how much of a path already existed.
Permissions of New Items
Every new item receives permissions that depend on a setting called the umask. Print it with umask:
umask
Output:
0022
The umask lists permission bits to remove. New directories start from 777 and new files from 666. Removing 022 leaves 755 for directories (rwxr-xr-x) and 644 for files (rw-r--r--), which matches the -rw-r--r-- line you saw earlier.
mkdir -m sets the permissions of a new directory directly. This example creates a directory that only the owner can enter:
mkdir -m 700 private
ls -ld private projects
Output:
drwx------ 2 student student 4096 2026-11-20 10:52 private
drwxr-xr-x 2 student student 4096 2026-11-20 10:52 projects
The directory private shows drwx------, and projects shows the default drwxr-xr-x. A later lesson explains permission strings fully.
Names That Contain Spaces
The shell splits a command line into words at every space. A name with a space therefore needs quotes, or it becomes two arguments:
touch "my file.txt"
mkdir "my folder"
mkdir my other folder
ls
Output:
folder my my file.txt my folder other
The first two commands each created one item with a space in its name: the file my file.txt and the directory my folder. The third command had no quotes, so mkdir received three separate arguments and created three directories named my, other and folder. Quoting the name, or escaping each space with a backslash as in my\ folder, is the reliable way to avoid this surprise.
Practical Applications
Creating a Project Skeleton
Brace expansion and mkdir -p combine well. This creates a directory tree for a small project:
mkdir -p myapp/{src,tests,docs}
touch myapp/README.md myapp/src/main.py myapp/tests/test_main.py
find myapp | sort
Output:
myapp
myapp/README.md
myapp/docs
myapp/src
myapp/src/main.py
myapp/tests
myapp/tests/test_main.py
The first command expands into mkdir -p myapp/src myapp/tests myapp/docs. Because of -p, myapp is created automatically along the way. The second command then fills the structure with empty starter files.
Braces can be nested to create a grid of directories:
mkdir -p src/{main,test}/{java,resources}
find src | sort
Output:
src
src/main
src/main/java
src/main/resources
src/test
src/test/java
src/test/resources
Each of the two choices in the first brace pair is combined with each of the two choices in the second pair, giving four leaf directories.
Making a Script Safe to Run Twice
A setup script often needs a directory for its output. Using mkdir -p means the script works on the first run and on every run after that:
mkdir -p "$HOME/backups/2026"
If the script had used plain mkdir, the second run would print File exists and return a failing status.