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
/etcor/var/wwwusually needsudo.
Create a folder with mkdir
mkdir makes a new folder:
mkdir projectsYou can create several at once:
mkdir logs backups scriptsCreate 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:
mkdir -p /home/deploy/apps/shop/releasesThat makes -p safe to use in scripts that may run more than once.
Brace expansion lets you build a whole structure in one line:
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:
mkdir -m 700 ~/secretsCreate an empty file with touch
touch creates an empty file if it does not exist:
touch app.log
touch notes.txt todo.txtIf 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:
touch report-{jan,feb,mar}.txtWrite 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.
echo "Hello from my VPS" > hello.txt
echo "A second line" >> hello.txt
cat hello.txtHello from my VPS
A second lineWarning: 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:
set -o noclobberWith 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:
printf 'line one\nline two\n' > two-lines.txtWrite 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:
cat > ~/site/README.txt <<'EOF'
This folder holds the shop website.
public/ - files served to visitors
logs/ - application logs
EOFQuoting 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:
echo "203.0.113.10 app.example.com" | sudo tee -a /etc/hoststee -a appends; plain tee overwrites. For several lines, combine tee with a heredoc:
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:
ls -la ~/site
cat ~/site/README.txtFor a nested structure, find shows every folder at once:
find ~/site -type dTroubleshooting
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 withls -l, or use-pif you only want to make sure the folder exists.Permission deniedwithsudo echo... >: the redirection runs without root. Usesudo teeas shown above.cannot overwrite existing file:noclobberis on. Use>>to append, or>|to overwrite on purpose.- A heredoc never ends: the closing
EOFmust be alone on its line with no spaces before or after it. Press Ctrl + C and paste again.
Next steps
- Edit files properly: Edit files with nano.
- Learn more about
>,>>and|: Pipes and redirection in Linux. - Control who can open your new files: Linux file permissions explained.
