That's a super insightful question! Nearly all standard Linux command-line utilities are designed to work seamlessly with pipes, making them incredibly powerful when chained together. The general rule is: if a command writes to standard output (stdout) or reads from standard input (stdin), it can be used with pipes.
Here's a list of common commands often used with pipes, along with their general function:
-
Filtering Commands: These narrow down the data.
grep: Filters lines matching a pattern. (e.g.,cat access.log | grep "GET /admin")awk: A powerful text processing tool, often used for more complex filtering, pattern matching, and data reformatting. (e.g.,cat access.log | awk '{print $7}')sed: A stream editor, used for substituting text or deleting lines. (e.g.,cat report.txt | sed 's/error/critical/')uniq: Removes or reports duplicate lines. (e.g.,cat list.txt | sort | uniq)head: Displays the beginning of input. (As you've seen, often used at the end of a pipe to show a sample).tail: Displays the end of input. (e.g.,tail -f /var/log/syslog | grep errorto watch for errors in real-time)
-
Transformation/Formatting Commands: These change the structure or content of the data.
cut: Extracts columns or fields from lines (as you just used).sort: Sorts lines of text. (e.g.,ls -l | sort -k5nto sort files by size)tr: Translates or deletes characters. (e.g.,cat file.txt | tr 'a-z' 'A-Z'to convert to uppercase)column: Formats output into columns. (e.g.,ls -l | column -t)wc: Counts lines, words, and characters. (e.g.,cat document.txt | wc -lto count lines)
-
Input/Output Commands:
cat: Concatenates files and prints to standard output (often used at the beginning of a pipe to feed file content).less,more: Paginates output, allowing you to view it screen by screen. (e.g.,long_command_output | less)tee: Reads from standard input and writes to both standard output and one or more files. (e.g.,command | tee logfile.txt)
-
Statistical/Reporting Commands:
xargs: Builds and executes command lines from standard input (very powerful for applying commands to lists of items). (e.g.,find . -name "*.txt" | xargs rmto delete all .txt files)
Example Scenario:
Imagine you want to find all unique error messages in a log file, sort them alphabetically, and then only see the first 5:
grep "ERROR" access.log | sort | uniq | head -n 5
This is a very common and powerful pattern: source_command | filter_command | transform_command | output_command.
Do any of these particular commands spark more questions, or would you like to see another example of piping?