Linux/Unix Tutorial
Estimated reading: 4 minutes 284 views

Linux/Unix: File Viewing, Redirection & Filters – View, Manipulate & Chain Output Like a Pro

Introduction – Master Shell Text Flow

In the Linux/Unix shell, managing text files and command output is a core daily task. Whether you’re reading logs, extracting columns, or redirecting output to a file, these tools make the shell powerful, scriptable, and flexible.

In this guide, you’ll learn:

  • How to view file content using commands like cat, less, head, and tail
  • How to extract/manipulate data using cut, paste, wc, and more
  • How I/O redirection (>, <, >>, 2>) works
  • How to use pipes and filters (|, tee, xargs, sort, uniq) to create workflows

Topics Covered

Subtopic Description
Viewing Tools (cat, less, head)View content in full, page-by-page, or by line count
Text Utilities (cut, paste, nl)Analyze, split, combine, and transform file content
I/O Redirection (>, >>, 2>, <)Save output to files, capture errors, or read input from files
Pipes & Filters (`, tee, xargs`)

Linux/Unix: Viewing Tools (cat, less, more, head, tail, tac)

cat – View Entire File

cat file.txt

Displays full content at once.


less / more – Paginated View

less file.txt
more file.txt

Scroll through large files one page at a time (q to quit).


head / tail – View Start or End

head -n 5 file.txt    # First 5 lines
tail -n 10 file.txt   # Last 10 lines

tac – Reverse Line Output

tac file.txt

Displays file content in reverse line order.


Linux/Unix: Text Utilities (nl, wc, cut, paste, split)

nl – Number Lines

nl file.txt

wc – Word Count

wc -l file.txt     # Line count
wc -w file.txt     # Word count
wc -c file.txt     # Byte count

cut – Extract Columns

cut -d ':' -f1 /etc/passwd

Extracts usernames from /etc/passwd.


paste – Merge Line-by-Line

paste file1.txt file2.txt

Horizontally joins lines from two files.


split – Divide File into Chunks

split -l 100 largefile.txt part_

Splits into 100-line chunks.


Linux/Unix: I/O Redirection (>, <, >>, 2>)

SymbolMeaningExample
>Redirect output (overwrite)echo Hello > file.txt
>>Append outputecho Again >> file.txt
2>Redirect stderrls nofile 2> error.log
<Use file as inputsort < data.txt

Combine Output and Error

command > out.txt 2>&1

Sends both stdout and stderr to the same file.


Linux/Unix: Pipes & Filters (|, tee, xargs, sort, uniq)

| – Pipe Output to Another Command

ls | grep txt

Lists only .txt files.


tee – Output to File and Screen

ls | tee list.txt

Saves and shows output simultaneously.


sort and uniq

sort names.txt | uniq

Sorts and removes duplicates.


xargs – Feed Arguments to Commands

ls *.txt | xargs rm

Deletes all .txt files (safe alternative to rm *.txt in scripts).


Summary – Recap & Next Steps

Mastering file viewing, I/O redirection, and filters gives you the ability to read, process, and transform data directly in the terminal. These tools are the heart of every shell script and sysadmin task.

Key Takeaways:

  • Use cat, less, head, tail, and tac to explore files
  • Use cut, nl, wc, and paste to manipulate content
  • Redirect input/output with >, >>, 2>, and <
  • Chain commands using |, and manipulate streams with xargs, tee, sort, and uniq

Practical Applications:

  • Build scripts that log and monitor command outputs
  • Quickly sort, filter, and deduplicate log files
  • Combine commands for advanced text processing pipelines

Frequently Asked Questions

How do I view a file in reverse order?
Use tac file.txt to display lines in reverse.


How do I get line and word count of a file?
Use:

wc -l file.txt     # lines
wc -w file.txt     # words

What does 2>&1 mean?
It redirects stderr (2) to the same location as stdout (1).


How can I combine grep, sort, and uniq?
Example:

cat logs.txt | grep error | sort | uniq -c

Counts unique error lines from a log file.


Share Now :
Share

πŸ“‘Linux/Unix: File Viewing, Redirection & Filters

Or Copy Link

CONTENTS
Scroll to Top