| ) and redirections (
>>) are two of the most powerful features in bash and on the command line. They allow us to chain commands together, passing
data and outputs from on to the other and even into files.
We've already covered the
ls command, which lists the content of our current working directory. Now instead of printing the output of
ls to the CLI,
we can use the pipe operator to put it right into a file. If the file doesn't exist yet, it will be created in the process.
Now all the output from
ls is actually put inside
directory-listing.txt and not displayed explicitly in the CLI. If we want to append to a file instead of overwriting its content (if it exists already),
we can use the appending redirect syntax
>> like below
The pipe operator (
| ) allows us to use the output of one command as the input for another. A great example of that is combining any output
sed command, which allows many simple and complex text-based operations, such as replacing strings that match a regular expression.
In the following example, we're using the output of
ls -l and pipe it into
sed, which replaces all occurrences of the letters
a,e,i,o,u with the letter
It is worth noting that we can chain as many commands as we want or need to, so we could take the examples above and combine them into
one big chain that lists our current directory's files, replaces all letters
X and immediately writes them to a file called
The more shell commands we know, the more possibilities those operators open up to us.
Here's a great resource, covering those opertators in more depth: https://tldp.org/LDP/abs/html/io-redirection.html