Mastering Linux File Inspection, Filtering, Archiving, and Terminal Editors

Viewing and Navigating File Streams

The cat utility dumps an entire file's contents directly to standard output. For large datasets requiring paginated reading, more or less provide an interactive terminal viewer.

cat /etc/os-release
less /etc/nginx/nginx.conf

Press q inside the pager to exit cleanly.

Extracting Boundary Lines

head and tail stream lines from the top and bottom of a document, respectively. Both accept the -n parameter to define record counts.

head -n 10 /var/log/syslog
tail -n 5 /var/www/logs/error.log

Chaining these tools via pipe operators enables precise range extraction. To capture lines 11 through 15:

head -n 15 /tmp/app.log | tail -n 5
# Alternate approach: fetch the final three records from the initial twenty lines
head -n 20 /tmp/app.log | tail -n 3

Pattern Searching and Stream Filtering

grep scans input streams for regex matches, printing corresponding lines. Omitting flags returns all hits; adding modifiers refines output scope.

grep "timeout exceeded" /var/log/proxy/access.log
grep "^FATAL" /var/log/application.log
grep "deprecated"$ /var/log/debug.log

The -v flag inverts selection, suppressing matched records. Pipes allow multi-stage filtering without intermediate files:

df -h | grep "/srv/storage$"
ls -laR | grep "^d"

Sanitizing Configuration Files: Remove comment headers and empty lines to isolate active directives:

grep -Ev '^\s*(#|$)' /etc/pam.d/common-auth

Multi-Criteria Filtering: Chain multiple conditions to narrow results dynamically:

grep -Ev '^\s*(#|$)' /etc/pam.d/common-auth | grep Listen | grep ":[0-9]+$"

Record Counting

wc -l tallies newline characters. When routed through a pipe, it evaluates standard input volume rather than disk files.

wc -l /var/log/syslog
grep -Ev '^\s*(#|$)' /etc/pam.d/common-auth | wc -l

This combination yields the precise count of functional configuration statements. Find operations also integrate seamlessly:

find /workspace -type f -name "*.tmp" | wc -l

Archive Bundling and Decompression

tar consolidates multiple entities into container archives. Compression algorithms are toggled via flags:

Creating Archives:

tar -czf /backup/site_config.tar.gz /etc/ssl/certs /usr/local/bin
tar -cjf /backup/manual_docs.tar.bz2 /usr/share/info /usr/share/man

Restoring Data: Define extraction targets using -C.

tar -xzf /backup/site_config.tar.gz -C /restore_point/tmp

Inspecting Archives:

tar -tzf /backup/site_config.tar.gz
tar -tjf /backup/manual_docs.tar.bz2

Command-Line Text Processing (Vim)

vim utilizes three distinct operational phases:

  • Command Phase: Initial launch state. Keyboard strokes execute shortcuts. Type i, a, or O to enter editing mode.
  • Insert Phase: Standard text entry state. Hit Esc to revert to Command Phase.
  • Ex Phase: Activated by :. Handles persistence, window management, and bulk find/replace. Exit via Esc.

Cursor Navigation & Buffer Editing (Command Phase):

gg          # Move to top boundary
G           # Move to bottom boundary
:75         # Navigate to line seventy-five
x / Backspace # Remove character beneath cursor
dd          # Erase current row
8dd         # Strip eight rows downward
yy          # Duplicate current row
4yy         # Clone four rows downward
p           # Inject clipboard below cursor
P           # Inject clipboard above cursor
/pattern    # Advance forward through matches
?pattern    # Retreat backward through matches
n / N       # Cycle next / previous match
u           # Reverse latest modification
ZZ          # Commit changes and exit

Ex Phase Operations:

:w              # Persist modifications
:w /tmp/draft.md # Export duplicate to target location
:q!             # Discard and terminate
:e ~/notes/dev_tasks.md # Load alternate buffer
:r /etc/protocols # Import external file content at cursor anchor
:sp /usr/include/stddef.h # Initiate vertical pane division

Switching split windows: Maintain Ctrl + w, release left hand, navigate using directional keys.

Bulk Substitution:

:50,90 s/API/library/g   # Swap API for library across rows 50-90
:% s/default_value/production_key/gc # Interactive global replacement engine

The g modifier forces complete line-wide replacement. Suffixing c triggers verification prompts before applying each substitution.

Tags: Linux CLI grep tar Vim editor shell-pipes

Posted on Fri, 05 Jun 2026 17:34:40 +0000 by erfg1