Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

What does following cat commnad in Unix mean? I am new to Unix

cat test.txt | java myfilegrammar.pcfg > test1.txt 2> test2.txt
share|improve this question

migrated from Mar 23 '10 at 4:14

This question came from our site for professional and enthusiast programmers.

It just makes test.txt's contents the standard-input to the java program -- but that has nothing to do with programming. – Alex Martelli Mar 23 '10 at 3:59
up vote 3 down vote accepted

It pipes the contents of test.txt to the input of java grammar.pcfg, and then redirects that java command's output stream to test1.txt and its error stream to test2.txt.

You might find it a little easier to understand if it is parenthesized, like this (note that this is for clarity and is not valid syntax):

(cat test.txt | java grammar.pcfg) > test1.txt 2> test2.txt

This makes it more clear that the first text file is an input file, while the last two are output files.

(However, the java bit doesn't really make sense to me -- java is called on .class files, not .java files.)

share|improve this answer
corrected the command Thanks. – Harshit Mar 23 '10 at 4:20

Randal L. Schwartz would award your code a Useless use of cat award. For more information of how one earns this award, and how it can be avoided, see I'll just quote Randal L. Schwartz's form letter for posterity.

And of course, if you've been following along for a week or two, you know that this (BING!) is a Useless Use of Cat!

Rememeber, nearly all cases where you have:

    cat file | some_command and its args ...

you can rewrite it as:

    <file some_command and its args ...

and in some cases, such as this one, you can move the filename to the arglist as in:

    some_command and its args ... file

Just another Useless Use of Usenet,

(No, I'm no trying to be insulting; merely entertaining. If you follow the links, your question will be asnwered.)

share|improve this answer
  1. Dump the contents of test.txt into the standard input for a process launched using java as the executable with the arguments " grammar.pcfg"
  2. Map the standard output of the java execution into test1.txt
  3. Map the error stream into test2.txt
share|improve this answer

Your java program reads the input from stdin and the input that you want to supply are in the file test.txt. The way to supply these inputs is to put the contents of the text.txt file to stdout and pipe it to the stdin of the java program.

The same can be done without using cat as:

java grammar.pcfg > test1.txt 2> test2.txt < test.txt 
share|improve this answer

Each process in Linux starts with three file descriptors by default:

  • 0 -> Stdandard Input
  • 1 -> Standard Output
  • 2 -> Standard Error

If you see a redirection, i.e. fd_number > some_file.txt , it indicates that whatever would normally be printed to the file that holds file descriptor fd_number will now be redirected to whatever comes after it.

The command that you gave could also be written as:

cat test.txt | java grammar.pcfg 1> test1.txt 2> test2.txt


java grammar.pcfg 0< test.txt 1> test1.txt 2> test2.txt

... depending on how handles standard input.

share|improve this answer

send the contents of test.txt to " grammar.pcfg" via standard input. send the standard output of grammar.pcfg to test1.txt send the standard error output of grammar.pcfg to test2.txt

share|improve this answer

it is the same as

java grammar.pcfg > test1.txt 2> test2.txt < test.txt

but with additional process (cat) that reads the data from test.txt and prints it to standard output (which is being redirected by the pipe | to the standard input of java)

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .