π 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, andtail
- 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>)
| Symbol | Meaning | Example | 
|---|---|---|
| > | Redirect output (overwrite) | echo Hello > file.txt | 
| >> | Append output | echo Again >> file.txt | 
| 2> | Redirect stderr | ls nofile 2> error.log | 
| < | Use file as input | sort < 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, andtacto explore files
- Use cut,nl,wc, andpasteto manipulate content
- Redirect input/output with >,>>,2>, and<
- Chain commands using |, and manipulate streams withxargs,tee,sort, anduniq
βοΈ 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 :
