4.2. Viewing Data#

Before you can process data, you need to see it. This section covers tools to view, inspect, and understand file contents.

4.2.1. Quick Reference#

Tool

Purpose

Use When

cat

Display file

Small files, piping

head

First N lines

Previewing large files

tail

Last N lines

Monitoring logs, checking end

less

Interactive viewer

Large files, exploration

file

Identify type

Unknown files

wc

Count lines/words

Statistics

hexdump

Binary data

Debugging binary files

4.2.2. Combining for Analysis#

# Check if file is text or binary
$ file data.dat
data.dat: ASCII text

# View first few lines
$ head -5 data.dat

# Count total lines
$ wc -l data.dat

# View last few entries
$ tail -10 data.dat

# Find specific patterns
$ grep -c "ERROR" data.dat

4.2.3. od: Octal/Hex Dump#

Another tool for viewing binary data (alternative to hexdump).

# Hex format
$ od -x file

# Decimal format
$ od -d file

# ASCII format
$ od -c file

4.2.3.1. When to Use#

  • Debugging binary files

  • Examining corrupt data

  • Understanding file formats

4.2.3.2. Basic Usage#

# View in hex and ASCII
$ hexdump -C binary_file | head -20
00000000  7f 45 4c 46 01 01 01 00  00 00 00 00 00 00 00 00  |.ELF............|
00000010  02 00 03 00 01 00 00 00  54 80 01 08 34 00 00 00  |........T...4...|
                                          hex data                     ASCII representation

# View just hex
$ hexdump -C file | head

4.2.4. hexdump: View Binary Data#

For binary files, see hex representation and ASCII.

4.2.4.1. Real-World Uses#

# How many errors in a log?
$ grep "ERROR" app.log | wc -l
1523

# How many lines in a dataset?
$ wc -l data.csv
50000 data.csv

# Total lines in all log files
$ wc -l /var/log/*.log
  100 /var/log/auth.log
  200 /var/log/syslog
  1000 /var/log/app.log
  1300 total

4.2.4.2. Basic Usage#

# Count all three
$ wc file.txt
  10  25  120 file.txt
        ↑
lines words bytes

# Count lines only
$ wc -l file.txt
10 file.txt

# Count words only
$ wc -w file.txt
25 file.txt

# Count bytes only
$ wc -c file.txt
120 file.txt

# Multiple files
$ wc -l *.txt
  10 file1.txt
  20 file2.txt
  30 total

4.2.5. wc: Count Lines, Words, Bytes#

Count lines, words, and characters.

4.2.5.1. Why This Matters#

# Before using grep on unknown file:
$ file mystery.dat
mystery.dat: ELF 64-bit LSB executable

# Don't grep a binary! Use strings instead:
$ strings mystery.dat | grep "pattern"

4.2.5.2. Basic Usage#

# Check file type
$ file script.sh
script.sh: Bourne-Again shell script text executable

$ file /bin/ls
/bin/ls: ELF 64-bit LSB executable, x86-64

$ file image.jpg
image.jpg: JPEG image data, JFIF standard 1.01

$ file data.csv
data.csv: ASCII text

# Check multiple files
$ file *
script.sh:       Bourne-Again shell script text executable
image.jpg:       JPEG image data, JFIF standard 1.01
data.csv:        ASCII text
binary_file:     ELF 64-bit LSB executable

4.2.6. file: Determine File Type#

Identify what kind of file you’re dealing with.

4.2.6.1. Real Example#

# View a large log file
$ less /var/log/syslog

# Inside less:
# Press 'G' to jump to end
# Type '/ERROR' to search
# Press 'n' to find next ERROR
# Press 'q' to exit

4.2.6.3. Basic Usage#

$ less /var/log/syslog
# Displays file with interactive controls

4.2.7. less: Interactive Paging#

For viewing large files interactively.

4.2.7.1. Real-World Example#

# Check last error in log
$ tail -20 app.log

# Monitor a running application
$ tail -f /var/log/app.log &
# Shows new entries in real-time

# Get middle section of file
$ head -100 file.txt | tail -10
# Lines 91-100

4.2.7.2. tail: Last N Lines#

# Last 10 lines (default)
$ tail file.txt

# Last 3 lines
$ tail -n 3 file.txt

# Follow a growing file (like logs)
$ tail -f /var/log/syslog
# Keeps printing as new lines are added
# Press Ctrl+C to stop

# Start from line 100 onward
$ tail -n +100 file.txt

4.2.7.3. head: First N Lines#

# First 10 lines (default)
$ head file.txt

# First 5 lines
$ head -n 5 file.txt
Line 1
Line 2
Line 3
Line 4
Line 5

# First 5 lines (shorthand)
$ head -5 file.txt

# All but last 10 lines
$ head -n -10 file.txt

4.2.8. head and tail: First/Last Lines#

View the beginning or end of files—useful for large files.

4.2.8.1. When to Use cat#

✓ Small files
✓ Quick viewing
✓ Piping to other commands
❌ Large files (use less instead)
❌ Binary files (use file or hexdump)

4.2.8.2. Basic Usage#

# View a file
$ cat file.txt
Line 1
Line 2
Line 3

# View multiple files (concatenates them)
$ cat file1.txt file2.txt
Line 1 (file1)
Line 2 (file1)
Line 1 (file2)
Line 2 (file2)

# View with line numbers
$ cat -n file.txt
     1  Line 1
     2  Line 2
     3  Line 3

# Show non-printing characters (useful for debugging)
$ cat -A file.txt
Line 1$
Line 2$
Line 3$
# $ shows end of line (or nothing if different line ending)

4.2.9. cat: Viewing File Contents#

The simplest tool: concatenate and print file contents.