Create files and folders in Linux (touch, mkdir, echo)

Create empty files, folders and nested folder trees on a Linux server with touch and mkdir -p, and write text into files with echo, redirection and heredocs.

  • Beginner
  • Linux VPS
  • 4 min read

By RunRacks Team · Published

Sooner or later you need a new folder for a website, an empty log file for an app, or a short config file. On a Linux server you create all of these from the command line. This guide covers mkdir for folders, touch for empty files, and three quick ways to put text into a file without opening an editor.

Before you start

  • SSH access to your Linux VPS. See Connect to your Linux VPS with SSH.
  • Write permission in the folder where you create things. Your home folder is always fine. System folders such as /etc or /var/www usually need sudo.

Create a folder with mkdir

mkdir makes a new folder:

Terminal
mkdir projects

You can create several at once:

Terminal
mkdir logs backups scripts

Create nested folders with -p

Without options, mkdir fails if a parent folder does not exist. -p creates every missing folder along the path, and does not complain if the folder already exists:

Terminal
mkdir -p /home/deploy/apps/shop/releases

That makes -p safe to use in scripts that may run more than once.

Brace expansion lets you build a whole structure in one line:

Terminal
mkdir -p ~/site/{public,logs,backups}

This creates site/public, site/logs and site/backups.

Set permissions at creation

-m sets the permissions straight away. For a private folder only you can open:

Terminal
mkdir -m 700 ~/secrets

Create an empty file with touch

touch creates an empty file if it does not exist:

Terminal
touch app.log
touch notes.txt todo.txt

If the file already exists, touch does not change its contents. It only updates the modification time. That is why it is safe to run on existing files.

Brace expansion works here too:

Terminal
touch report-{jan,feb,mar}.txt

Write text with echo and redirection

echo prints text. The > and >> operators send that text into a file instead of the screen.

  • > creates the file, or replaces everything in it if it exists.
  • >> creates the file, or adds to the end of it.
Terminal
echo "Hello from my VPS" > hello.txt
echo "A second line" >> hello.txt
cat hello.txt
Text
Hello from my VPS
A second line

To stop > from overwriting files by accident, turn on the shell's no-clobber option:

Terminal
set -o noclobber

With it on, > refuses to overwrite an existing file. Use >| when you really do mean to overwrite.

printf is useful when you need precise formatting, such as several lines:

Terminal
printf 'line one\nline two\n' > two-lines.txt

Write several lines with a heredoc

A heredoc feeds a block of text into a command until a marker word appears. Together with cat, it writes multi-line files in one paste:

Terminal
cat > ~/site/README.txt <<'EOF'
This folder holds the shop website.
public/  - files served to visitors
logs/    - application logs
EOF

Quoting the marker ('EOF') stops the shell from expanding $variables inside the text, so what you type is exactly what gets written.

Write to a root-owned file

sudo echo "x" > /etc/file does not work: the redirection happens in your own shell, before sudo runs. Use tee instead, which runs with sudo and writes the file itself:

Terminal
echo "203.0.113.10 app.example.com" | sudo tee -a /etc/hosts

tee -a appends; plain tee overwrites. For several lines, combine tee with a heredoc:

Terminal
sudo tee /etc/myapp/config.ini > /dev/null <<'EOF'
[server]
port = 8080
EOF

> /dev/null hides the copy that tee would otherwise print to the screen.

Check it works

List what you created and look inside:

Terminal
ls -la ~/site
cat ~/site/README.txt

For a nested structure, find shows every folder at once:

Terminal
find ~/site -type d

Troubleshooting

  • `mkdir: cannot create directory: No such file or directory`: a parent folder is missing. Add -p.
  • `mkdir: cannot create directory: File exists`: a file or folder with that name is already there. Check with ls -l, or use -p if you only want to make sure the folder exists.
  • `Permission denied` with `sudo echo ... >`: the redirection runs without root. Use sudo tee as shown above.
  • `cannot overwrite existing file`: noclobber is on. Use >> to append, or >| to overwrite on purpose.
  • A heredoc never ends: the closing EOF must be alone on its line with no spaces before or after it. Press Ctrl + C and paste again.

Next steps

Was this guide helpful?

Still stuck?

Ask RunRacks AI for a quick answer, or open a ticket and our team will take a look at your server.

Getting started

Edit files with nano: a beginner's guide

Open, edit, search and save files with the nano text editor on a Linux server, including cutting and pasting lines, showing line numbers and safely editing system config files with sudo.

  • Beginner
  • Linux VPS
  • 5 min read

Getting started

Your first hour with a new RunRacks server

A step-by-step roadmap for the first hour after your RunRacks server is delivered, from finding your login details to changing the password, updating the OS and picking your next guides.

  • Beginner
  • All
  • 5 min read

Getting started

How to choose a server location

Pick a data center location based on where your users, broker or team are, confirm it with ping tests, and weigh the legal and latency points that come with each choice.

  • Beginner
  • All
  • 4 min read