How To Create Empty File In Linux – Creating Zero Byte Files

Generating an empty file in Linux can be done quickly with the touch command and no additional parameters. This guide shows you exactly How To Create Empty File In Linux using multiple methods, from the simplest to more advanced techniques. Whether you’re a beginner or a seasoned sysadmin, you’ll find the right approach for your workflow.

How To Create Empty File In Linux

The most common way to create an empty file is using the touch command. It’s built into nearly every Linux distribution and requires no extra software.

Using The Touch Command

Open your terminal and type:

touch filename.txt

This creates a zero-byte file named filename.txt in your current directory. If the file already exists, touch updates its timestamp without changing its content.

You can create multiple empty files at once:

touch file1.txt file2.txt file3.txt

Or use brace expansion for numbered files:

touch file{1..10}.txt

This creates file1.txt through file10.txt, all empty.

Using Output Redirection

Another quick method uses the shell’s redirection operators. These send nothing to a new file, creating an empty one.

With Greater-Than Symbol

> emptyfile.txt

This works in bash, zsh, and most POSIX shells. Be careful: if the file exists, it will be truncated to zero bytes.

With Echo And Redirection

echo -n > emptyfile.txt

The -n flag prevents echo from adding a newline, so the file stays empty. Without it, echo adds a single newline character, making the file 1 byte.

With Colon Command

: > emptyfile.txt

The colon is a built-in shell command that does nothing. Combined with redirection, it creates an empty file.

Using The Cat Command

Cat can also create empty files by reading from /dev/null:

cat /dev/null > emptyfile.txt

Or using the > operator alone with cat:

cat > emptyfile.txt

Then press Ctrl+D to close the input stream. This creates an empty file, but it’s less efficient than touch.

Using The Truncate Command

The truncate command is designed for resizing files. To create an empty file:

truncate -s 0 emptyfile.txt

This sets the file size to zero. If the file doesn’t exist, it creates it. Truncate is part of the coreutils package and is available on most systems.

Using The Install Command

The install command, typically used for copying files with permissions, can also create empty files:

install -m 644 /dev/null emptyfile.txt

This creates an empty file with permissions 644 (readable by all, writable by owner). Adjust the mode as needed.

Using The Mktemp Command

For temporary files, mktemp is ideal:

mktemp /tmp/empty.XXXXXX

This creates a uniquely named empty file in /tmp. The X’s are replaced with random characters. Use --tmpdir to specify a different directory.

Using Scripting Languages

If you’re writing scripts, you can use built-in commands from bash, Python, or Perl.

In Bash Scripts

#!/bin/bash
touch "$1"

Or using redirection:

#!/bin/bash
> "$1"

In Python

#!/usr/bin/env python3
open('emptyfile.txt', 'w').close()

This opens the file for writing and immediately closes it, creating an empty file.

In Perl

#!/usr/bin/perl
open(my $fh, '>', 'emptyfile.txt') or die;
close($fh);

Creating Empty Files With Specific Permissions

Sometimes you need an empty file with particular permissions. Use umask before touch:

umask 000
touch worldreadable.txt
umask 022

Or combine with install as shown earlier. For more control, use chmod after creation:

touch script.sh
chmod 755 script.sh

Creating Empty Files In Specific Directories

Specify the full path to create a file elsewhere:

touch /home/user/docs/notes.txt

If the directory doesn’t exist, you’ll get an error. Use mkdir -p first:

mkdir -p /home/user/docs
touch /home/user/docs/notes.txt

Creating Empty Files With Timestamps

The touch command can set specific timestamps:

touch -t 202501011200.00 oldfile.txt

This creates an empty file with a modification time of January 1, 2025 at 12:00. Use -a for access time only.

Creating Empty Files Using Find And Xargs

For batch operations, combine find with xargs:

find . -type d -name "logs" -exec touch {}/empty.log \;

This creates an empty.log file in every directory named logs under the current path.

Creating Empty Files With A Specific Size

While not strictly empty, you can create files with a specific size using dd:

dd if=/dev/zero of=zerofile.txt bs=1 count=0

This creates a zero-byte file. For a 1MB file of zeros:

dd if=/dev/zero of=1MBfile.txt bs=1M count=1

But that’s not empty—it’s filled with null bytes.

Common Mistakes And Pitfalls

Here are some errors you might encounter:

  • Permission denied: You don’t have write access to the directory. Use sudo or change to a writable location.
  • File exists: Using > truncates existing files. Use touch if you want to preserve content.
  • No such file or directory: The parent directory doesn’t exist. Create it first with mkdir -p.
  • Read-only filesystem: You’re on a read-only partition. Mount it as read-write or use a different location.

When To Use Each Method

Choose the method based on your needs:

  • Touch: Best for everyday use, creating single or multiple files.
  • Redirection: Quick and shell-native, but overwrites existing files.
  • Cat /dev/null: Useful in scripts where you want to ensure the file is empty.
  • Truncate: Good for setting exact sizes, including zero.
  • Install: When you need specific permissions from the start.
  • Mktemp: For temporary files with unique names.

Performance Considerations

All methods are fast, but touch is the most efficient for creating empty files. Redirection operators are also fast because they’re built into the shell. For thousands of files, use touch with brace expansion or a loop.

Creating Empty Files In Different Shells

