A Deep Dive into the Process of Text File Creation in Linux
How to Create a Text File in Linux
Now will explain multiple procedures to create text files easily in Linux:
Using the Touch Command to Create a File
We may use the touch command to change the timestamps of current files and folders and create new, empty files. The touch command is the simplest and most unforgettable way to generate new, empty folders.
To make a new file, use the touch command and the name of the file you want to make:
If filename.txt does not exist, the command above will generate it; otherwise, the timestamps will be changed. To make several files at the same time, split the file names with space:
touch filename1.txt filename2.txt filename3.txt
Using the Redirection Operator to Make a File
You may use redirection to take a command's output and use it as input for another command or file. There are two ways to assign the output to a file. The >> operator appends the output to a current file, while the > operator overwrites it. In Linux, this is the quickest way to make a new file. When using redirection to create a file, take care not to delete a significant existing file. To make an empty zero-length buffer, specifically indicate the name of the file you want to construct after the redirection operator:
Using the Cat Command to Create Text File
Cat is mostly used to read and concatenate files, but it can also create new ones. To make a new file, use the cat button, followed by the redirection operator > and the new file's name. Click Enter to type your email, and then press CTRL and D keys to save the files.
cat > filename.txt
Using the Echo Command to Create a File
The echo command outputs the strings passed as arguments to standard output, which can be forwarded to a register. For creating a new file, type echo followed by the text you want to print, and then use the redirection operator > to write the output to the new file.
echo "Some line" > filename.txt
If you want to make an empty file, just type:
echo > filename.txt
Using Heredoc to Create a File
Heredoc is a form of redirection that lets you give a command multiple lines of input. This approach is typically used when you need to generate a file from a shell script containing several text lines. To build a new file named filename.txt, for example, use the following code:
cat << EOF > filename.txt
Some other lines
Variables, special characters, and instructions will all be used in the heredoc's body.
How to Create a Large Text File
You may want to generate a big data file for testing purposes on occasion. When you want to measure the write speed of your drive or the download speed of your link, this is handy.
Making use of the dd command
The dd command is most often used to copy and transfer files. To make a 2GB file called 2G.test, execute the following commands:
dd if=/dev/zero of=2G.test bs=1 count=0 seek=2G
By the fallocate function, fallocate is a command-line feature that allows you to assign actual disc space to data. The command below will generate a new file called 2G.test with a 2GB size:
fallocate -l 2G 2G.test
Using different commands and redirection, we have learned how to generate a new file in Linux using the command line functions. We hope that our described information can help you create a text file in Linux without having any trouble.