I think CoW works for this.
echo 'stackoverflow' > foo
cp --reflink foo bar
So what about this one?
echo 'stackoverflow' > foo
echo 'stackoverflow' > bar
In this case, do they use the disk space of two files?
In my use case, there are not many copied files on my disk. However, there are many files of the same content generated by programs.
I hope that CoW is effective for such files, but if not, isn't compression more effective? This is why I asked this question.
In this case, do they use the disk space of two files?
Yes: the files are created independently and use individual disk space.
If you already have many files with duplicate contents and want to take advantage of BTRFS' CoW features, you are looking for (offline) deduplication using tools like duperemove
.