linuxunixterminalcat

Redirecting the cat ouput of file to the same file


In a particular directory, I made a file named "fileName" and add contents to it. When I typed cat fileName, it's content are printed on the terminal. Now I used the following command:

cat fileName>fileName

No error was shown. Now when I try to see contents of file using,

cat fileName

nothing was shown in the terminal and file is empty (when I checked it). What is the reason for this?


Solution

  • > i.e. redirection to the same file will create/truncate the file before cat command is invoked as it has a higher precedence. You could avoid the same by using intermediate file and then from intermediate to actual file or you could use tee like:

    cat fileName | tee fileName