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.
- 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.txtcat shopping.txtClick Run to see what this code prints.
>> appends to the end of a file instead of replacing it:
echo "Buy bread" >> shopping.txtecho "Buy eggs" >> shopping.txtcat shopping.txtClick Run to see what this code prints.
A single > empties the file before writing. Typing > when you meant >> silently deletes everything that was in the file.
echo "Buy rice" > shopping.txtcat shopping.txtClick Run to see what this code prints.
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?
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.txtClick 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.logClick 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.logClick Run to see what this code prints.
tail -n 3 app.logClick Run to see what this code prints.
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:
| Key | Action |
|---|---|
| Space / b | Page down / page up |
| Arrow keys | Scroll a line at a time |
| g / G | Jump to the start / end |
| /word | Search forward for “word”; n jumps to the next match |
| q | Quit 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:
| Shortcut | Action |
|---|---|
| Ctrl+O, then Enter | Save (“write Out”) |
| Ctrl+X | Exit (it asks to save if there are changes) |
| Ctrl+W | Search ("Where is") |
| Ctrl+K / Ctrl+U | Cut a line / paste it |
| Alt+U | Undo |
nano --version | head -1Click 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.
Put the steps of editing a configuration file with nano in order.
- Press Ctrl+O and Enter to save
- Open it with nano settings.conf
- Move to the line with the arrow keys and make your change
- Check the result with cat settings.conf
- 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 directoryEOFcat notes.mdClick 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 /etcClick 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
- 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.