Categories: TutorialsUbuntu

How to Create a File in Linux Using Cat Command

As there are several ways to do a single task in a Linux system. The same is also true for creating a file in Linux. In Linux, you can create a file using multiple ways including both the command line and the graphical ways. Some of the commands that are used to create a file in Linux include touch, cat, and echo commands. You can also use the text editors like Nano, Vim, and Gedit to create a file in Linux.

Sponsored

Today’s post is about how to create a file in Linux using the cat command. This is a quick method for those who want to quickly create a new file or add some text to their files.

Note: The method outlined here is focused on Linux distribution Ubuntu, but you can follow the same method if you’re using any other Linux distribution.

Create a File Using Cat Command

To create a file in Linux using the cat command, type cat followed by > operator and the filename as follows:

$ cat > filename

For instance to create a file named testfile.txt, use the cat command as follows:

$ cat > testfile.txt

After running the above command, you will not be returned to the Terminal prompt. Instead, Terminal will wait for your input. Type the text you want to save in this file. Hit Enter after typing each line. Once you entered all the text, hit Ctrl+D to save and exit.

Now to verify if the file has been created, run this command:

$ ls

This command lists all the files in the current working directory. In the output of this command, you will find the testfile.txt that you have created using the cat command.

After verifying the file creation, you can also view its content using the command below:

$ cat testfile.txt

This command will show you the text that this file testfile.txt contains.

Sponsored

Append Text to File Using Cat Command

After creating the file, you may need to add more text to the file. You can also do this using the cat command. For instance, to add more text to the file testfile.txt, type cat followed by the >> operator and the filename that you want to edit:

$ cat >> testfile.txt

Now add the text you want to append in the file. This command will append the text in the testfile.txt file. It will not overwrite the already added text. Once you have added the text, hit Ctrl+D to save and exit the file.

Now to verify if the file has been appended, use the command below:

$ cat testfile.txt

This command will show you the text that has been appended in the file testfile.txt.

Note: If a file already contains the text, using the cat command with the >> operator will append the text in the file. It will not replace or overwrite any existing text in the file. While using the cat command with > operator will overwrite the text in the file.

There you have how to create a file in Linux using the cat command. Using the cat command, you can also create a file, view the content of a file without opening it, and concatenate multiple files.

Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications including CCNA RS, SCP, and ACE. As an IT engineer and technical author, he writes for various websites.

Ubuntu Server Admin

Recent Posts

Building RAG with enterprise open source AI infrastructure

One of the most critical gaps in traditional Large Language Models (LLMs) is that they…

4 hours ago

Life at Canonical: Victoria Antipova’s perspective as a new joiner in Product Marketing

Canonical is continuously hiring new talent. Being a remote- first company, Canonical’s new joiners receive…

1 day ago

What is patching automation?

What is patching automation? With increasing numbers of vulnerabilities, there is a growing risk of…

2 days ago

A beginner’s tutorial for your first Machine Learning project using Charmed Kubeflow

Wouldn’t it be wonderful to wake up one day with a desire to explore AI…

3 days ago

Ubuntu brings comprehensive support to Azure Cobalt 100 VMs

Ubuntu and Ubuntu Pro supports Microsoft’s Azure Cobalt 100 Virtual Machines (VMs), powered by their…

3 days ago

Ubuntu Weekly Newsletter Issue 870

Welcome to the Ubuntu Weekly Newsletter, Issue 870 for the week of December 8 –…

4 days ago