Using the touch command in the Linux terminal creates an empty file with the specified name. This is the fastest way to learn how to make a file in linux terminal. You don’t need a text editor or any special software.
Linux gives you many ways to create files from the command line. Each method has its own use case. Some commands create empty files, while others let you add content right away. This guide covers all the essential techniques.
How To Make A File In Linux Terminal
Before we jump into the details, let me show you the most common commands. You will use these everyday as a Linux user. The terminal might look scary at first, but file creation is actually quite simple.
Think of the terminal as your direct line to the operating system. Every command you type gets executed instantly. No clicking, no dragging, just pure speed and control.
Using The Touch Command
The touch command is the simplest way to create an empty file. It updates the timestamp of an existing file, or creates a new one if it doesn’t exist.
Here is the basic syntax:
touch filename.txt
This creates a file called filename.txt in your current directory. If the file already exists, touch just updates its last modified time.
You can create multiple files at once:
touch file1.txt file2.txt file3.txt
This is great for setting up project structures quickly. You can create a whole batch of files in one command.
Touch Command Options
The touch command has a few useful options:
-achanges only the access time-mchanges only the modification time-cdoes not create the file if it doesn’t exist-tsets a specific timestamp
For example, to create a file with a specific timestamp:
touch -t 202503011200 report.txt
This creates report.txt with a timestamp of March 1, 2025 at 12:00.
Creating Files With Redirection
Redirection is another common method. You can use the greater-than symbol to send output to a file.
To create an empty file using redirection:
> newfile.txt
This creates an empty file named newfile.txt. If the file exists, it gets truncated (emptied).
You can also create a file with content right away:
echo "Hello World" > greeting.txt
This creates greeting.txt with the text “Hello World” inside it.
Appending Vs Overwriting
Be careful with single and double redirection:
>overwrites the file>>appends to the file
If you want to add content without deleting what’s already there, use double redirection:
echo "Second line" >> greeting.txt
This adds “Second line” to the end of greeting.txt.
Using Cat Command
The cat command is short for concatenate. It can create files and add content interactively.
To create a file with cat:
cat > notes.txt
After typing this command, the terminal waits for your input. Type whatever you want in the file. When you are done, press Ctrl+D to save and exit.
This is useful for quick notes or small configuration files. You don’t need to open a text editor.
Cat With Heredoc
You can also use a heredoc with cat to create multi-line files:
cat << EOF > script.sh
#!/bin/bash
echo "Running script"
EOF
This creates a bash script file with multiple lines. The EOF marker tells cat where the input ends.
Using Text Editors In Terminal
Sometimes you want to create a file and edit it right away. Terminal-based text editors are perfect for this.
Nano Editor
Nano is beginner-friendly and included in most Linux distributions.
To create a file with nano:
nano myfile.txt
This opens nano with a new file. Type your content, then press Ctrl+O to save and Ctrl+X to exit.
Nano shows helpful commands at the bottom of the screen. The caret symbol (^) means the Ctrl key.
Vim Editor
Vim is more powerful but has a learning curve. To create a file with vim:
vim myfile.txt
Press i to enter insert mode. Type your content. Press Esc to exit insert mode. Then type :wq and press Enter to save and quit.
If you just want to create an empty file and exit vim:
vim myfile.txt
:q
This creates the file and exits without saving any content.
Emacs Editor
Emacs is another popular terminal editor. To create a file:
emacs myfile.txt
Type your content, then press Ctrl+X followed by Ctrl+S to save. Press Ctrl+X then Ctrl+C to exit.
Emacs has a steeper learning curve than nano but offers extensive customization.
Creating Files With Specific Sizes
Sometimes you need a file of a specific size for testing purposes. The dd command can create files with exact sizes.
To create a 1MB file:
dd if=/dev/zero of=testfile bs=1M count=1
This creates a 1MB file filled with zeros. You can change the block size (bs) and count to get different sizes.
For a 100MB file:
dd if=/dev/zero of=largefile bs=1M count=100
This is useful for testing disk performance or application behavior with large files.
Using Fallocate
The fallocate command is faster than dd for allocating space:
fallocate -l 10M testfile
This instantly creates a 10MB file. The file is allocated but not filled with data, making it much faster.
Fallocate works on most modern Linux filesystems like ext4 and xfs.
Creating Hidden Files
Hidden files in Linux start with a dot. Creating them is the same as regular files, just add a dot at the beginning.
touch .hiddenfile
echo "config data" > .config
Hidden files won’t show up with a regular ls command. Use ls -a to see them.
Many Linux applications store configuration in hidden files in your home directory.
Creating Files In Specific Directories
You don’t have to be in the same directory to create a file. Just specify the full path.
touch /home/user/documents/report.txt
echo "data" > /tmp/scratch.txt
Make sure the directory exists before creating the file. You can create directories with the mkdir command.
To create a directory and file in one go:
mkdir -p /home/user/newproject && touch /home/user/newproject/readme.md
The && operator runs the second command only if the first succeeds.
Creating Multiple Files With Brace Expansion
Brace expansion is a powerful bash feature. It lets you create multiple files with patterns.
touch file{1..10}.txt
This creates file1.txt through file10.txt in one command.
You can also use letters:
touch {a,b,c,d}.log
This creates a.log, b.log, c.log, and d.log.
Brace expansion works with other commands too, not just touch.
Creating Files With Permissions
Sometimes you need a file with specific permissions from the start. The install command can do this.
install -m 755 /dev/null script.sh
This creates an empty file called script.sh with executable permissions (755).
You can also copy a template file with permissions:
install -m 644 template.txt newfile.txt
This copies template.txt to newfile.txt with 644 permissions.
Common Mistakes And How To Avoid Them
New Linux users often make these mistakes when creating files:
- Forgetting to specify a filename
- Using spaces in filenames without quotes
- Creating files in directories where you don’t have write permission
- Overwriting existing files accidentally
To avoid overwriting, use the -i flag with cp or mv for confirmation. With touch, the file is never overwritten, just updated.
For filenames with spaces, use quotes:
touch "my important file.txt"
Or escape the spaces with backslashes:
touch my\ important\ file.txt
Checking If A File Was Created
After creating a file, you should verify it exists. Use the ls command:
ls -l filename.txt
This shows the file details including size, permissions, and timestamp.
To check without listing all files:
test -f filename.txt && echo "File exists"
The test command returns true if the file exists.
You can also use stat for detailed information:
stat filename.txt
This shows inode, block size, and timestamps.
Creating Files From Command Output
You can capture the output of any command into a file. This is one of the most powerful features of the terminal.
ls -la > directory_listing.txt
This saves the directory listing to a file.
date > current_time.txt
This saves the current date and time.
ps aux > running_processes.txt
This saves a list of running processes.
You can combine commands with pipes:
grep "error" /var/log/syslog > errors.txt
This extracts lines containing “error” from the system log and saves them.
Creating Temporary Files
Linux has a special directory for temporary files: /tmp. Files here are deleted on reboot.
touch /tmp/tempfile.txt
You can also use the mktemp command to create unique temporary files:
mktemp
This creates a file like /tmp/tmp.XXXXXXXXXX and prints its name.
For a temporary directory:
mktemp -d
This is useful for scripts that need temporary storage.
Creating Symbolic Links As Files
A symbolic link is a special file that points to another file. It’s like a shortcut.
ln -s /path/to/original linkname
This creates a symbolic link called linkname that points to the original file.
Symbolic links can point to files or directories. They are useful for organizing files without duplicating data.
Batch File Creation With Scripts
For creating many files at once, write a small bash script:
#!/bin/bash
for i in {1..100}
do
touch "file_$i.txt"
done
Save this as create_files.sh, make it executable with chmod +x create_files.sh, and run it with ./create_files.sh.
This creates 100 files named file_1.txt through file_100.txt.
Creating Files With Content From The Internet
You can download files directly from the internet using wget or curl:
wget -O myfile.html https://example.com
curl -o myfile.html https://example.com
Both commands save the webpage content to a file.
To save the output of an API call:
curl https://api.example.com/data > data.json
This is common for working with web services.
File Creation In Different Shells
The commands work in bash, zsh, and most other Unix shells. Some features like brace expansion might vary.
In bash, brace expansion is enabled by default. In some older shells, you might need to enable it.
For maximum compatibility, stick with basic touch and redirection commands.
Security Considerations
When creating files, be aware of permissions and ownership. Files you create belong to you by default.
Use the umask command to set default permissions for new files:
umask 022
This gives you read/write permissions and everyone else read-only.
Never create files in system directories unless you are root. Use your home directory or /tmp for testing.
Recap Of Commands
Here is a quick reference of all the methods covered:
touch filename– Create empty file> filename– Create empty file with redirectionecho "text" > filename– Create file with contentcat > filename– Create file interactivelynano filename– Create and edit with nanovim filename– Create and edit with vimdd if=/dev/zero of=filename bs=1M count=1– Create file of specific sizefallocate -l 10M filename– Allocate file quicklyinstall -m 755 /dev/null filename– Create file with permissions
Each method has its strengths. Touch is fastest for empty files. Echo and cat are great for files with content. Text editors are best for complex editing.
Frequently Asked Questions
What Is The Difference Between Touch And Cat For Creating Files?
Touch creates an empty file instantly without opening any input. Cat waits for you to type content and requires Ctrl+D to finish. Use touch for empty placeholder files and cat when you need to add content right away.
Can I Create A File In Linux Terminal Without Any Content?
Yes, the touch command creates an empty file with no content. You can also use the redirection operator > filename to create an empty file. Both methods work instantly.
How Do I Create A File With A Specific Extension In Linux?
Just include the extension in the filename. For example, touch script.sh creates a shell script file, touch data.csv creates a CSV file. The extension doesn’t affect how Linux treats the file, but it helps with organization.
What Happens If I Try To Create A File That Already Exists?
With touch, the file’s timestamp gets updated but content remains unchanged. With redirection (>), the file gets overwritten and becomes empty. With echo and cat, the content gets replaced. Always check before overwriting important files.
How Do I Create A Hidden File In Linux Terminal?
Start the filename with a dot. For example, touch .hidden creates a hidden file. Use ls -a to see hidden files. They work exactly like regular files but are hidden from normal directory listings