Getting started

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.

BeginnerLinux server4 min read

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:

Bash
mkdir projects

You can create several at once:

Bash
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:

Bash
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:

Bash
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:

Bash
mkdir -m 700 ~/secrets

Create an empty file with touch

touch creates an empty file if it does not exist:

Bash
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:

Bash
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.
Bash
echo "Hello from my VPS" > hello.txt
echo "A second line" >> hello.txt
cat hello.txt
Hello from my VPS
A second line

Warning: A single > silently wipes an existing file. Typing > when you meant >> on a config or log file destroys its contents. Double-check before pressing Enter.

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

Bash
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:

Bash
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:

Bash
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:

Bash
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:

Bash
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.

Tip: Back up a system file before you write to it: sudo cp /etc/hosts /etc/hosts.bak.

Check it works

List what you created and look inside:

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

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

Bash
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 MdmHost AI for a quick answer, or open a ticket and our team will take a look. We are here 24/7.