SyntaxStudy
Sign Up
Linux / Bash Viewing and Editing File Content
Linux / Bash Beginner 1 min read

Viewing and Editing File Content

Reading file content efficiently is a fundamental skill. `cat` prints the entire file to standard output — useful for short files or piping content into other commands. For long files, `less` provides a paginated view with search capability (press `/` to search, `q` to quit). `head` and `tail` display the first or last N lines, with `tail -f` following a log file in real time. For text editing directly in the terminal, `nano` is the most approachable editor for beginners with on-screen key bindings. `vim` is ubiquitous on servers and, once learned, extremely powerful — it opens in normal mode, press `i` to insert text, `Esc` to return to normal mode, and `:wq` to save and quit. `vi` is available on almost every Unix-like system as a fallback. The `wc` command counts lines, words, and characters. `file` identifies a file type without relying on extensions. `stat` displays detailed metadata about a file including inode number, block allocation, all timestamps, and permissions in octal notation.
Example
# Display entire file
cat /etc/hosts

# Display with line numbers
cat -n /etc/os-release

# Paginate through a large file
less /var/log/syslog
# Inside less: /pattern  search | n  next | q  quit | G  end | g  start

# Show first 10 lines (default)
head /var/log/syslog

# Show first 20 lines
head -n 20 /var/log/syslog

# Show last 10 lines
tail /var/log/syslog

# Follow a log file in real time
tail -f /var/log/auth.log

# Follow and show last 50 lines
tail -n 50 -f /var/log/syslog

# Count lines, words, characters
wc /etc/passwd
wc -l /etc/passwd     # lines only

# Identify file type
file /bin/bash
# /bin/bash: ELF 64-bit LSB pie executable, x86-64 ...
file image.png
# image.png: PNG image data, 800 x 600, 8-bit/color RGB

# Detailed file metadata
stat /etc/hosts

# Edit with nano (beginner-friendly)
nano /tmp/test.txt
# Ctrl+O  save | Ctrl+X  exit

# Quick vim cheat-sheet (as comments)
# vim file.txt
# i       — enter insert mode
# Esc     — return to normal mode
# :w      — save
# :q      — quit
# :wq     — save and quit
# :q!     — quit without saving
# dd      — delete current line