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 :
