LearnAI ToolsCareerPractice BuildsPlayDevelopmentContact
Lesson 513 min read

Viewing and Editing Files: cat, less, head, tail and nano

Read files with cat, less, head and tail, write text with echo and redirection, and edit files in the terminal with nano.

On this page 12 sections

Introduction

Linux keeps almost everything in plain text files: configuration, logs, scripts and code. Being able to read and edit them quickly from the terminal is essential, especially on servers where there is no graphical editor. This lesson covers the handful of commands you will use every day for it.

What You Will Learn
  • How to write text into files with echo, > and >>.
  • How to read files with cat, head, tail and less.
  • How to follow a growing log file live with tail -f.
  • How to edit files in the terminal with nano.

Writing Text with echo

echo prints text to the screen. The > symbol redirects that text into a file instead (creating it if needed):

echo "Buy milk" > shopping.txt
cat shopping.txt
Output

Click Run to see what this code prints.

>> appends to the end of a file instead of replacing it:

echo "Buy bread" >> shopping.txt
echo "Buy eggs" >> shopping.txt
cat shopping.txt
Output

Click Run to see what this code prints.

> Replaces the Whole File

A single > empties the file before writing. Typing > when you meant >> silently deletes everything that was in the file.

echo "Buy rice" > shopping.txt
cat shopping.txt
Output

Click Run to see what this code prints.

Mini Challenge · Fix the Command

Priya wants to add the line “Call the bank” to the end of todo.txt without losing what is already there. Which command does that?

Choose one answer

Showing a File: cat

cat prints a whole file. -n numbers the lines, which is handy when an error message mentions a line number:

cat -n shopping.txt
Output

Click Run to see what this code prints.

The name comes from “concatenate”: cat a.txt b.txt prints both files one after another, and cat a.txt b.txt > both.txt joins them into a new file.

Big Files: head, tail and wc

For a long file like a log, printing everything floods the screen. wc -l counts the lines first:

wc -l app.log
Output

Click Run to see what this code prints.

head shows the first lines and tail the last ones; -n says how many (the default is 10):

head -n 3 app.log
Output

Click Run to see what this code prints.

tail -n 3 app.log
Output

Click Run to see what this code prints.

Watch a Log Live with tail -f

tail -f app.log keeps the file open and prints new lines the moment they are written — the standard way to watch a server’s log while you test it. Press Ctrl+C to stop watching.

Scrolling with less

less app.log opens a file in a scrollable viewer that loads instantly even for huge files. It is also what man and git log use. The keys to know:

KeyAction
Space / bPage down / page up
Arrow keysScroll a line at a time
g / GJump to the start / end
/wordSearch forward for “word”; n jumps to the next match
qQuit and return to the prompt

Editing with nano

nano is a simple terminal text editor installed on most systems. Open a file with nano notes.txt (it is created if it does not exist), type normally, and use the shortcuts shown at the bottom of the screen, where ^ means the Ctrl key:

ShortcutAction
Ctrl+O, then EnterSave (“write Out”)
Ctrl+XExit (it asks to save if there are changes)
Ctrl+WSearch ("Where is")
Ctrl+K / Ctrl+UCut a line / paste it
Alt+UUndo
nano --version | head -1
Output

Click Run to see what this code prints.

You will also meet Vim (vi), a powerful editor that many servers have by default. If you open it by accident, press Esc, type :q! and press Enter to quit without saving.

Mini Challenge · Put It in Order

Put the steps of editing a configuration file with nano in order.

  1. Press Ctrl+O and Enter to save
  2. Open it with nano settings.conf
  3. Move to the line with the arrow keys and make your change
  4. Check the result with cat settings.conf
  5. Press Ctrl+X to exit

Multi-Line Text: Here-Documents

To write several lines into a file in one go — common in tutorials and scripts — use a here-document. Everything up to the line containing only EOF goes into the file:

cat > notes.md << 'EOF'
# Linux notes
- ls lists files
- cd changes directory
EOF
cat notes.md
Output

Click Run to see what this code prints.

What Kind of File Is It?

Linux does not rely on file extensions. The file command looks at the contents to tell you what something really is:

file shopping.txt notes.md /etc
Output

Click Run to see what this code prints.

This is a good check before running cat on something unknown — printing a binary file such as an image fills the screen with garbage characters.

Common Mistakes

Avoid These Mistakes
  • Using > instead of >> and wiping a file.
  • Running cat on a huge log and waiting for thousands of lines to scroll past — use less, head or tail.
  • Getting stuck in less or vim — q quits less; Esc then :q! quits vim.
  • Editing a system file without first making a copy (cp file file.bak).

Frequently Asked Questions

Start with nano — it shows its shortcuts on screen. Learn the basics of Vim later, because it is available on almost every server, and some developers come to prefer it.

On your own machine, yes: in WSL, code . opens the current folder in VS Code. On remote servers, a terminal editor is often quicker, or you can use VS Code’s Remote SSH extension.

Both page through files. less is newer and can scroll backwards and search, so it is the one to use.

Key Takeaways

  • > writes to a file (replacing it); >> appends to the end.
  • cat prints a file, head and tail show its start and end, and wc -l counts lines.
  • less scrolls through big files; tail -f follows a log live.
  • nano edits files in the terminal: Ctrl+O saves, Ctrl+X exits.
  • file identifies a file’s real type from its contents.

Summary

You can now read and edit files from the terminal. But you may already have seen a “Permission denied” message — next you will learn how Linux decides who can read, change and run each file.

Next Lesson →

Permissions and Ownership: chmod, chown and sudo