Georgez Georgez - 4 months ago 8x
Bash Question

Dont understand (Pipes) usage unix

I am studying for 'operational Systems' exams at my uni and i am having hard time understanding pipes usage (|).
Here is an example i found on internet:

ps -ax | grep Finder

Use the ps command to get a list of processes running on the system, and pass the list to grep to search for lines containing "Finder". (Usually, it'll find two: the Finder, and the processes executing grep Finder.)

What if i first write
ps -ax
and at next line
grep finder
? wont it have the same result? why i have to pipe them together?

ps: Bigginer at unix shell commands and how it works.


It's all about redirecting input and output.

If you were to type ps -ax > processes, you would create the file processes with a list of all the processes. This is redirecting output. All the data shown to the screen is instead written to a file.

If you were to type grep Finder < processes, you would search the file processes for the word Finder. This is redirecting input.

Pipe does both. It redirects the output of the command on the left side and redirects the input of the command on the right side.

$ ps -ax | grep Finder

is like

$ ps -ax > temp
$ grep Finder < temp

except all on one line with no temp file to delete.