How to create and edit text files using command-line from Linux Terminal

We shall discuss several ways of creating and editing text files using the command-line from the Linux Terminal.

In today’s Terminal Tuts session, we shall show you several ways of creating and editing text files that can be done easily and quickly using the command line.

Here are the following commands that can be used to create a text file.

  1. Cat command
  2. Touch command
  3. Standard Redirect Symbol
  4. Nano command
  5. Vi command

1. Cat Command

Cat command is mainly used to preview the text file content. However, you can use it to create new files and edit them, too, by using the redirection method. For example, use the following command to create a new file:

cat > cattestfile.txt

After executing the command, a cursor will appear waiting for you to enter any text you need to edit the newly created file.

Create a File Using The Cat Command
Cat Command

Once you finished editing your file and you need to exit, press CTRL+D. Now you can see the standard command prompt comes again.

To check if the file was created successfully, you can use the list command as follows:

ls -l

Check Newly Created File
Check Newly Created File

To ensure that the text you have entered was saved successfully, then you can use the command:

cat cattestfile.txt

Display The Cat File Content
Display The Cat File Content

2. Touch Command

In this method, you will be able to create single or multiple files using the touch command.

To create a single file.

touch touchfile.txt

Create a File Using The Touch Command
Touch Command

To check if the new file was created successfully.

ls -l

Check Newly Created Touch File
Touch File

Now, in case you need to create multiple files. Then you can use the following command.

touch file1.txt file2.txt file3.txt file4.txt

Create Multiple Files Using The Touch Command
Touch Command

To check if the previous files were created or not.

ls -l

Check Newly Created Multiple Touch File
Check Newly Created Multiple Touch File

3. Redirect command

In this method, we will use the standard redirect command to create a new file. Unlike the touch command, this method will be able to create one single file only at the time.

To create a new file.

> stdred.txt

Create File Using The Standard Redirect Symbol
Standard Redirect Symbol

To check that the file was created successfully.

ls -l

Check Newly Created File By Standard Redirect Symbol
Standard Redirect Symbol

4. Nano Command

Using the nano command, you will be able to create a new file and edit it too.

To create a new file.

nano nanofile.txt

Create File Using The Nano Command
Nano Command

A nano editor will open like the below screenshot, and you will be able to write and edit your file. Once you have finished editing your file, use the CTRL+O to save your file and use the CTRL+X to exit the nano editor.

Edit The Newly Created Nano File
Edit The Newly Created Nano File

To ensure that the previous file was created successfully, use the list command.

ls -l

Check Newly Created File By The Nano Command
Nano Command

To display the file content, use the following command.

cat nanofile.txt

Display The Nano File Content
Display The Nano File Content

6. Vi Command

In this method, we will use the vi command to create a new file and edit it.

To create a new file.

vi vifile.txt

Create And Edit New File Using The Vi Editor
Vi Editor

A vi editor will open then you can start editing your file. Vi is a little bit different than the nano editor, which means for every action you need to do, there is a command that you need to execute first. For example, if you need to enter the vi command mode first you need to press ESC, then one of the following commands:

:i --> To insert a new line.
:w --> To save file.
:q --> To exit file.
:wq --> To save and quit file.
:q! --> To exit file without saving .

Edit New File Using The Vi Editor
Vi Editor

To check if the file was created successfully.

ls -l

Check Newly Created File By The Vi Command
Vi Command

To display the file content.

cat vifile.txt

Display The Vi File Content
Display The Vi File Content

Conclusion

That ends our guide on creating text files and editing them using command-lines via the Linux Terminal. I hope you enjoyed it.

Hend Adel
Hi! I'm Hend Adel, a freelancer technical geek with successful experience in Database, Linux and many other IT fields. I help to build solutions to suit business needs and creating streamlined processes. I love Linux and I'm here to share my skills via FOSS Linux! Thanks for reading my article.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

STAY CONNECTED

23,555FansLike
365FollowersFollow
16SubscribersSubscribe

LATEST ARTICLES

The 10 Best Linux Server Distros For Home And Businesses

By the year 2020, it is estimated that there are close to 600 Linux distributions in the market. It includes both servers and Desktop versions; therefore, if you are looking for lightweight Linux distribution for your old PC or a reliable desktop version for employees in your organization, you may be overwhelmed with the number of choices for finding one for your use.

How to list the installed Packages on Ubuntu

It is only natural to start installing more and more software on your Ubuntu PC once you start using it. Similarly, it is also natural to lose track of the different packages that you have installed on your system. Eventually, you may be overwhelmed with the number of packages in your system and with the ones you hardly used.

How to configure the Ubuntu Firewall (UFW)

A properly configured firewall is a crucial part of establishing preliminary system security. Keeping this in mind, here we will go over how to configure the firewall on your Ubuntu PC.

How to install and use Zoom on Ubuntu

If you work from home or hold meetings with other people remotely, then you have probably heard of Zoom. It is one of the popular video conferencing software app available today.

How to Restart Networking on Ubuntu

Sometimes resetting your Ubuntu network is required to apply some network configurations like changing your IP from Automatic DHCP to static one. Restarting the network is not a big deal, but should be done carefully.

How to install Chrome and Chromium Browser on Pop!_OS

By default, Pop!_OS comes with Mozilla Firefox Browser installed to help you browse the internet. It is a reliable web browser covering almost all the features and functionalities you need. However, working with Firefox is noticeably different compared to Google Chrome, especially from the speed point of view. Google Chrome is a tad quicker than Firefox.

MUST READ

Linux is growing faster than ever. As per the latest report, there is a drop in the Windows 10 market share for the first time, and Linux's market share has improved to 2.87% this month. Most of the features in the list were rolled out in the Pop OS 20.04. Let's a detailed look into the new features, how to upgrade, and a ride through video.
Elementary OS 5.1 Hera has received a point release with a handful of new features and bug fixes, and we will be reviewing the significant changes in this article. For those new to elementary OS, this Ubuntu-based Linux distribution uses their inhouse built Pantheon desktop environment and AppCenter.

5 Best Music Players for Linux

For sure, there will be Linux users out there who cannot live without music. If you're one of those people, you would know how important it is to have a good music player installed on your computer system. In this article, we present to you the 5 Best Music Players for Linux, which will hopefully be able to satisfy each of our readers' needs.

15 Tar command in Linux uses with examples

Tar is a famous utility that is basically used for collecting multiple files in a single archive. This file is often called a 'tarball'. Today, we are going to tell you about the different things you can do with tarballs using the tar command, with practical examples.

Manjaro ‘Gellivara’ XFCE Edition (17.0.5) in 10 Screenshots

Want to take a quick virtual tour Manjaro XFCE edition instead of downloading GBs worth of ISO image and then making a Live USB of it? We will make it easy for you. Here are a series of screenshots of the important aspects of Manjaro Linux in XFCE edition. This is a light-weight edition, and aims to be fast and low on system resources. You will be amazed on how it is still visually appealing and user friendly.

5 Best ways to encrypt files in Linux

Privacy is something that almost all of us have concerns about. Many people switch over to Linux because of its better privacy features. In this time, where many people believe (quite rightly so) that privacy doesn't exist anymore, Linux provides a ray of hope. Distributions like Tails OS are specifically designed for that purpose.