I am a Linux terminal fan, at all times looking out for tactics to carry out widespread operations immediately from the command line. One such operation I typically carry out within the terminal is the short creation of a file. A number of Linux instructions enable us to create recordsdata with out leaving the terminal.
Create Information With No Content material
Generally, you simply want a placeholder—a file that is empty and able to be stuffed later. There are a few methods to perform this within the terminal. For instance, you possibly can use the touch command to generate single or quite a few empty recordsdata, or use the redirection operator to create a single empty file.
The contact Command
The simplicity of the contact command makes it a favourite for fast duties like initializing log recordsdata or resetting timestamps. You will not see any output within the terminal after making a file with contact. Nevertheless, if the file is already current, contact will replace its entry and modification occasions as an alternative of overwriting it.
To create a single empty file, run:
contact filename.txt
This can create a file within the residence listing. If you wish to create a file in a particular listing, point out the listing path earlier than the filename, like this:
contact /path_to_directory/file_name.txt
Additional, to create a number of recordsdata directly, specify all of the filenames together with their extensions, separated by areas, after the contact command.
contact file1.txt file2.txt file3.txt
You may confirm the file creation by utilizing the ls command to record the recordsdata in your present listing.
ls
I typically use the contact command to rapidly create empty recordsdata, and after that, I exploit the Nano or Vim textual content editor to insert textual content.
Utilizing the Redirection Operator
The > operator is often used with numerous instructions like echo to redirect data to specific files. Nevertheless, it’s also possible to use this operator alone with only a filename to create a brand new empty file.
As an illustration, let’s make an empty file with:
> empty_log.txt
If the file already exists, the > operator overwrites it. Redirection is tremendous versatile, and you will find your self utilizing it far and wide within the terminal for different duties. So, getting conversant in it early on is a great transfer.
Create Information With Knowledge
More often than not, we create recordsdata to retailer textual content, code, or knowledge proper from the beginning. You may carry out all of those with the instructions comparable to cat, echo, and printf. These instructions supply a fast approach so as to add textual content with out leaving the terminal. I like to recommend utilizing these instructions when you do not require superior enhancing choices and simply want to avoid wasting one or a number of strains in your newly created configuration recordsdata.
Use echo or printf
You may already use the echo or printf instructions to show textual content within the terminal. However guess what? You may mix them with the redirection operator (>) to jot down textual content immediately right into a file.
To create a file and add textual content, use the echo command adopted by your message inside citation marks and the redirection operator pointing to your new file, comparable to:
echo "Hi there, Linux!" > file.txt
This creates a brand new file (or overwrites an current one) in your house listing with the desired content material. To confirm, use cat or much less:
cat file.txt
Moreover, if you wish to append knowledge as an alternative of overwriting the file, use >> as an alternative:
echo "New Entry" >> file.txt
If you happen to want extra formatting management, printf works equally however supplies higher flexibility. It permits higher management over textual content formatting, much like what you may use in programming languages.
For instance, to create a file with structured textual content:
printf "Identify: %snAge: %dn" "Alice" 30 > data.txt
Right here, %s is changed by “Alice” (a string), and %d is changed by 30 (a decimal integer). The n inserts a newline character, making each bit of knowledge seem on a separate line.
With cat
The cat command is not only for studying recordsdata—it may well create them, too. You may create a file and add a number of strains of textual content in a single go immediately out of your terminal.
Simply mix cat with the enter redirection operator (>), sort your textual content, and press Ctrl+D while you’re executed:
cat > new.txt
Kind your ideas...
Press Enter for brand new strains.
Press Ctrl+D to avoid wasting.
Your textual content is saved to new.txt upon exiting. You may then view the content material with:
cat new.txt
With cat, you do not want a full-fledged textual content editor—it acts like a easy command-line textual content enter instrument.

Associated
10 Basic Linux Commands for Beginners
These terminal instruments are each straightforward and helpful for profiting from your LInux PC.
Use Command-Line Textual content Editors
If you happen to want extra superior choices than cat however do not wish to go away the terminal, try command-line text editors. A number of choices, comparable to Nano, Vim, and Emacs, enable in-terminal enhancing with full customization choices and shortcuts.
I really like utilizing nano as a result of it is easy, user-friendly, and filled with helpful options. To get began, merely run the nano command adopted by the file identify, like this:
nano draft.txt
If draft.txt would not exist, nano will create it. Your terminal window will now rework into the nano editor. You may see a textual content space the place you possibly can sort, and useful command shortcuts are normally displayed on the backside of the display screen (like Ctrl+O to avoid wasting, Ctrl+X to exit).
For these able to degree up, there are Vim or Emacs, although they do have steeper studying curves. Vim is extremely highly effective and environment friendly when you grasp its modal enhancing system (modes for inserting textual content, instructions, and so on.). To create a file with vim:
vim draft.txt
This opens Vim in regular mode. To begin typing, change to insert mode by coming into “i”. When you’re executed enhancing, press Esc to return to regular mode. To avoid wasting and exit, sort “:wq” and press Enter (“:w” means write/save, and q means stop).
If you happen to’re a newbie, use Nano for fast edits. If you’d like extra superior choices and are prepared to study, attempt Vim, which is broadly used for coding and textual content manipulation.
Create Information of a Particular Measurement
At occasions, you may must create a file of a particular dimension for testing or simulation functions. This sounds odd at first, however it’s really useful in testing disk area, creating placeholder recordsdata for functions, and even securely deleting data.
The fallocate command is a quick and environment friendly method to create a file of a particular dimension with out really writing knowledge to it. In contrast to others strategies, fallocate pre-allocates area with out filling it with knowledge, making it ultimate for digital disks or placeholder recordsdata.
To create a file of a selected dimension, use the -l choice adopted by the dimensions:
fallocate -l 10M large_file.bin
Confirm it with the ls command:
ls -lh
This creates a large_file.bin file that’s 10 MB in dimension. It stays principally empty when it comes to precise knowledge blocks till actual content material is written to it.
Moreover, you should use the dd command to create a file full of zeros. As an illustration, let’s create a 100 MB file full of zeros with this:
dd if=/dev/zero of=zeros.bin bs=1M rely=100
Right here, bs units the block dimension, and the rely determines the variety of blocks to jot down. This methodology is beneficial for creating bootable USBs or testing I/O efficiency.
Additional, you should use completely different enter recordsdata (like /dev/random for random knowledge) and completely different output recordsdata. Nevertheless, be further cautious when utilizing the dd command, particularly with the “of=” choice, to keep away from by accident overwriting vital recordsdata.
There you could have it! Creating and enhancing recordsdata within the Linux terminal would not should be difficult. With these instruments, you possibly can effectively create and modify recordsdata with out leaving the command line terminal.

Associated
10 Linux Commands to Know for Managing Files
These instructions are important while you’re working with recordsdata and directories.