output redirection in UNIX output redirection in UNIX unix unix

output redirection in UNIX


  1. Because ls reads all the names and sorts them before printing anything, and because the output file is created before the command is executed, at the time when ls checks the size of temp, it is empty, so it shows up in the list as an empty file.

  2. When wc reads the file, it is empty, so it reports 0 characters in 0 words on 0 lines, and writes this information into the file after it has finished reading the empty file.


When you pipe the output to a file, that file is created, the command is run (so ls lists it as an empty file, and wc counts the characters in the empty file), then the output is added to the file.

… in that order.


You cannot write and read from the same file at the same time.

So:

wc file > file # NOT WORKING# but this works:wc file > file.stats mv file.stats file # if you want that