Linux viewing files
---
title: "Viewing File Contents"
category: "linux-tutorials"
description: "Using commands like 'more', 'cat', 'head', and 'tail' to display the contents of text files."
---
# Viewing File Contents
This document explains how to view the contents of text files in the Linux command line environment using common utilities like `cat`, `more`, `head`, and `tail`. These commands allow you to display entire files or specific portions of them directly in your terminal.
## Displaying Entire File Content
There are several ways to display the full content of a file.
### `cat`: Concatenate and Display Files
The `cat` command reads files sequentially, writing them to standard output. It's commonly used to display the contents of short files or to concatenate multiple files.
**Syntax:**
```bash
cat [options] [filename...]
Example: Display a single file
cat my_file.txt
This command will print the entire content of my_file.txt
to the terminal.
Example: Using cat
with pipes
The cat
command can be piped (|
) to other commands for further processing. For instance, searching within a file:
cat costdata.dat | grep searchstring
This command displays the contents of costdata.dat
and pipes the output to grep
, which then prints only the lines containing "searchstring".
Getting Help:
To see options for the cat
command, use the --help
flag:
cat --help
more
: Display File Content Page by Page
The more
command displays the contents of a file one screen-full at a time. This is useful for viewing larger files that don't fit entirely on the screen.
Syntax:
more [filename]
Example:
more costdata.dat
Usage:
- When the file content exceeds the screen size,
more
pauses after displaying the first page. - Press the Spacebar to advance to the next page.
- Press the
q
key to quitmore
and return to the command prompt.
Example: Using more
with pipes
more
is often used with pipes to paginate the output of other commands:
ls -l | more
This command lists the directory contents in long format and pipes the output to more
, allowing you to view the list page by page if it's long.
Displaying Parts of a File
Sometimes you only need to see the beginning or end of a file.
head
: Display the Beginning of a File
The head
command displays the first few lines of a file (10 lines by default).
Syntax:
head [options] [filename...]
Example:
head my_large_file.log
This displays the first 10 lines of my_large_file.log
.
Example: Specify number of lines
Use the -n
option to specify a different number of lines:
head -n 20 my_large_file.log
This displays the first 20 lines.
tail
: Display the End of a File
The tail
command displays the last few lines of a file (10 lines by default). This is particularly useful for checking log files or the end of large datasets.
Syntax:
tail [options] [filename...]
Example:
tail my_large_file.log
This displays the last 10 lines of my_large_file.log
.
Example: Specify number of lines
Use the -n
option to specify a different number of lines:
tail -n 50 my_large_file.log
This displays the last 50 lines.
Example: Follow file changes
The -f
option allows you to "follow" a file, displaying new lines as they are added (useful for monitoring logs in real-time).
tail -f application.log
Press Ctrl+C
to stop following the file.
Breaking Out of Commands
If a command like more
or a long cat
seems stuck, or if you want to stop a command like tail -f
, you can usually interrupt it by pressing Ctrl+C
. This will cancel the current command and return you to the command prompt.
```