Viewing File Content: cat
, less
, nano
, vim
1. cat
: simple file content viewer
Viewing and editing text files are basic skills for any Linux user. Configuration files, error logs, source code — all of these are simple text documents. Forget about Word and Notepad, because in the Linux world it's all replaced by commands and programs like cat
, less
, nano
, and vim
. Today we’ll start with something simple and move on to more powerful tools.
What is cat
?
cat
stands for "concatenate." But it's more often used for simple and quick file content viewing.
Syntax:
cat [file_name]
Usage Example:
Let’s create a sample file and check its contents:
echo "Hello, Linux!" > example.txt
cat example.txt
Output:
Hello, Linux!
Features:
- Outputs the file content as-is, without page breaks. If the file is big, everything will just spill out into the terminal uncontrollably.
It can also be used to combine files:
cat file1.txt file2.txt > combined.txt
Typical mistake: If you forget to specify a file name, cat
will just wait for input "out of thin air." To exit that state, press Ctrl+D
.
2. less
: viewing large files
Why is less
cooler than cat
?
When a file has more than a few lines, cat
becomes inconvenient — scrolling through the text gets tricky. That's when the "less pushy brother" steps in — the less
command.
Syntax:
less [file_name]
Example of usage:
Let's try opening a file:
less example.txt
Navigation inside less
:
Key | Action |
---|---|
Space |
Go to the next page |
b |
Return to the previous page |
/text |
Search for text in the file |
q |
Exit the less program |
Features:
- Works faster with large files.
- Doesn't load the entire content right away but shows only the necessary parts (handy for files with tens of thousands of lines).
If you ever run into issues displaying Cyrillic characters in less
, make sure the file's encoding is compatible with your locale. For example, files with UTF-8
encoding are read perfectly.
3. nano
: a simple text editor
Why do you need an editor?
Sometimes just viewing a file isn't enough—you want to make some changes (like tweaking system settings or adding comments to code). This is where real programming begins, and your first handy tool will be nano
.
Syntax:
nano [file_name]
If the file doesn’t exist, nano
will offer to create it.
Example:
nano example.txt
After running this command, a text editor with an interface similar to Notepad (but in the terminal) will open. Add some text, for example:
I love Linux.
Working with nano
:
The nano
commands show up at the bottom of the window. Key ones include:
Combination | Action |
---|---|
Ctrl+O |
Save the file |
Ctrl+X |
Exit the editor |
Ctrl+K |
Cut a line |
Ctrl+U |
Paste a line |
Features:
- Simplicity makes
nano
a great choice for beginners. - Doesn't require much time to learn, and its GUI isn’t intimidating.
Common mistake: Forgetting to save the file after editing. If you're used to closing everything with an X button, here Ctrl+O
and Ctrl+X
are your friends.
4. vim
: a powerful tool for true geeks
Why do you need vim
?
If you want to become "that programmer" who edits files at the speed of thought, you need vim
. This editor is a legend among developers thanks to its flexibility and powerful features. But get ready: it’ll be tough at first.
Syntax:
vim [file_name]
Example:
vim example.txt
Navigation through modes
vim
can work in several modes. Here are the two main ones:
- Command mode (default).
- Used for navigation and executing commands.
- Switch to edit mode: press
i
.
- Edit mode.
- You can modify text.
- Switch back to command mode: press
Esc
.
Main commands in vim
:
Command | Action |
---|---|
i |
Switch to edit mode |
Esc |
Switch back to command mode |
:w |
Save the file |
:q |
Exit the editor |
:wq |
Save and exit |
Example of use:
Open the file:
vim example.txt
- Press
i
to enter edit mode. Add text:
Hello, vim world!
- Return to command mode (
Esc
) and save the file by typing:wq
.
Features:
- Powerful, advanced editor with plugin support.
- High learning curve: helps you become a truly efficient developer.
Typical mistake: Getting stuck in the editor and not knowing how to exit. If this happens, just press Esc
, then type :q!
(exit without saving).
5. Tool Comparison
Tool | Usage | Advantages | Disadvantages |
---|---|---|---|
cat |
Quickly view file contents | Simplicity | Inconvenient for large files |
less |
View large files | Page-by-page navigation | View-only, no editing |
nano |
Simple text editor | Easy to learn | Not very functional for complex tasks |
vim |
Professional editor | Tons of features | High learning curve |
Practical Exercise
Let's practice working with files:
Create a file
test.txt
:nano test.txt
Type some text you like, save, and exit the file.
- View the file contents using
cat
:cat test.txt
- Open the file with
less
and search for a word (e.g.,/Linux
). - Next, edit the file using
vim
. Add another line of text, save, and exit.
These exercises will help you get comfortable with tools for navigating and editing text files, which are useful for any day-to-day Linux tasks.
GO TO FULL VERSION