Fundamentals 9 min read

Master Linux Redirection, Pipes, and Text‑Processing Commands

This guide explains how to redirect standard streams, chain commands with pipes, and use essential text‑processing utilities such as tee, xargs, grep, cut, awk, sed, sort, wc, uniq, and tr on the Linux command line.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Master Linux Redirection, Pipes, and Text‑Processing Commands

Redirection

Standard output (stdout) can be written to a file with command > file, overwriting existing content, while command >> file appends. Standard error (stderr) uses 2> file, and both streams can be combined with command > file 2>&1 or command >& file.

Pipes

The pipe operator commandA | commandB | commandC passes the stdout of the left command as stdin to the right command. Only commands that can read from stdin (e.g., less, more, head, tail) are suitable; commands like ls, cp, mv cannot be used directly.

tee Command

tee [OPTION]... [FILE]...

reads stdin, writes it to stdout, and optionally saves it to one or more files. Combined with a pipe, it allows simultaneous display and logging, e.g., ll /home | tee list_home.out or find /home -name .bashrc 2>&1 | tee find.out.

xargs Command

xargs [options] [command [initial-arguments]]

reads items from stdin, splits them by whitespace or newline, and builds command lines. Typical usage includes passing find results to other commands, such as find /usr/sbin -perm /7000 | xargs ls -lh or find /home -name "*.go" | xargs du -cb.

Text‑Processing Utilities

grep

Searches files for lines matching a pattern: grep pattern file. It can be combined with pipes, e.g., ps -ef | grep postgres.

cut

Extracts sections from each line of input. Use -d to set a delimiter and -f to select fields, e.g., echo $PATH | cut -d ':' -f 1, or export | cut -c 12- to trim the first 12 characters.

awk

Powerful pattern‑action language. Basic usage: awk '{print $1,$4}' file. The field separator can be changed with -F, e.g., awk -F, '{print $1,$4}' file.

sed

Stream editor for applying scripted edits to text. Common commands include a (append), c (change), d (delete), i (insert), p (print), and s (substitute). Example: sed -e '4a\naaaa' file adds a line after line 4; sed '2,5d' file deletes lines 2‑5.

sort

Sorts lines of text alphabetically or numerically: sort file.

wc

Counts lines, words, and bytes. Example: ps -ef | grep postgres | wc -l returns the number of matching processes.

uniq

Filters adjacent duplicate lines, typically after sort: uniq file.

tr

Translates or deletes characters. Example: cat file | tr a-z A-Z converts lowercase to uppercase.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

ShellBashcommand-lineRedirectiontext-processing
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.