Linux pipe command examples (command mashups)

One of my favorite things about Unix, Linux, and Mac OS X systems is that you can create your own commands by merging other commands. There isn't any formal name for these command combinations, other than to say that you're "piping" commands together, so I recently started referring to these as "command mashups".

Here's a simple pipeline command I use all the time, creating a long list of files and piping the output into the Linux more command:

ls -al | more

Here's a pipeline command mashup I use to get a list of all sub-directories in the current directory:

ls -al | grep '^d'

That pipe command works because the grep command only shows the file records that begin with the letter d.

Here's how I list all the processes running on a Unix system that include the string "fred":

ps auxwww | grep fred | more

That helps me find processes that might be owned by a user named "fred".

Here's an example of how I list all the files in my Apache log directory for the month of August in the current year:

ls -al | grep Aug | grep -v '200[456]' | more

I use that grep command to filter out files from the years 2004, 2005, and 2006, as all the log files for several years are currently in one directory.

This ps and wc command pipeline gives you an idea of how many processes are currently running on your system:

ps aux | wc -l

The number returned is actually high by one because the first line of output from the ps command is a header, and not a process.

This command that combines locate and grep helps me find Java files on my computer:

locate "*.java" | grep java

Of course there are many more mashups you can create, just about as many as your imagination allows. I just wanted to show these examples so you'd see how they work.

The basic concept is that you run one command, and then pipe the output from that command into another, and then another, etc. This is formally referred to as piping the "standard output" ("standard out", "STDOUT") from one command to the "standard input" ("standard in", "STDIN") of another command.

Add new comment

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.