
I’m a Linux terminal fan, always on the lookout for ways to perform common operations directly from the command line. One such operation I often perform in the terminal is the quick creation of a file. Several Linux commands allow us to create files without leaving the terminal.
Create Files With No Content
Sometimes, you just need a placeholder—a file that’s empty and ready to be filled later. There are a couple of ways to accomplish this in the terminal. For example, you can use the touch command to generate single or numerous empty files, or use the redirection operator to create a single empty file.
The touch Command
The simplicity of the touch command makes it a favorite for quick tasks like initializing log files or resetting timestamps. You won’t see any output in the terminal after creating a file with touch. However, if the file is already present, touch will update its access and modification times instead of overwriting it.
To create a single empty file, run:
touch filename.txt
This will create a file in the home directory. If you want to create a file in a specific directory, mention the directory path before the filename, like this:
touch /path_to_directory/file_name.txt
Further, to create multiple files at once, specify all the filenames along with their extensions, separated by spaces, after the touch command.
touch file1.txt file2.txt file3.txt
You can verify the file creation by using the ls command to list the files in your current directory.
ls
I often use the touch command to quickly create empty files, and after that, I use the Nano or Vim text editor to insert text.
Using the Redirection Operator
The > operator is normally used with various commands like echo to redirect data to specific files. However, you can also use this operator alone with just a filename to create a new empty file.
For instance, let’s make an empty file with:
> empty_log.txt
If the file already exists, the > operator overwrites it. Redirection is super versatile, and you’ll find yourself using it all over the place in the terminal for other tasks. So, getting familiar with it early on is a smart move.

Related
How to Pipe and Redirect Like a Pro in the Linux Command Line
Many hands make light work.
Create Files With Data
Most of the time, we create files to store text, code, or data right from the start. You can perform all of these with the commands such as cat, echo, and printf. These commands offer a quick way to add text without leaving the terminal. I recommend using these commands when you don’t require advanced editing options and just need to save one or multiple lines in your newly created configuration files.
Use echo or printf
You might already use the echo or printf commands to display text in the terminal. But guess what? You can combine them with the redirection operator (>) to write text directly into a file.
To create a file and add text, use the echo command followed by your message inside quotation marks and the redirection operator pointing to your new file, such as:
echo "Hello, Linux!" > file.txt
This creates a new file (or overwrites an existing one) in your home directory with the specified content. To verify, use cat or less:
cat file.txt
Additionally, if you want to append data instead of overwriting the file, use >> instead:
echo "New Entry" >> file.txt
If you need more formatting control, printf works similarly but provides greater flexibility. It allows better control over text formatting, similar to what you might use in programming languages.
For example, to create a file with structured text:
printf "Name: %s\nAge: %d\n" "Alice" 30 > info.txt
Here, %s is replaced by “Alice” (a string), and %d is replaced by 30 (a decimal integer). The \n inserts a newline character, making each piece of information appear on a separate line.
With cat
The cat command isn’t just for reading files—it can create them, too. You can create a file and add multiple lines of text in one go directly from your terminal.
Just combine cat with the input redirection operator (>), type your text, and press Ctrl+D when you’re done:
cat > new.txt
Type your thoughts...
Press Enter for new lines.
Press Ctrl+D to save.
Your text is saved to new.txt upon exiting. You can then view the content with:
cat new.txt
With cat, you don’t need a full-fledged text editor—it acts like a simple command-line text input tool.

Related
10 Basic Linux Commands for Beginners
These terminal tools are both easy and useful for making the most of your LInux PC.
Use Command-Line Text Editors
If you need more advanced options than cat but don’t want to leave the terminal, try command-line text editors. Several options, such as Nano, Vim, and Emacs, allow in-terminal editing with full customization options and shortcuts.
I love using nano because it’s simple, user-friendly, and packed with useful features. To get started, simply run the nano command followed by the file name, like this:
nano draft.txt
If draft.txt doesn’t exist, nano will create it. Your terminal window will now transform into the nano editor. You’ll see a text area where you can type, and helpful command shortcuts are usually displayed at the bottom of the screen (like Ctrl+O to save, Ctrl+X to exit).
For those ready to level up, there are Vim or Emacs, though they do have steeper learning curves. Vim is incredibly powerful and efficient once you master its modal editing system (modes for inserting text, commands, etc.). To create a file with vim:
vim draft.txt
This opens Vim in normal mode. To start typing, switch to insert mode by entering “i”. Once you’re done editing, press Esc to return to normal mode. To save and exit, type “:wq” and press Enter (“:w” means write/save, and q means quit).
If you’re a beginner, use Nano for quick edits. If you want more advanced options and are willing to learn, try Vim, which is widely used for coding and text manipulation.
Create Files of a Specific Size
At times, you might need to create a file of a specific size for testing or simulation purposes. This sounds odd at first, but it’s actually helpful in testing disk space, creating placeholder files for applications, or even securely deleting data.
The fallocate command is a fast and efficient way to create a file of a specific size without actually writing data to it. Unlike others methods, fallocate pre-allocates space without filling it with data, making it ideal for virtual disks or placeholder files.
To create a file of a particular size, use the -l option followed by the size:
fallocate -l 10M large_file.bin
Verify it with the ls command:
ls -lh
This creates a large_file.bin file that is 10 MB in size. It remains mostly empty in terms of actual data blocks until real content is written to it.
Furthermore, you can use the dd command to create a file filled with zeros. For instance, let’s create a 100 MB file filled with zeros with this:
dd if=/dev/zero of=zeros.bin bs=1M count=100
Here, bs sets the block size, and the count determines the number of blocks to write. This method is useful for creating bootable USBs or testing I/O performance.
Further, you can use different input files (like /dev/random for random data) and different output files. However, be extra careful when using the dd command, especially with the “of=” option, to avoid accidentally overwriting important files.
There you have it! Creating and editing files in the Linux terminal doesn’t have to be complicated. With these tools, you can efficiently create and modify files without leaving the command line terminal.

Related
10 Linux Commands to Know for Managing Files
These commands are essential when you’re working with files and directories.
Source link