How to Create a File in Linux

Linux provides several command-line tools for creating files, from a simple one-word command to full text editors. To create a file in a directory, you need write permission on that parent directory; otherwise you will receive a “Permission denied” error.
This guide explains the most common ways to create files in Linux from the command line.
Quick Reference
| Method | Command | Creates with content? |
|---|---|---|
touch | touch file.txt | No (empty file) |
| Redirection | > file.txt | No (empty file) |
echo | echo "text" > file.txt | Yes |
cat | cat > file.txt | Yes (interactive) |
printf | printf "text" > file.txt | Yes |
| Heredoc | cat << EOF > file.txt | Yes (multi-line) |
nano | nano file.txt | Yes (editor) |
vim | vim file.txt | Yes (editor) |
Large file (fallocate) | fallocate -l 1G file | No (allocated space) |
Large file (dd) | dd if=/dev/zero of=file bs=1 count=0 seek=1G | No (allocated space) |
Using touch
The simplest way to create a new empty file is the touch command
:
touch file1.txtIf file1.txt does not exist, touch creates it as an empty file. If it already exists, touch updates its timestamps without changing the content.
To create multiple files at once, separate the names with spaces:
touch file1.txt file2.txt file3.txtTo create a file in another directory, provide the full path:
touch /home/zoe/Documents/file.txtUsing the Redirection Operator
The output redirection operator
> creates a new file or overwrites an existing one. To create an empty file, use it with no command before it:
> file1.txtThis is the shortest command to create an empty file in Linux.
The >> operator appends to a file rather than overwriting it. Both operators create the file if it does not exist.
Be careful not to overwrite an important existing file when using >.
Using cat
The cat command
is primarily used to read and concatenate files, but it can also create files interactively.
To create a new file, run cat with the redirection operator and the file name:
cat > file1.txtPress Enter and type the text you want to write. When you are done, press Ctrl+D to save and exit. To create an empty file, press Ctrl+D immediately without typing anything.
Using echo
The echo command
prints text to standard output. Redirect that output to a file to create it with content in a single command:
echo "Some line" > file1.txtTo create an empty file with echo, run it without arguments:
echo > file1.txtUsing printf
The printf command
is similar to echo but gives more control over output formatting. Redirect its output to create a file:
printf "Open issues: %s\nClosed issues: %s\n" "12" "25" > file.txtUsing Heredoc
A here document (heredoc) passes multiple lines of input to a command. This method is useful for creating files with multi-line content from a shell script:
cat << EOF > file1.txt
Some line
Some other line
EOFThe body of the heredoc can contain variables, special characters, and command substitutions.
Using a Text Editor
Text editors let you create and edit files interactively.
nano
Nano is a straightforward command-line editor available on most Linux systems.
To create a new file, run nano followed by the file name:
nano file.txtA new editor window opens where you can type your content.

When you are done, press Ctrl+X to exit. Nano will ask whether to save changes — type y, confirm the file name, and press Enter to save and exit
.
Vim
Vim
is preinstalled on many Linux distributions, but minimal images may include only vi or no editor by default.
To create a file with Vim, run:
vim file.txtPress i to enter insert mode and start typing. When you are done, press Esc to return to normal mode, then type :wq and press Enter to save the file and exit
.

Creating a Large File for Testing
For transfer tests, storage allocation checks, or application testing, you may need to create a large file quickly.
Using fallocate
fallocate is a command-line utility that allocates disk space for a file without writing data:
fallocate -l 1G 1G.testThis creates a 1G.test file of exactly 1 GB almost instantly by allocating space. It is useful when you need a file of a specific size, but it is not a reliable write-throughput benchmark.
Using dd
The dd command copies and converts data. To create a 1 GB sparse file:
dd if=/dev/zero of=1G.test bs=1 count=0 seek=1GFor a real write test file, write actual data instead of creating a sparse file:
dd if=/dev/zero of=1G-write.test bs=1M count=1024 status=progressTroubleshooting
Permission denied when creating a file
You do not have write permission on the target directory. Check with ls -ld /path/to/dir and use a writable location or sudo when appropriate.
Read-only file system error
The target filesystem is mounted read-only. Verify mount options with mount or findmnt, then remount as read-write if needed.
Existing file was overwritten by >
The > operator truncates existing files immediately. Use >> to append, or enable shell safeguards such as set -o noclobber in scripts.
fallocate fails with Operation not supported
Some filesystems do not support preallocation. Use the dd method to create the file instead.
FAQ
Which method should I use to create an empty file?
Use touch file.txt. It is the most readable and idiomatic way to create an empty file in Linux. The > operator also works but is less obvious to readers of a script.
Which method should I use to create a file with content?
For a single line from a script or terminal, use echo "text" > file.txt. For multi-line content in a script, use a heredoc with cat. For interactive editing, use nano or vim.
What is the difference between > and >>?> overwrites the file if it exists, or creates it if it does not. >> appends to the file if it exists, or creates it if it does not. Use >> when you want to add content without losing existing data.
How do I create a file in a directory I do not own?
Use sudo: sudo touch /etc/newfile. You need sufficient privileges to write to the target directory.
How do I create multiple files with a pattern?
Use brace expansion: touch file{1..5}.txt creates file1.txt through file5.txt in one command.
Conclusion
The fastest way to create an empty file in Linux is touch filename. To create a file with content in one command, use echo "text" > filename. For multi-line content in scripts, use a heredoc. For interactive editing, use nano or vim.
Linuxize Weekly Newsletter
A quick weekly roundup of new tutorials, news, and tips.
About the authors

Dejan Panovski
Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.
View author page