[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


standard output(>) and Pipe(|)

Posted on 2005-04-17
Medium Priority
Last Modified: 2010-04-20
Hi ,
what are the differences between  "|" and ">" ?
Can you bring A Example For differing these two Case?

Best Regards
Question by:ronnie_bobo
LVL 14

Assisted Solution

trigger-happy earned 80 total points
ID: 13802614
Actualy that's called a redirector (>), basically what this does is to redirect output to somewhere else like a file
ps -A > procs.txt
The pipe operator is used to pass output to another process
ps -A | grep apache
hope that clears up the differences

LVL 10

Expert Comment

ID: 13802623
$ echo "foo" > file
$ cat file
$ cat file | sed 's/foo/bar/' > file1
$ cat file1
$ cat file*
LVL 16

Assisted Solution

manav_mathur earned 100 total points
ID: 13810734
pipe connects stdout of the process on the left to the stdin of the process on the right.
Important thing to note here is 'process'. As files are not processes, they hae no stdouts/stdins.

> redirects the filedescriptor on its left (1 by default) to the file specified on the right
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

LVL 16

Expert Comment

ID: 13810748
The process on the left must be capable of generating something on its STDOUT. The process on the right must be capable of feeding on its STDIN.

Accepted Solution

macker- earned 120 total points
ID: 13830166
The redirectors > and < are used for redirecting input from/to a file or other filesystem-based object.  I.e. to write the output of a command to a file, or to read in the contents of a file.

Pipes are used to redirect the output of one process (from stdout) to another process without the need to place it in a file first.  The output of an infinite number of processes can be "chained" using pipes, either for text processing or structured command sequences.

For pipes, something can be done such as:

cat /etc/passwd (read file /etc/passwd) | awk -F : '{print $1}' (print the first field) | sort (sort the list alphabetically) | uniq -c (remove any duplicate entries, showing how many times each entry occurred)

For redirects, you may wish to send the stdout or stderr output of a command to a file, for later review, e.g.: date > current-time

Redirects can be used in three common ways:

date > current-time
Write the output of the "date" command to the file "current-time", overwriting it if it currently exists

date >> current-time
Append the output of the "date" command to the file "current-time", creating it if it doesn't already exist

date > current-time 2> /dev/null
Same as first example, but if there's any errors (or any output written to stderr), write it to /dev/null, where it will disappear

/dev/null is a special character device.  Any data sent to it will be discarded.  Reading from it will produce a "null" character.  Other interesting character devices are /dev/zero (zero's), /dev/random (random characters), and /dev/urandom (greater level of randomness, but slower).

You can also use redirects to read in from a file, e.g. "more < /etc/passwd" would read the file /etc/passwd and pass it to stdin (standard input) of the more command, as if you had typed the text yourself, and is functionally equivalent to "cat /etc/passwd | more" (and "more /etc/passwd")

Assisted Solution

bzImage8 earned 100 total points
ID: 13846291
redirection (>, >>) only works when the program creates a output to the stdout or stderr (standard output or standard error).

Pipes only works when the program to the right of the pipe obtains its input from the standard input, this kind of programs are called "filters".

For example:

Cat is a filter

$ echo "hello" | cat | cat | cat | cat

But echo is not a filter

$ echo "hello" | echo | echo | echo

Some common filters are:

cat, cut, sort, tr, tee, etc.

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
Fine Tune your automatic Updates for Ubuntu / Debian
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
Suggested Courses
Course of the Month19 days, 18 hours left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question