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.

Ubuntu Server Admin

Recent Posts

Canonical Releases Ubuntu 25.04 Plucky Puffin

The latest interim release of Ubuntu introduces “devpacks” for popular frameworks like Spring, along with…

2 days ago

Ubuntu 25.04 (Plucky Puffin) Released

Ubuntu 25.04, codenamed “Plucky Puffin”, is here. This release continues Ubuntu’s proud tradition of integrating…

3 days ago

Extended Security Maintenance for Ubuntu 20.04 (Focal Fossa) begins May 29, 2025

Ubuntu released its 20.04 (Focal Fossa) release 5 years ago, on March 23, 2020. As…

3 days ago

Ubuntu 20.04 LTS End Of Life – activate ESM to keep your fleet of devices secure and operational

Focal Fossa will reach the End of Standard Support in May 2025, also known as…

4 days ago

Ubuntu MATE 25.04 Release Notes

Ubuntu MATE 25.04 is ready to soar! 🪽 Celebrating our 10th anniversary as an official…

4 days ago

Ubuntu Weekly Newsletter Issue 887

Welcome to the Ubuntu Weekly Newsletter, Issue 887 for the week of April 6 –…

6 days ago