How To Create A File In Linux: Echo, Touch, Tee and Cat Commands

Introduction

Creating files in Linux is a common task for system administrators and developers. There are several ways to create files in Linux, including using the echo, touch, tee, and cat commands. Each of these commands has its own purpose and can be used to create different types of files. In this guide, we will discuss how to use each of these commands to create files in Linux. We will also discuss some of the common options and flags that can be used with each command. By the end of this guide, you should have a good understanding of how to create files in Linux using these commands.

How To Create A File In Linux: Echo, Touch, Tee and Cat Commands

1. Echo Command:

The echo command is used to create a new file in Linux. To create a file, type the following command in the terminal:

echo “This is a new file” > filename.txt

This will create a new file called filename.txt with the text “This is a new file” inside it.

2. Touch Command:

The touch command is used to create an empty file in Linux. To create an empty file, type the following command in the terminal:

touch filename.txt

This will create an empty file called filename.txt.

3. Tee Command:

The tee command is used to create a file and write data to it. To create a file and write data to it, type the following command in the terminal:

echo “This is a new file” | tee filename.txt

This will create a new file called filename.txt with the text “This is a new file” inside it.

4. Cat Command:

The cat command is used to create a file and write data to it. To create a file and write data to it, type the following command in the terminal:

cat > filename.txt

This will open a text editor in the terminal. Type the text you want to write to the file and press Ctrl+D to save and exit. This will create a new file called filename.txt with the text you typed inside it.

Files are one of the most important objects of any operating system and Linux is not an exception. Files provide a reliable way of storing data in a persistent manner.

Linux uses plain text files to store important configurations. For example, the /etc/hosts file stores static table lookup for hostnames, the /etc/crontab file contains instructions for the cron daemon, and so on.

Certainly, we can use graphical tools to create files. However, the same can be achieved using the command line interface as well. In this easy-to-follow guide, we will discuss various ways of creating a file in Linux.

1. Create an Empty File Using > Redirection Operator

In Linux, the redirection operator (>) is used to redirect the output of a command to a file instead of displaying it on the terminal.

The same (>) operator is also used to create a file if it doesn’t exist already. However, it makes the file empty if it exists already. Hence one should be very careful while using the redirect operator.

$ > tecmint.txt
$ head tecmint.txt
Create File with Redirect Operator
Create a File with Redirect Operator

In the above example, we can see that the head command is not showing any output as the file is empty.

2. Create File and Write Content Using > Redirection Operator

Sometimes, we want to create a non-empty file quickly. In such cases, you can use the output redirection operator (>) to create a file and write content to it using the echo command as shown.

$ echo "Tecmint.com is a popular Linux blog" > tecmint.txt
$ head tecmint.txt
Ceate a File and Write Content to File
Create a File and Write Content to the File

It is important to note that in this example, we have used the echo command to create a file. However, we can redirect the output of the other Linux commands as well to create a file.

Also, it is important to note that the > redirection operator is used to overwrite the contents of an already existing file, which cause data loss if the operation is performed carelessly.

In such a case, we can use the >> redirection operator, which is used to append the contents to the existing file.

$ echo "Tecmint.com #1 Linux blog" > tecmint.txt
$ head tecmint.txt
Append Content to File in Linux
Append content to File in Linux

In the above output, we can see that the new line gets appended at the end of the file.

It is worth noting that, just like the redirection operator, the append operator will also create an empty file if it doesn’t exist already.

3. Create Files Using touch Command

One more way of creating a file is using the touch command, which offers the safest way of creating an empty file because it never overwrites the existing file. Instead, it just updates the time stamp (access time and modification time) of the existing file.

$ touch tecmint.txt

4. Create Files Using tee Command

Similar to the redirection operator we can also use the tee command to create a file. The tee command writes the output of the command to the standard output stream as well as the file.

For example, to create a file named “tecmint.txt“, use the tee command, which will be ready to accept input.

$ tee tecmint.txt

Now type or paste the content you want to write to the file and then press Enter and hit Ctrl + C close the input window as shown.

