Quantcast
Channel: Is it a UUOC (useless use of cat) to redirect one file to another? - Unix & Linux Stack Exchange
Viewing all articles
Browse latest Browse all 8

Is it a UUOC (useless use of cat) to redirect one file to another?

$
0
0

If I want to make the contents of file2 match the contents of file1, I could obviously just run cp file1 file2.

However, if I want to preserve everything about file2 except the contents—owner, permissions, extended attributes, ACLs, hard links, etc., etc., then I wouldn't want to run cp.* In that case I just want to plop the contents of file1 into file2.

It seems like the following would do it:

< file1 > file2

But it doesn't work. file2 is truncated to nothing and not written to. However,

cat < file1 > file2

does work.

It surprised me that the first version doesn't work.

Is the second version a UUOC? Is there a way to do this without invoking a command, merely by using redirections?

Note: I'm aware that UUOC is more of a pedantic point than a true anti-pattern.

*As tniles09 discovered, cp will in fact work in this case.


Viewing all articles
Browse latest Browse all 8

Latest Images

Trending Articles





Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>
<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596344.js" async> </script>