3
ls -l *.txt | wc -l | tee count.txt
ls -l *.txt | wc -l > tee count.txt

I am learning basic Unix commands. I thought those two commands mentioned would do the same thing, but they do not. Only the first command writes to count.txt. How come the output redirection used in the second command is not doing anything?

5
ls -l *.txt | wc -l | tee count.txt

In first example:

The ls command lists all files in the current directory that have the filename extension .txt, one file per line; this output is piped to wc, which counts the lines and outputs the number; this output is piped to tee, which writes the output to the terminal, and writes the same information to the file count.txt. If count.txt already exists, it is overwritten.

In your second example:

ls -l *.txt | wc -l > tee count.txt

In this example tee will be treated as file name instead of a command and output of wc -l will be redirected to the newly crated file tee and will contain the output of wc -l and string count.txt.

If you want the same behaviour as in the first example then right way of doing this would be:

 ls -l *.txt | wc -l > count.txt

> itself is sufficient to redirect the output to count.txt file

| improve this answer | |
  • In the second example, are you saying it's ambiguous because the output from wc -l is not piped? – Mint.K Jan 20 '17 at 8:41
  • 3
    @Mint.K your second example is a comepletely diferent command. It means "save the output of wc -l in a file called tee" and doesn't involve the tee command in any way. – terdon Jan 20 '17 at 8:51
  • You're right. I just noticed my second command made a file called tee with the output of wc -l in it. – Mint.K Jan 20 '17 at 8:54

Not the answer you're looking for? Browse other questions tagged or ask your own question.