YYisak
Published on

Accessing and Editing text files in Linux

Authors

Introduction

In this blog post, I'll guide you through the step-by-step process, to access and edit text files in Linux. For this guide I'm using Ubuntu operating system.

Definitions and Tools

What is a text file?

A text file is a computer file that stores a typed document as a series of alphanumeric characters, usually without visual formatting information. The content may be configuration settings, scripts, notes, documents, a personal note, a journal, a book, or any other text that can be represented and edited in plain text. By plain text meaning meaning they don't contain formatting information like fonts or colors.

We can navigate the file system and view text files using the command line interface in Linux. Here are some basic commands to navigate the file system and view text files:

  1. Common commands for navigation include: pwd, cd, ls

  2. Common command for viewing text files:

    • cat command displays the content of a text file on the screen.

      • Eg: this will display the contents of "myfile.txt".
      zsh
      $ cat myfile.txt
      
    • head command displays the first few lines of a text file.

      • Eg: this will display the first 5 lines of "myfile.txt" (use -n to specify the number of lines).
      zsh
      $ head -n 5 myfile.txt
      
    • tail command displays the last few lines of a text file. - Eg: this will display the last 2 lines of "myfile.txt" (use -n to specify the number of lines).

      zsh
      $ tail -n 2 myfile.txt
      

NOTE

These commands only display the contents, they don't modify the file itself.

Editing Text Files

Linux systems offer various text editors to choose from. The two primary types of text editors available for Linux are GUI text editors and terminal-based text editors.

  1. GUI based editors* (sudo apt-get install gedit, nedit, geany, kate)

    GUI based editors are easy to use and provide a graphical interface for editing text files. Some GUI text editors include:

    • Gedit,
    • nedit,
    • geany,
    • kate,
    • sublime text
  2. Terminal based text editors (sudo apt-get install vi, nano, vim, emacs)

    Terminal-based editors are text editors that operate fully within a terminal window and do not require a graphical user interface (GUI). Some terminal-based text editors include:

    • vi
    • Nano
    • Vim
    • Emacs

Nano (Terminal based text editors)

The Nano editor was developed as a completely open source editor that is loosely based on Pico, as the license for Pico is not an open source license and forbids making changes and distributing it. While Nano is simple and easy to use, it doesn’t offer the extensive suite of more advanced editing and key binding features that an editor like Vi does. Administrators should strive to gain some basic familiarity with Vi, though, because it is available on almost every Linux system in existence.

In order to save this file press CTRL + O and to exit press CTRL + X.

Vi (Terminal based text editors)

Vi is a terminal-based text editor. It is a modal editor, which means it has different modes for inserting and manipulating text. Vi is a powerful editor that can be difficult to learn, but once you master it, you can edit text files quickly and efficiently.

Vi editor has various modes like command mode, insert mode, When starting Vi, it opens with normal mode, which is basically a command mode. In this mode, whatever we type is considered as a command, not as input.

  • 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 testv.txt to save the file as test.txt

Conclusion

In this post, we covered the basics of accessing and editing text files in Linux. We learned how view text files and edit text files using various text editors. By mastering these skills, you can work with text files in Linux environments.