Create a File Using Tee Command
Create a File Using Tee Command

If you want to overwrite the content of a file using the tee command, you can use the following command:

$ echo "Overwrite file using the tee command" | tee tecmint.txt
$ head tecmint.txt
Overwrite a File in Linux
Overwrite a File in Linux

In this example, we can observe that the tee command overwrites the contents of the tecmint.txt file which was created and updated in earlier examples.

To append the contents to the existing file, use the -a option of the tee command, which allows us to append the data at the end of the existing file.

$ echo "Append data using the tee command" | tee -a tecmint.txt

5. Create a File Using cat Command

We can use the combination of the cat command and redirection operator to create a file. For example, the below command creates a new file if it doesn’t exist already.

$ cat > tecmint.txt

Here, the terminal waits infinitely for the user input. We have to press Ctrl + D after entering the required text to save the contents to the file:

Create File Using Cat Command
Create File Using Cat Command

The main advantage of this approach is that it allows us to create a multi-line file using an interactive way. Just like the redirection operator, we have to use this method very carefully as it overwrites the existing file.

In a similar way, we can use the combination of the cat command and append operator to append the contents at the end of the existing file.

$ cat >> tecmint.txt

Just like in the previous example, we have to press Ctrl + D to append the contents to the file after entering the required text.

Append Text to End of File in Linux
Append Text to End of File in Linux
Conclusion

In this guide, we discussed how to create a file using the Linux command line interface. Linux beginners can use one of the methods to create a file from the terminal.

Do you know of any other way of creating a file from the terminal in Linux? Let us know your views in the comments below.

How To Create A File In Linux: Echo, Touch, Tee and Cat Commands

Creating a file in Linux is a relatively simple process. There are several commands that can be used to create a file, including the echo, touch, tee and cat commands. Each of these commands has its own purpose and can be used to create a file in different ways.

Using the Echo Command

The echo command is used to create a file by writing text to it. To use the echo command, you must specify the text that you want to write to the file, followed by the name of the file. For example, to create a file called “myfile.txt” with the text “Hello World”, you would use the following command:

echo "Hello World" > myfile.txt

This command will create a file called “myfile.txt” with the text “Hello World” in it.

Using the Touch Command

The touch command is used to create an empty file. To use the touch command, you must specify the name of the file that you want to create. For example, to create a file called “myfile.txt”, you would use the following command:

touch myfile.txt

This command will create an empty file called “myfile.txt”.

Using the Tee Command

The tee command is used to create a file by writing text to it, as well as displaying the text on the screen. To use the tee command, you must specify the text that you want to write to the file, followed by the name of the file. For example, to create a file called “myfile.txt” with the text “Hello World”, you would use the following command:

echo "Hello World" | tee myfile.txt

This command will create a file called “myfile.txt” with the text “Hello World” in it, as well as displaying the text on the screen.

Using the Cat Command

The cat command is used to create a file by copying the contents of another file. To use the cat command, you must specify the name of the file that you want to copy, followed by the name of the file that you want to create. For example, to create a file called “myfile.txt” by copying the contents of “otherfile.txt”, you would use the following command:

cat otherfile.txt > myfile.txt

This command will create a file called “myfile.txt” with the contents of “otherfile.txt”.

These are just a few of the commands that can be used to create a file in Linux. Each of these commands has its own purpose and can be used to create a file in different ways. With a little practice, you should be able to create files in Linux with ease.

Jaspreet Singh Ghuman

Jaspreet Singh Ghuman

Jassweb.com/

Passionate Professional Blogger, Freelancer, WordPress Enthusiast, Digital Marketer, Web Developer, Server Operator, Networking Expert. Empowering online presence with diverse skills.

jassweb logo

Jassweb always keeps its services up-to-date with the latest trends in the market, providing its customers all over the world with high-end and easily extensible internet, intranet, and extranet products.

Contact
San Vito Al Tagliamento 33078
Pordenone Italy
Item added to cart.
0 items - 0.00
Open chat
Scan the code
Hello 👋
Can we help you?