How to View Files in Linux

In Linux, you can view the contents of a file directly from the terminal using several built-in commands. These are especially useful when working with configuration files, scripts, and logs.

Common Commands to View Files

Examples

$ cat file.txt
Displays full content

$ less file.txt
Use arrows/PageUp/PageDown to scroll

$ head -n 5 file.txt
Shows first 5 lines

$ tail -n 10 file.txt
Shows last 10 lines

Tips

Use Case: Monitor a Log File

$ tail -f /var/log/syslog

This command will continuously show new log entries as they are written to the file.

How to View Files in Linux – Beginner to Advanced Guide

Linux is a powerful operating system used by developers, system administrators, and tech professionals worldwide. One of the most common tasks in Linux is viewing the contents of a file—whether it's a configuration file, log file, code file, or document. Knowing how to view files quickly and efficiently from the command line can help you troubleshoot issues, edit settings, and manage your system more effectively.

In this comprehensive guide, we will walk through the different ways to view files in Linux using command-line tools, GUI methods, and advanced techniques.

Why View Files in Linux?

Here are some common reasons to view files in a Linux environment:

1. Using the cat Command

The cat (short for concatenate) command is one of the simplest and most commonly used tools to view files in Linux.

Syntax:

cat filename

Example:

cat /etc/hostname

This displays the entire contents of the file in the terminal window. For very large files, however, cat is not ideal.

2. Using the less Command

less is a pager program that allows you to scroll through large files page-by-page.

Example:

less /var/log/syslog

Use the arrow keys to scroll and press q to quit.

3. Using the more Command

Similar to less, but with fewer features. more also displays file content one screen at a time.

Example:

more /etc/passwd

Press Enter to scroll line-by-line or Space to scroll page-by-page.

4. Using the head and tail Commands

head and tail are great for previewing only the beginning or end of a file.

View the first 10 lines of a file:

head filename

View the last 10 lines:

tail filename

Monitor a live file (e.g., logs):

tail -f /var/log/auth.log

This is useful for real-time log monitoring.

5. Using the nano or vim Editors in Read-Only Mode

If you want to safely view and scroll a file without editing it, you can open it in read-only mode using editors:

Using nano:

nano filename

Using vim in read-only mode:

vim -R filename

6. Using GUI File Viewers

If you're on a Linux desktop like Ubuntu, Fedora, or Mint, you can use graphical file managers like:

Right-click the file and choose “Open With Text Editor” (e.g., Gedit, Kate, Mousepad).

7. Viewing Binary Files (with hexdump or xxd)

If you're working with compiled files, device data, or other binary formats, use tools like:

Hex view:

hexdump -C filename

Alternative tool:

xxd filename

8. Use file Command to Identify File Type

Before opening a file, it’s a good idea to check what type it is:

file filename

This tells you if the file is ASCII text, UTF-8, executable, archive, etc.

9. Viewing Multiple Files at Once

You can use cat, less, or head on multiple files:

cat file1.txt file2.txt
less file1.txt file2.txt

This helps in comparing or reviewing files side-by-side.

10. Viewing Hidden Files

Files that start with a dot (.) are hidden by default. Use:

In CLI:

ls -a

To view a hidden file:

cat .bashrc

11. Viewing Large Files Efficiently

For files that are several hundred megabytes or more, avoid using cat. Use:

less largefile.log

You can jump to the end of a file in less using Shift+G, and back to the top with g.

12. Searching While Viewing Files

Use less and search with /pattern to find a specific keyword:

Example:

less /var/log/syslog

Then type: /error to search for the word "error"

13. Previewing File Content Without Opening Entire File

To just preview a few bytes of a file:

head -c 100 file.txt

14. Viewing Files Owned by a Specific User

find /home -user username -type f -exec cat {} \;

This command is powerful for auditing or gathering reports from user-specific files.

15. Viewing Specific Lines in a File

Use sed or awk to view specific lines:

Example: View line 5

sed -n '5p' file.txt

Example: View lines 10 to 20

sed -n '10,20p' file.txt

16. View File Metadata

Sometimes you don’t need the content, just the metadata:

ls -lh filename
stat filename

This includes size, permissions, creation time, last modified time, etc.

17. Viewing Remote Files via SSH

If you're managing a remote server, use ssh and pipe file viewers:

ssh user@server "cat /etc/nginx/nginx.conf"

18. Combine grep with File Viewers

To view only lines that match a pattern:

grep "404" access.log

Combine with less for better navigation:

