standard output(>) and Pipe(|)

Posted on 2005-04-17
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

    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

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

    Assisted Solution

    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
    LVL 16

    Expert Comment

    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.
    LVL 7

    Accepted Solution

    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

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Currently, there is not an RPM package available under the RHEL/Fedora/CentOS distributions that gives you a quick and easy way to allow PHP to interface with Oracle. As a result, I have included a set of instructions on how to do this with minimal …
    I am a long time windows user and for me it is normal to have spaces in directory and file names. Changing to Linux I found myself frustrated when I moved my windows data over to my new Linux computer. The problem occurs when at the command line.…
    Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    779 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

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now