The contents of a file in Linux can be displayed using `cat`, `head`, or `tail` depending on your needs. If you are new to Linux or just need a quick refresher, knowing how to view contents of a file in linux is a fundamental skill. This guide covers every major command and technique, from simple viewing to searching and paginating large files.
You will learn the most common commands like `cat`, `less`, `more`, `head`, and `tail`. We also cover how to view binary files, search inside files, and use text editors for quick peeks. Each section includes practical examples you can try right away on your terminal.
How To View Contents Of A File In Linux
The quickest way to see a file’s content is using the `cat` command. But for larger files, you need tools like `less` or `head`. Let’s break down each method step by step.
Using The Cat Command
The `cat` command prints the entire file to the terminal. It’s best for small files or when you want to concatenate multiple files.
- Open your terminal.
- Type:
cat filename.txt - Press Enter. The file content appears on screen.
For example, to view a file named “notes.txt”, run cat notes.txt. If the file is long, the output scrolls quickly. That’s why `cat` is not ideal for large files.
You can also view multiple files at once: cat file1.txt file2.txt. This prints both files one after another.
Using Less For Paginated Viewing
The `less` command lets you scroll through a file page by page. It’s perfect for long log files or documents.
- Type:
less filename.txt - Use the arrow keys to scroll up and down.
- Press
Spaceto go to the next page. - Press
bto go back one page. - Press
qto quit.
You can also search inside `less`. Press / followed by a search term, then press Enter. The cursor jumps to the first match. Press n for the next match.
Another useful feature: less -N filename.txt shows line numbers on the left.
Using More For Basic Pagination
The `more` command is older than `less` but still works. It shows one screen at a time.
- Type:
more filename.txt - Press
Spaceto view the next page. - Press
Enterto scroll line by line. - Press
qto exit.
Unlike `less`, `more` does not let you scroll backward. For most users, `less` is the better choice.
Using Head To View The Beginning
The `head` command shows the first 10 lines of a file by default. Use it to preview a file without opening the whole thing.
- Type:
head filename.txt - To see more lines, use:
head -n 20 filename.txt(shows first 20 lines).
For example, head -n 5 config.txt shows only the first five lines. This is useful for checking headers or configuration files.
Using Tail To View The End
The `tail` command shows the last 10 lines of a file. It’s perfect for checking recent log entries.
- Type:
tail filename.txt - To see more lines:
tail -n 30 filename.txt - To follow a file in real time:
tail -f filename.txt(press Ctrl+C to stop).
Real-time following is great for monitoring log files as they update. For instance, tail -f /var/log/syslog shows new log entries as they appear.
Using The Nl Command For Numbered Lines
The `nl` command displays file content with line numbers. It’s like `cat` but with numbering.
- Type:
nl filename.txt - You can customize numbering style with options like
-ba(number all lines).
Example: nl -ba script.sh shows every line numbered, including blank ones.
Using Od To View Binary Or Special Files
The `od` command dumps file content in octal, hex, or other formats. Use it for binary files or when you need to see raw bytes.
- Type:
od filename.bin - For hex output:
od -x filename.bin - For ASCII output:
od -c filename.bin
This is advanced but useful for debugging or analyzing non-text files.
Using Strings To Extract Text From Binary Files
The `strings` command extracts readable text from binary files. It’s helpful for finding error messages in compiled programs.
- Type:
strings filename.bin - To filter output:
strings filename.bin | grep "error"
Example: strings /usr/bin/ls | head shows text strings from the `ls` binary.
Using Grep To Search Inside Files
The `grep` command searches for patterns inside files. It does not show the whole file, only matching lines.
- Type:
grep "search_term" filename.txt - To show line numbers:
grep -n "search_term" filename.txt - To search recursively:
grep -r "search_term" /path/to/dir
For example, grep "error" /var/log/syslog shows all lines containing “error”. Combine with `cat` or `less` for full context.
Using Text Editors For Quick Viewing
You can also use text editors like `nano`, `vim`, or `emacs` to view files. They are more interactive but heavier than command-line tools.
nano filename.txt– Simple editor, easy to use.vim filename.txt– Powerful but has a learning curve.emacs filename.txt– Highly customizable.
For quick viewing, `nano` is beginner-friendly. Just open the file, read, and press Ctrl+X to exit.
Using The File Command To Identify File Type
Before viewing, you might want to know the file type. The `file` command tells you if it’s text, binary, or something else.
- Type:
file filename - It shows output like “ASCII text” or “ELF 64-bit LSB executable”.
This helps you choose the right viewing method. For example, if it’s a compressed file, you need `zcat` or `gunzip` first.
Viewing Compressed Files
Linux has commands to view compressed files without decompressing them first.
zcat file.gz– View gzip compressed files.bzcat file.bz2– View bzip2 compressed files.xzcat file.xz– View xz compressed files.zless file.gz– View compressed files with pagination.
Example: zcat archive.gz | head shows the first 10 lines of a gzip file.
Using The Stat Command For Metadata
Sometimes you need file metadata like size, permissions, and timestamps. The `stat` command provides this.
- Type:
stat filename.txt - It shows file size, blocks, access time, and more.
This is not direct content viewing, but it helps you understand the file before opening it.
Practical Examples And Use Cases
Here are common scenarios and which command to use:
- Quick check of a config file: Use
head -n 20 config.cfg - Monitor a log file in real time: Use
tail -f /var/log/nginx/access.log - Read a long document: Use
less report.txt - Search for a keyword in a file: Use
grep "TODO" tasks.txt - View a binary file’s text: Use
strings program.bin | less
These commands cover 90% of your file viewing needs on Linux.
Combining Commands For Advanced Viewing
You can pipe commands together for more power. For example:
cat file.txt | grep "error" | head -n 5– Show first 5 lines containing “error”.tail -n 100 log.txt | less– View last 100 lines in a paginated way.strings binary.bin | grep "password"– Find passwords in a binary file.
Piping is a core Linux concept. It lets you chain commands to get exactly what you need.
Common Mistakes To Avoid
New users often make these errors:
- Using `cat` on large files (causes terminal flooding).
- Forgetting to use quotes around search terms in `grep`.
- Not checking file type before viewing (binary files may garble terminal).
- Using `more` when `less` is more flexible.
Avoid these to save time and frustration.
Viewing Files With Special Characters
If a file contains non-printable characters, use `cat -v` to show them visibly. The `-v` flag displays control characters.
- Type:
cat -v filename.txt - Control characters appear as `^` followed by a letter.
This is useful for debugging scripts or data files.
Using The Watch Command For Repeated Viewing
The `watch` command runs a command repeatedly and shows its output. Use it to monitor a file that changes over time.
- Type:
watch -n 2 tail -n 10 log.txt - This updates every 2 seconds, showing the last 10 lines.
Press Ctrl+C to stop.
Viewing Files From Remote Servers
If the file is on a remote server, use SSH to view it:
- Type:
ssh user@server "cat /path/to/file.txt" - Or use:
ssh user@server "less /path/to/file.txt"
You can also use `scp` to copy the file locally first.
Using The Find Command With File Viewing
Combine `find` with `cat` or `less` to view files matching certain criteria.
- Type:
find /home -name "*.txt" -exec cat {} \; - This finds all .txt files in /home and displays their content.
Be careful with large directories to avoid overwhelming output.
Viewing Files With Line Wrapping
By default, long lines wrap to the next line. To disable wrapping in `less`, use the `-S` option:
- Type:
less -S filename.txt - Long lines are truncated, and you scroll horizontally with arrow keys.
This is helpful for files with very long lines, like CSV data.
Using The Cut Command To View Columns
The `cut` command extracts specific columns from a file. It’s not for full file viewing but for focused data.
- Type:
cut -d',' -f1,3 data.csv - This shows the first and third columns, using comma as delimiter.
Combine with `head` to preview: head data.csv | cut -d',' -f1,3.
Viewing Files With Syntax Highlighting
For code files, use `bat` (a modern `cat` clone) for syntax highlighting. Install it via your package manager.
- Type:
bat filename.py - It shows line numbers and colorized syntax.
If `bat` is not available, `highlight` or `pygmentize` also work.
Using The Diff Command To Compare Files
While not strictly viewing, `diff` shows differences between two files. It’s useful for understanding changes.
- Type:
diff file1.txt file2.txt - It shows lines that differ, with markers like `<` and `>`.
For side-by-side view: diff -y file1.txt file2.txt.
Viewing Files With The Ls Command
The `ls` command lists file names, not content. But `ls -l` shows metadata like size and permissions. Use it before viewing to check file size.
- Type:
ls -lh filename.txt - The output shows human-readable file size (e.g., 2.3M).
This helps you decide if `cat` or `less` is appropriate.
Frequently Asked Questions
1. What is the easiest way to view contents of a file in Linux?
The easiest way is using `cat filename.txt` for small files, or `less filename.txt` for larger ones.
2. How do I view the first 10 lines of a file in Linux?
Use the `head` command: head filename.txt. For a different number, use `head -n N filename.txt`.
3. How can I view a file in real time as it updates?
Use `tail -f filename.txt`. This shows new lines as they are added. Press Ctrl+C to stop.
4. What command shows file content with line numbers?
Use `nl filename.txt` or `cat -n filename.txt`. Both display line numbers.
5. How do I search for a word inside a file while viewing?
Use `less filename.txt`, then press `/` and type the word. Press `n` for next match. Alternatively, use `grep “word” filename.txt`.
Now you have a complete toolkit for viewing file contents in Linux. Practice these commands on sample files to build muscle memory. Each command has its strengths, so choose based on your specific need. Whether you are a sysadmin, developer, or casual user, these skills will save you time every day.