Here's how to reproduce it:
echo 'the original file' > orig
ln -s orig symb # now symb is symlinked to orig
cat symb > orig # this makes orig an EMPTY FILE, but why?
orig becomes an empty file after the third command, but why?
Symlinks are evaluated as you attempt to open a file. Under "normal" circumstances the result of opening a symbolic link is to open the file it references. So:
ln -s original_file my_symlink
cat my_symlink > original_file
Is very similar to:
cat original_file > original_file
Why does it destroy the content?
In any shell command > some_file will first open and truncate (zero) some_file and then execute command, writing the result to some_file.
So if you cat x > x then the shell will open and truncate x, then cat x will open and read the truncated file writing all of it's zero bytes.
cat orig >orig. Are you thinking you're doing something different? – Kusalananda Feb 19 '21 at 13:57catcommand performs the redirection. If this is what you think, you are wrong. It is the shell. Before executingcat, the shell processes> orig. This means that it creates an emptyorigif it doesn't exist, or it resetsorigto a size of 0 if it exists. After that,catis called, its stdout connected toorig. – berndbausch Feb 19 '21 at 14:24