Create a File: The Four Common Ways
touch notes.txt
echo "first line" > notes.txt
printf 'one\ntwo\n' > notes.txt
nano notes.txt
touch creates an empty file. echo and printf with > create a file with content. nano opens an editor and creates the file when you save. Each one creates the file in the current folder unless you give a path.
An Empty File with touch
touch app.log
touch a.txt b.txt c.txt
touch /var/www/example.com/maintenance.flag
If the file does not exist, touch creates it empty. If it exists, touch leaves the content alone and only updates the modification time. That makes touch the safe choice when you are not sure whether a file is already there.
Write Content with a Redirect
Warning: > empties an existing file before it writes, without asking. Check with ls -l name first, or use >>, which appends.
echo "APP_PORT=8080" > app.env
echo "APP_ENV=production" >> app.env
printf '%s\n' "line one" "line two" > list.txt
>> adds to the end and creates the file if it is missing. printf treats backslashes and percent signs the same way everywhere, which makes it more predictable than echo in scripts. In bash, set -o noclobber makes > refuse to overwrite an existing file for the rest of the session.
Several Lines with a Here-Document
cat > backup.sh <<'EOF'
#!/bin/bash
tar -czf "/var/backups/site-$(date +%F).tar.gz" -C /var/www example.com
EOF
Every line up to the one that holds only EOF goes into the file. The quotes around the first 'EOF' keep $(date +%F) as text, so it runs later, when the script runs, not now. The cat command guide covers here-documents, and how to write and run a bash script covers what comes next.
Create and Edit in nano or vim
nano notes.txt
vi notes.txt
In nano, Ctrl+O saves and Ctrl+X quits. In vi or vim, press i to type, then Esc and :wq to save and quit; see how to save and quit in Vim. nano is usually installed on Ubuntu and Debian. On a minimal AlmaLinux or Rocky Linux system you may need sudo dnf install nano, while vi is usually there. Full vim is a separate package on minimal systems of every distribution.
A File with Set Permissions
install -m 600 /dev/null ~/.config/app/token
sudo install -m 640 -o root -g www-data /dev/null /etc/app/db.conf
install copies /dev/null, which is empty, to the new name and sets the mode, owner, and group in the same step, instead of creating the file first and running chmod and chown afterward. This suits files that will hold passwords. The parent folder must already exist.
A File of a Given Size
fallocate -l 1G test.img
truncate -s 1G sparse.img
ls -lh test.img sparse.img
du -h test.img sparse.img
fallocate reserves real disk space at once, which is useful for testing what happens when a disk fills. It works on ext4 and XFS, the usual server file systems; on a file system without that support it stops with an error. truncate creates a sparse file: ls shows 1 GB, but du shows that almost no space is used until data is written.
Files Under /etc Need sudo tee
A command such as sudo echo "vm.swappiness=10" > /etc/sysctl.d/99-swap.conf fails with "Permission denied". The redirect is handled by your own shell, as your user, before sudo starts; sudo gives root rights only to echo, which does not need them. tee writes its input to a file, so running tee with sudo works:
echo "vm.swappiness=10" | sudo tee /etc/sysctl.d/99-swap.conf > /dev/null
echo "extra=1" | sudo tee -a /etc/app/app.conf > /dev/null
-a appends instead of replacing, and > /dev/null hides the copy tee prints to the screen. Before you change an existing file in /etc, keep a copy with sudo cp -a file file.bak; see the cp command guide.
Common Errors
- Permission denied: you cannot write to that folder. Check it with
ls -ldand use sudo as shown above for system paths. - No such file or directory: the parent folder does not exist. Create it with mkdir -p.
- Is a directory: a folder with that name already exists.