grep "ERROR" logfile.txt | less

19. Viewing Compressed Files

Use zcat, zless, or bzcat to view compressed logs:

zcat log.gz
zless log.gz

20. Best Practices for Viewing Files in Linux

Final analysis

Knowing how to view files in Linux is essential whether you're managing a server, debugging applications, or working on a development project. From simple cat commands to powerful tools like less, grep, and vim, Linux provides flexible options for reading files. By mastering these tools, you’ll be able to handle any type of file viewing task efficiently and securely.

21. Viewing File Permissions

In Linux, before opening or editing a file, it's important to check who has access. The file permissions dictate whether a user can read, write, or execute a file.

Use ls -l to View Permissions

ls -l filename

Output example:

-rw-r--r-- 1 user user 2048 Jun 24 12:00 example.txt

22. View Only Specific Content in Files

You can use pattern-matching tools to isolate only the part of the file you want to view.

Example: View all lines starting with “error”

grep '^error' server.log

Example: View lines that contain numbers

grep '[0-9]' data.txt

23. View Differences Between Two Files

If you want to compare the contents of two files line-by-line, use the diff command:

diff file1.txt file2.txt

Use sdiff for side-by-side comparison:

sdiff file1.txt file2.txt

24. Viewing Files Recursively in a Directory

To view contents of all text files in a directory tree:

find . -type f -name "*.txt" -exec cat {} \;

25. View Files Opened by a Process

Sometimes you need to see which files are being used by a running application.

lsof -p <PID>

This shows all files opened by the process with the specified PID (Process ID).

26. View File Structure or Syntax Highlighted Files

To improve readability of source code, use tools that add syntax highlighting:

Using bat (better cat):

sudo apt install bat
bat script.py

This works like cat but with line numbers and color coding.

27. View PDF Files on Linux

Linux has several GUI and CLI-based PDF viewers.

Example (CLI):

mupdf file.pdf

28. View Images from Command Line

To preview image files without opening a full GUI environment:

Install feh (image viewer):

sudo apt install feh

Usage:

feh image.jpg

29. View File Size and Line Count

Before viewing large files, it’s good to know how big they are or how many lines they contain.

Count lines:

wc -l filename

Check file size:

du -sh filename

30. View Files Using Midnight Commander

mc (Midnight Commander) is a text-based file manager that lets you browse and view files interactively.

Install and Run:

sudo apt install mc
mc

Select a file and press F3 to view it, or F4 to edit.

31. Viewing Files Over the Network (SFTP, SSH)

Use tools like ssh, scp, or sftp to connect and view remote files.

Example: Read file remotely

ssh user@host 'cat /var/log/nginx/error.log'

32. Using tail with grep to Monitor Specific Log Events

To watch for specific keywords as they appear in real time:

tail -f /var/log/syslog | grep --line-buffered "error"

33. View File Encodings

Knowing the encoding (UTF-8, ASCII, etc.) is important when handling text files in different languages or systems.

file -i filename

This will show something like text/plain; charset=utf-8.

34. Viewing Only the First X Bytes or KBs

To quickly peek at large binary or log files:

head -c 512 file.txt

This shows only the first 512 bytes.

35. Viewing Clipboard or Temp Files

Sometimes clipboard or temp data is stored in text files. You can use cat, less, or strings to view them:

strings /tmp/tempfile

36. Viewing CSV or TSV Files

To quickly inspect CSV or tab-separated files, use column formatting:

column -s, -t < data.csv | less -#2 -N

37. Viewing JSON or XML Files Nicely

For structured data formats, readability matters:

View JSON prettily:

jq . file.json

View XML prettily:

xmllint --format file.xml

38. How to View Files Securely (as Read-Only)

To ensure you're not accidentally editing or modifying system-critical files:

less /etc/shadow
vim -R /etc/fstab

Use read-only options with caution and always check file permissions using ls -l.

39. Use watch Command to Repeatedly View File Output

To refresh and see file changes every 2 seconds:

watch tail -n 20 /var/log/syslog

40. Viewing Files Created or Modified Today

List and view files based on modification time:

find . -type f -newermt "today" -exec cat {} \;

Final Thoughts

Linux gives you extensive power to view, analyze, and manipulate file content using both graphical tools and command-line utilities. Whether you're monitoring logs, debugging code, or inspecting system files, mastering these file viewing techniques will significantly boost your efficiency and system awareness. The more you practice with tools like cat, less, grep, head, vim, and jq, the more comfortable and productive you’ll become in any Linux environment.

See Also