Most methods work across bash, zsh, fish, and sh. However, brace expansion is bash-specific. In sh, use a loop:

for i in 1 2 3 4 5; do
    touch "file$i.txt"
done

Automating With Cron Jobs

You can schedule empty file creation using cron. For example, to create a log file every day at midnight:

0 0 * * * touch /var/log/daily_report.txt

Add this to your crontab with crontab -e.

Using Aliases For Faster Workflows

Create an alias in your .bashrc or .zshrc:

alias mkempty='touch'

Then type mkempty newfile.txt to create an empty file. Or create a function for more complex needs:

mkempty() {
    for file in "$@"; do
        : > "$file"
    done
}

Creating Empty Files With A Graphical Interface

If you prefer a GUI, use your file manager. In Nautilus (GNOME), right-click and select “New Document” then “Empty Document.” In Dolphin (KDE), right-click and choose “Create New” then “Text File.” In Thunar (XFCE), right-click and select “Create Document” then “Empty File.”

These actions typically create a file with a default name like “Untitled Document.” You can rename it immediately.

Using The Fallocate Command

Fallocate is used for preallocating space, but can create empty files:

fallocate -l 0 emptyfile.txt

This creates a zero-byte file. Fallocate is faster than dd for large files but works similarly for empty ones.

Creating Empty Files In Containers And VMs

Inside Docker containers or virtual machines, the same commands work. Ensure you have the necessary permissions. In minimal containers, you might need to install coreutils first:

apt-get update && apt-get install -y coreutils

Or use the built-in shell redirection if touch isn’t available.

Security Considerations

Creating empty files is generally safe, but be aware of:

  • Overwriting: Using > or truncate can destroy existing data. Double-check your file paths.
  • Permissions: Files created with touch inherit the umask. Use install or chmod for sensitive files.
  • Symbolic links: If you create a file where a symlink exists, you might overwrite the target.

Troubleshooting Common Issues

If touch doesn’t work, check if the filesystem is full. Use df -h to see available space. Also verify you’re not in a restricted directory like /proc or /sys.

If redirection fails with “ambiguous redirect,” check for spaces in the filename. Quote the filename:

> "my file.txt"

Advanced: Creating Empty Files With Extended Attributes

You can set extended attributes when creating files:

touch emptyfile.txt
setfattr -n user.comment -v "This is an empty file" emptyfile.txt

This adds metadata without affecting the file’s content.

Using The Head Command

Head can create an empty file by reading zero lines:

head -c 0 /dev/null > emptyfile.txt

Or simply:

head -c 0 > emptyfile.txt

Using The Tee Command

Tee normally writes to both stdout and files. To create an empty file:

echo -n | tee emptyfile.txt > /dev/null

This sends nothing to the file and suppresses stdout.

Creating Empty Files In Different File Systems

All methods work on ext4, XFS, Btrfs, ZFS, and most Linux filesystems. On network filesystems like NFS, ensure you have write permissions. On FUSE filesystems like sshfs, latency might be higher but commands remain the same.

Using The Cp Command

Copy from /dev/null to create an empty file:

cp /dev/null emptyfile.txt

This is equivalent to cat /dev/null > emptyfile.txt.

Creating Empty Files With A Specific Owner

Use sudo and chown after creation:

sudo touch /etc/config.ini
sudo chown root:root /etc/config.ini

Or combine with install:

sudo install -o root -g root -m 644 /dev/null /etc/config.ini

Using The Dd Command

Dd can create an empty file with a specified block size:

dd if=/dev/null of=emptyfile.txt bs=1 count=0

This is overkill for empty files but shows dd’s flexibility.

Creating Empty Files In Scripts With Error Handling

Always check for errors in scripts:

if touch /path/to/file; then
    echo "File created successfully"
else
    echo "Error creating file" >&2
    exit 1
fi

This ensures your script fails gracefully.

Using The Mkfifo Command

Mkfifo creates a named pipe, not a regular file, but it’s empty:

mkfifo mypipe

This creates a FIFO special file with zero size. Use ls -l to see the ‘p’ permission indicator.

Creating Empty Files With Compression

You can create an empty compressed file:

gzip -c /dev/null > emptyfile.gz

This creates a valid gzip file containing no data.

Using The Ln Command

Ln creates links, but you can create a hard link to an empty file:

touch original.txt
ln original.txt link.txt

Both files point to the same inode and are empty.

Creating Empty Files In A Loop

For dynamic file names, use a while loop:

i=1
while [ $i -le 10 ]; do
    touch "file$i.txt"
    i=$((i + 1))
done

Using The Printf Command

Printf can create an empty file:

printf '' > emptyfile.txt

This prints nothing and redirects to the file.

Creating Empty Files With A Specific Modification Time

Use touch -t as shown earlier, or use date for dynamic timestamps:

touch -t $(date -d "yesterday" +%Y%m%d%H%M.%S) oldfile.txt

Using The Tempfile Command

Some systems have tempfile for creating temporary files:

tempfile -d /tmp -p empty

This creates a temporary empty file with a name like /tmp/empty.XXXX.

Creating Empty Files With Extended Logging

Use script to log your session while creating files:

script -q /dev/null -c "touch emptyfile.txt"

This runs touch without recording output.

Using The Xargs Command

Xargs can create multiple empty files from a list:

echo "file1.txt file2.txt file3.txt" | xargs