How to Create a Text File in Linux OS

How to Create a Text File in Linux OS

Do you know how to create files in Linux? If not, then this article will help you to create Linux file commands. Anyone who uses the Linux operating system on a daily basis must have created a text file in Linux or should be familiar with the Linux create file command. A new file can be created using the command line or desktop file manager. You should have given written permission to the parent directory to create a new file. If you don’t, you will get a permission denied error. This tutorial will provide brief information on how to create a file in Linux.

How to create a text file in Linux?

Now we will explain different methods to create files easily in Linux:

1. Using the Touch command to create a file

We can use the touch command to change the timestamps of current files and folders and create new, empty files. Touch commands are the easiest and most memorable way to create new, empty folders.

To create a new file, use the touch command and the name of the file you want to create:

 touch filename.txt

If filename.txt does not exist, the above command will create it; Otherwise, the timestamp will be changed. To create multiple files at the same time, separate the filenames listed in the Linux command below with spaces.

touch filename1.txt filename2.txt filename3.txt

2. Using the redirection operator to create a file

You can use redirection to take the output of a command 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 an existing file, while the > operator overwrites it. On Linux, this is the fastest way to create a new file. When using redirection to create a file, you should not delete a significant existing file. To create an empty zero-length buffer, specify the name of the file you want to create after the redirect operator:

>filename.txt

3. Using the Cat Command to Create a Text File

The cat command is mostly used to read and attach files, but it can also create new ones. To create a new file, use the cat button, followed by the redirection operator > and the name of the new file. Click Enter to type your email, and then press CTRL + D to save the files.

 cat > filename.txt

4. Using the echo command to create a file

The echo command output strings are passed as arguments to standard output, which can be forwarded to a register. To create a new file in Linux, type echo after 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 an empty file, just type:

 

 echo > filename.txt

Uses Heredoc to create a file

Heredoc is a form of redirection that allows you to input multiple lines of command. This method is usually used when you need to create a file from a shell script that contains several lines of text. To create a new file named filename.txt, for example, use the following code:

cat << EOF > filename.txt
some lines
A few more lines
Eof

Variables, special characters, and directives will all be used in the body of the heredoc.

6. How to create a large text file

You may occasionally want to create a large data file for testing purposes. When you want to measure your drive’s write speed or your link’s download speed, it’s easy.

6.1 Using the dd command

The dd command is often used to copy and move files. To create a 2GB file called 2G.test, run the following commands:

dd if=/dev/zero of=2G.test bs=1 count=0 seek=2G

The allocate function is a command-line feature that allows you to allocate physical disk space to data. The command below will create a new file named 2G.test of size 2GB:

fallocate -l 2G 2G.test

Conclusion

Using various commands and redirections, we learned how to create a file using Linux/Linux create file command line function. We hope that our described information will help you to create a new file in Linux without any problems. If you have any other suggestions for creating a new file, you can comment in the comment section below.

If you own a website or want to create a website but don’t know where to host it? You can go to our Linux VPS to host your website without any errors. You will have a 24/7 support team who will always be there to help you.

Scroll to Top