Open In App

How to Edit Text Files in Linux?

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

Using cat command

Text file created

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

Checking if the file is created successfully using ls command

cat test.txt

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.

touch test2.txt
touch file1.txt file2.txt file3.txt file4.txt file5.txt

Using touch command to create single or multiple files

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

Using standard redirect symbol

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

Using nano command

Nano editor

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

Using vi command

Vi editor

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

Using Vim command

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.


Article Tags :