5 ms·
cat > file vs. touch file cat file | cmd vs. cmd < file cmd1 | cmd2 | cat vs. { cmd1 | cmd2 || true; } vs. cmd1 | cmd2; true So even some of the thing list
by badboy 15y ago
cat > file vs. touch file
cat file | cmd vs. cmd < file
cmd1 | cmd2 | cat vs. { cmd1 | cmd2 || true; } vs. cmd1 | cmd2; true
So even some of the thing listed are still not the best use case for cat
- cristoperb 15y ago| cat > file vs. touch file With 'cat > file' you can actually type in the file's contents (then press ^d to finish). 'touch file' merely creates an empty file. (Not that I think 'cat > file' is much faster than 'vi file')
- badboy 15y agoOk, I should read more carefully. I thought the article uses cat > file followed by ^D to create empty files. Of course when having some text cat > file is just fine.
- icebraining 15y agoNo, you can remove 'cat' on most shells: > file
- LukeShu 15y agoWhat shell? I'm running bash 4.2.20(2)-release, and "> file" is behaving like "touch file".
- icebraining 15y agoYou're right, I was mistaken. It's just in Z shell.
- salvadors 15y agoYou mean there are other shells?
- dspillett 15y ago>* cat file | cmd vs. cmd < file* I tend to prefer the cat version as the flow of the command follows the left-to-right direction in which I generally read, and it makes things easier for less experienced users to read and understand for the same reason. It does make things less efficient of course, as there is at least one extra memory-copy of everything and extra process context switching, but in most cases your drives or network are the bottleneck not CPU speed or memory bandwidth. Also, when working on the command line you can drop in useful tools like pv in place of cat.
- veyron 15y agoIf you want left to right you can do <file cmd