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