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.
cat – Displays the entire file contentless – Opens file in a scrollable viewer (recommended for large files)more – Similar to less but olderhead – Shows the first few linestail – Shows the last few linesnano or vi – For viewing and editing$ 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
less for reading long files comfortably.tail -f to monitor logs in real time.grep to search within files while viewing.$ tail -f /var/log/syslog
This command will continuously show new log entries as they are written to the file.
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.
Here are some common reasons to view files in a Linux environment:
The cat (short for concatenate) command is one of the simplest and most commonly used tools to view files in Linux.
cat filename
cat /etc/hostname
This displays the entire contents of the file in the terminal window. For very large files, however, cat is not ideal.
less is a pager program that allows you to scroll through large files page-by-page.
less /var/log/syslog
Use the arrow keys to scroll and press q to quit.
Similar to less, but with fewer features. more also displays file content one screen at a time.
more /etc/passwd
Press Enter to scroll line-by-line or Space to scroll page-by-page.
head and tail are great for previewing only the beginning or end of a file.
head filename
tail filename
tail -f /var/log/auth.log
This is useful for real-time log monitoring.
If you want to safely view and scroll a file without editing it, you can open it in read-only mode using editors:
nano filename
vim -R filename
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).
If you're working with compiled files, device data, or other binary formats, use tools like:
hexdump -C filename
xxd filename
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.
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.
Files that start with a dot (.) are hidden by default. Use:
ls -a
cat .bashrc
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.
Use less and search with /pattern to find a specific keyword:
less /var/log/syslog
Then type: /error to search for the word "error"
To just preview a few bytes of a file:
head -c 100 file.txt
find /home -user username -type f -exec cat {} \;
This command is powerful for auditing or gathering reports from user-specific files.
Use sed or awk to view specific lines:
sed -n '5p' file.txt
sed -n '10,20p' file.txt
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.
If you're managing a remote server, use ssh and pipe file viewers:
ssh user@server "cat /etc/nginx/nginx.conf"
To view only lines that match a pattern:
grep "404" access.log
Combine with less for better navigation:
grep "ERROR" logfile.txt | less
Use zcat, zless, or bzcat to view compressed logs:
zcat log.gz
zless log.gz
less or more for large files to avoid freezing the terminaltail -f for real-time logsgrep or awk to filter content as you viewfile before opening unknown filesstat to gather file metadata without opening contentKnowing 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.
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.
ls -l filename
Output example:
-rw-r--r-- 1 user user 2048 Jun 24 12:00 example.txt
rw-: owner can read and writer--: group can readr--: others can readYou can use pattern-matching tools to isolate only the part of the file you want to view.
grep '^error' server.log
grep '[0-9]' data.txt
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
To view contents of all text files in a directory tree:
find . -type f -name "*.txt" -exec cat {} \;
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).
To improve readability of source code, use tools that add syntax highlighting:
sudo apt install bat
bat script.py
This works like cat but with line numbers and color coding.
Linux has several GUI and CLI-based PDF viewers.
evince: GNOME PDF viewerokular: KDE document viewermupdf: Lightweight CLI viewermupdf file.pdf
To preview image files without opening a full GUI environment:
sudo apt install feh
feh image.jpg
Before viewing large files, it’s good to know how big they are or how many lines they contain.
wc -l filename
du -sh filename
mc (Midnight Commander) is a text-based file manager that lets you browse and view files interactively.
sudo apt install mc
mc
Select a file and press F3 to view it, or F4 to edit.
Use tools like ssh, scp, or sftp to connect and view remote files.
ssh user@host 'cat /var/log/nginx/error.log'
To watch for specific keywords as they appear in real time:
tail -f /var/log/syslog | grep --line-buffered "error"
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.
To quickly peek at large binary or log files:
head -c 512 file.txt
This shows only the first 512 bytes.
Sometimes clipboard or temp data is stored in text files. You can use cat, less, or strings to view them:
strings /tmp/tempfile
To quickly inspect CSV or tab-separated files, use column formatting:
column -s, -t < data.csv | less -#2 -N
For structured data formats, readability matters:
jq . file.json
xmllint --format file.xml
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.
To refresh and see file changes every 2 seconds:
watch tail -n 20 /var/log/syslog
List and view files based on modification time:
find . -type f -newermt "today" -exec cat {} \;
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.