Viewing & editing files

Key takeaways To read a file without opening an app, use cat (dump a small file), less (scroll a big one), or head / tail (just the first or last lines). To edit, nano is the friendly beginner’s editor, while vim is worth surviving because it’s on every server. You’ve already learned to move around and list files in navigating & listing — now let’s look inside them.

Once you can move around the filesystem, the next thing you’ll want is to see what’s in a file — and eventually to change it. Both happen right in the terminal, no window, no mouse. This lesson covers a handful of reading commands and gives you a first, survivable footing in two text editors.

Reading files

cat prints a file straight to the screen. It’s perfect for something short — a config snippet, a note, a small script:

cat notes.txt

The whole file scrolls past. For a long file that’s useless, because the top flies off the screen. That’s where less comes in:

less /var/log/syslog

less takes over the terminal and lets you move through the file at your own pace: arrows or space to scroll, / followed by text to search, n to jump to the next match, and q to quit back to the prompt. Nothing is changed — less is read-only, so it’s safe on anything.

Often you only want a peek. head shows the first lines of a file and tail shows the last:

head config.yml
tail config.yml

Both default to ten lines; -n N sets how many. tail -n 50 app.log shows the last fifty lines — handy because the end of a log is usually the part you care about.

The star of the tail family is tail -f, which follows a file: it prints new lines as they’re written, so you can watch a log grow live while a program runs. Press Ctrl-C to stop watching. You’ll lean on this constantly when monitoring services and reading logs.

Editing with nano

Reading is safe; editing changes the file. The friendliest editor to start with is nano. Open a file (it’s created if it doesn’t exist yet):

nano todo.txt

You just type normally — nano behaves like a plain text box, with your cursor moved by the arrow keys. The clever part is the menu along the bottom, which lists the shortcuts. The ^ symbol there means Ctrl, so:

  • ^O (Ctrl-O) — write Out, i.e. save. Press Enter to confirm the name.
  • ^X (Ctrl-X) — exit. If you have unsaved changes it asks first.

That’s genuinely all you need. nano is the editor to reach for whenever comfort matters more than speed.

Surviving vim

Sooner or later you’ll land on a server where the habit is vim (or its older sibling vi). It’s powerful and fast once learned, but it trips up newcomers because it’s modal — the same keys do different things depending on the mode you’re in, and by default your typing doesn’t go into the file. You don’t need to master it today. You need to not get stuck:

  • Press i to enter insert mode — now typing edits the text as you’d expect.
  • Press Esc to leave insert mode and go back to command mode.
  • From command mode, type :wq and Enter to save and quit.
  • Or type :q! and Enter to quit without saving (throw away changes).

Memorise those four and vim can never trap you. If you’re ever lost, tap Esc a couple of times first — that always returns you to command mode, where :q! is your escape hatch.

Which to use

Both editors edit plain text, and plain text is what almost all Linux configuration is — no hidden formatting, just characters in a file. So the choice is about comfort, not capability:

  • Reach for nano when it’s available and you want a gentle, obvious editor.
  • Fall back to vim when it’s the only thing installed — which, on a bare server, it often is.

Learning just enough of both means no machine can leave you unable to make a quick edit.

Creating files without an editor

You don’t always need an editor at all. The shell can send a command’s output straight into a file with the > redirection operator:

echo "hello" > greeting.txt

echo prints its text, and > points that text into greeting.txt, creating the file (or overwriting it if it already exists). This is just a taste — redirection is a whole toolkit that gets the full treatment in pipes & redirection.

Quick check: you opened a file in less. How do you exit back to the prompt?

Recap

  • cat dumps a short file; less scrolls a long one (arrows/space, / to search, q to quit).
  • head and tail show the first or last lines; -n N sets how many.
  • tail -f follows a file live — the way you watch a log grow.
  • nano is the friendly editor: type normally, ^O to save, ^X to exit.
  • vim is everywhere: i to insert, Esc to leave, :wq to save-and-quit, :q! to bail out.
  • echo "text" > file creates a file with no editor at all.

Next up: finding files & text