Home Linux BasicsWorking with Text Files in Linux: cat, less, head, tail, nano and vim Basics

Working with Text Files in Linux: cat, less, head, tail, nano and vim Basics

by Knowledge Base Linux
Working with Text Files in Linux — Working with Text Files in Linux: cat, less, head, tail, nano and vim Basics

On Linux, almost everything is a text file: settings, logs, notes, and scripts. That is why learning to handle text files in Linux is a superpower. In this guide you will view text files in Linux with four small tools – cat, less, head, and tail – and then edit them with the two editors every beginner should know, the friendly nano and the famous vim. This article covers Working with Text Files in Linux in detail.

Viewing Text Files in Linux with cat and less – Working with Text Files in Linux

The quickest viewer is cat. It prints a file’s whole content to the screen:

$ cat shopping.txt
milk
eggs
coffee

For short files that is perfect. For long files, cat is like unrolling a scroll down a hill – everything rushes past. That is what less is for. It shows one screen at a time; move with the arrow keys, search with /word, and quit with q:

$ less /var/log/syslog

A bonus of cat: it can join (“concatenate” – that is where the name comes from) several files into one:

$ cat part1.txt part2.txt > combined.txt

Peeking at the Start and End with head and tail – Working with Text Files in Linux

Often you only need the beginning or the end of a file. head shows the first 10 lines, tail shows the last 10, and the -n option changes how many:

$ head -n 5 names.txt     # first 5 lines
$ tail -n 20 app.log      # last 20 lines

The real magic is tail -f (“follow”). It keeps the file open and prints new lines the moment they appear – the standard way to watch a log while something happens:

$ tail -f /var/log/syslog

Press Ctrl + C when you have seen enough. These four tools pair beautifully with the basics from our beginner’s terminal guide, and with the search tools you will meet next.

Text files in Linux - two editor windows side by side, nano and vim style

Editing Text Files in Linux with nano and vim

Viewing is half the story; sooner or later you must change a file. Start with nano, the beginner-friendly editor. Open a file, type normally, and use the shortcuts listed at the bottom of the screen (the ^ symbol means the Ctrl key):

$ nano notes.txt
# Ctrl+O = save, Ctrl+X = exit, Ctrl+W = search

Then there is vim, the powerful editor found on virtually every Linux machine. Vim has “modes,” which confuses newcomers: in Normal mode keys are commands, and you press i to switch to Insert mode and type. Press Esc to go back, then type :wq and Enter to save and quit, or :q! to quit without saving. The classic beginner joke – “I can’t exit vim!” – ends the moment you remember those two commands. You do not need vim’s thousand features on day one; open, edit, save is enough.

Saving Command Output Into a Text File

Here is a trick that makes everything else in this guide twice as useful: you can send a command’s output into a file instead of the screen, using the > symbol (called redirection). Whatever the command would have printed becomes the file’s content:

$ ls -la > listing.txt        # save a folder listing
$ date >> diary.txt          # >> appends instead of overwriting
$ cat listing.txt             # read it back any time

The single arrow replaces the file’s contents; the double arrow adds to the end, which is how simple logs and diaries are born. Be careful with the single arrow – it empties the target file without asking, even if the command itself then fails. A gentler tool, tee, shows the output on screen and saves it at the same time: ls -la | tee listing.txt. That vertical bar is a pipe, sending one command’s output into another – an idea you will use constantly once you start searching with grep. When you redirect, get into the habit of printing to the screen first and only saving once the result looks right – that two-second check prevents almost every redirection accident.

Common Beginner Mistakes with Text Files

  • Using cat on a huge log. It floods the screen. Reach for less or tail instead.
  • Editing system files without sudo – or with it carelessly. Nano will tell you a file is read-only; that is your cue to think before forcing anything.
  • Getting trapped in vim. Esc, then :q!, Enter. You are free.
  • Forgetting that combining files with > overwrites. cat a.txt > b.txt replaces b.txt entirely. Use >> to append instead.

Conclusion

Viewing text files in Linux comes down to four verbs – cat for quick prints, less for long reads, head and tail for the edges – and editing starts happily with nano, with vim waiting when you want more power. Once you can read files, the next skill is finding things inside them: our guide to searching with find and grep shows you how, and the official nano documentation covers every shortcut when you are ready.

You may also like

KbLinux

Typically replies within a day

Hello, Welcome to the site. Please click below button for chatting me through Telegram.