Linux/Unix Tutorial
Estimated reading: 4 minutes 39 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 :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

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

Or Copy Link

CONTENTS
Scroll to Top