Chaining commands with pipes and redirecting outputs

Pipes ( | ) 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.

command line
ls > directory-listing.txt

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

command line
// echo is simply printing a string to the cli
echo "appending this..." >> directory-listing.txt

Redirecting a command's output with pipes

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 with the 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 X.

command line
// Usually, without sed's replace we'd get:
// -rw-r--r--@ 1 ben staff 7 Nov 11 20:13 test.txt
ls -l | sed -e "s/[aeiou]/X/g"
-rw-r--r--@ 1 bXn stXff 7 NXv 11 20:13 tXst.txt

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 a,e,i,o,u with X and immediately writes them to a file called output.txt.

command line
ls -l | sed -e "s/[aeiou]/X/g" >> output.txt

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: