Open In App

How to Edit Text Files in Linux?

Last Updated : 08 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Like any other OS, creating and editing Text files is an important skill to be known to any user. In this article, we will learn how to easily create and edit Text files in Linux using simple and basic terminal commands such as cat, touch, nano, vi, and standard redirect symbol.

What is Text File in Linux?

In Linux, a text file in Linux is a type of file that stores information as plain text in human-readable characters. Text File or TXT file is a widely compatible text format with a small file size. It doesn’t include complex formatting, making it easy for users to create, edit, and view using text editors. Text files commonly end with extensions like .txt. and can be edited with simple text editors.

How to Open Text Files in Linux?

Text Files are simplest file format and can be in using built-in text editor. So to edit a text file in Linux, we need to open it. There are different ways to open file in Linux.

1. Graphical Text Editor: Use a graphical text editor like ‘gedit‘ to open a txt file by typing ‘gedit filename’.

gedit filename

2. Other Text Editor: Other popular text editor like ‘nano’, ‘vi’ or ‘vim’. Open a TXT file by typing their names followed by the file’s name. For example:

nano filename
vi filename
vim filename

How to Edit Text Files in Linux?

We will see how to use these commands to edit text files in Linux with all the commands and suitable images to help you better understand the concepts used here. Let’s start.

Edit Text Files Using Cat Command

This command is mainly used to see what is written in a text file, but it can also be used to create and edit text files. The command to create the text file using the Cat Command is given below:

cat > test.txt
  • Once you execute the above command ,you will be see a cursor waiting for you to enter any text you wish to add or in simple words you can edit the newly created text file.
Edit-Text-Files-in-Linux-using-cat-command

Using cat command

  • After you are done with editing the text file , press CTRL + D to exit.
Edit-Text-Files-in-Linux-using-cat-command---adding-a-sentence

Text file created

Note : If you want to check if the file is created , you may run the command as – ls -l .

Using-ls-command-to-check-if-file-created-successfully

Checking if the file is created successfully using ls command

  • To preview the text file that you have just created , you may run the command given below:
cat test.txt

Preview-a-text-file-using-cat-command

Edit Text Files Using Touch Command

This command is create one or more text files at a time. In simple words you can create single or multiple files using Touch Command.

  • To create a single file , you may run the command given below:
touch test2.txt
  • To create multiple files , you may run the command given below:
touch file1.txt file2.txt file3.txt file4.txt file5.txt
Edit-Text-Files-in-Linux-using-touch-command

Using touch command to create single or multiple files

  • Now the files are created you may use the cat command as cat>file_name.txt to edit them and once edited exit using CTRL + D .

Edit Text Files Using Standard Redirect Symbol

Standard Redirect Symbol command is the simplest method where we can use the standard redirect command to create a single new file . To create a new file , run the command given below :

> test3.txt
Edit-Text-Files-in-Linux-using-standard-redirect-symbol

Using standard redirect symbol

  • Now the files are created you may use the cat command as cat>file_name.txt to edit them and once edited exit using CTRL + D .
  • To check if the file is created successfully use ls -l command.

Edit Text Files Using Nano Command

Nano Command gives you the ability to create as well as edit the text file at the same time .To create a new file , run the command given below :

nano test4.txt
Edit-Text-Files-in-Linux-using-nano-command

Using nano command

  • Executing the above commands will open you a nano editor where you can edit the text file . In order to save this file press CTRL + O and to exit press CTRL + X.
Edit-Text-Files-in-Linux-using-nano-command---nano-editor

Nano editor

  • To check if the file is created successfully use ls -l command.
  • To preview the file use cat>file_name.txt command.

Edit Text Files Using Vi Command

Similar to nano command , Vi Command also gives you the ability to create as well as edit the text file at the same time .To create a new file , run the command given below:

vi test5.txt

Edit-Text-Files-in-Linux-using-vi-command

Using vi command

Edit-Text-Files-in-Linux-using-vi-command---vi-editor

Vi editor

  • Press the ESC key for normal mode.
  • Press i Key for insert mode.
  • Press q! keys to exit from the editor without saving a file.
  • Press wq! Keys to save the updated file and exit from the editor.
  • Press w test.txt to save the file as test.txt

Also check more Basic vi Commands

Edit Text Files Using Vim Command

Similar to Vi command , Vim Command also gives you the ability to create as well as edit the text file at the same time .To create a new file , run the command given below :

vim text6.txt
Pasted-image-2

Using Vim command

Pasted-image-3

Vim editor

Conclusion

This article is all about how to create and edit the text files in Linux that is an Open Source UNIX based OS . Handling text files is a usual task often dealt by any normal developer and this makes it very important to learn how to edit text files in Linux.

We have covered five very basic methods – cat, touch, nano, vi commands and using standard redirect symbol to handle text files with all the commands and suitable images to help you better understand the how to use these commands.

Also Read

Frequently Asked Questions: How to Edit Text Files in Linux?

How to Edit Text Files in Linux?

To edit text files in Linux, you can use a command-line text editor like Nano or Vim. Open the terminal, type the chosen editor’s command followed by the file name, and press Enter. Make changes, save, and exit as per the editor’s instructions. Moreover you can use commands like cat and vi .

How do I edit a text file in a Linux terminal?

To edit a text file in a Linux terminal, use a command-line text editor like Nano or Vim. or Vi . Type the editor’s command followed by the file name, press Enter, make changes, and save. Nano users can use Ctrl + X, while Vim users can use :wq to exit after making the edits in the text file .

Which command is used to edit a file in Linux?

For editing a file in Linux, we commonly used commands liike “nano” and “vim.” To edit a file using Nano, type “nano filename” in the terminal, make changes, press Ctrl + X to exit, and follow the prompts to save. For Vim, type “vim filename,” make changes, and use “:wq” to save and quit.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads