The power of cat and stdin (#tilPost)

Today I came across this tweet by Safia Abdalla. It showed that you can use the cat command to quickly add content to a file (cat – >> foo.txt). The way it works is that you can directly write to stdin until you hit ^D or ^C.


This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis

Today I came across this tweet by Safia Abdalla. It showed that you can use the cat command to quickly add content to a file (cat - >> foo.txt). The way it works is that you can directly write to stdin until you hit ^D or ^C.

So I opened the manual for the cat command and wanted to see what else is in there. One sentence suprised me though.

If file is a single dash (`-') or absent, cat reads from the standard input.

You can make the snippet even shorter! ?

$ cat - >> foo.txt
# is the same as
$ cat >> bar.txt

That's cool!

I also discovered that you can use cat and stdin right in between files and concat them (you have to use ^D^C doesn't work then). ?

$ cat 1.txt - 3.txt > all.txt

I'm not sure if I'll ever need this functionality but it's always good to know some shell tricks. Safia shares a lot more CLI tricks so you should definitely check these out.


Reply to Stefan


This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis


Print Share Comment Cite Upload Translate
APA
Stefan Judis | Sciencx (2023-02-09T02:37:48+00:00) » The power of cat and stdin (#tilPost). Retrieved from https://www.scien.cx/2017/07/24/the-power-of-cat-and-stdin-tilpost/.
MLA
" » The power of cat and stdin (#tilPost)." Stefan Judis | Sciencx - Monday July 24, 2017, https://www.scien.cx/2017/07/24/the-power-of-cat-and-stdin-tilpost/
HARVARD
Stefan Judis | Sciencx Monday July 24, 2017 » The power of cat and stdin (#tilPost)., viewed 2023-02-09T02:37:48+00:00,<https://www.scien.cx/2017/07/24/the-power-of-cat-and-stdin-tilpost/>
VANCOUVER
Stefan Judis | Sciencx - » The power of cat and stdin (#tilPost). [Internet]. [Accessed 2023-02-09T02:37:48+00:00]. Available from: https://www.scien.cx/2017/07/24/the-power-of-cat-and-stdin-tilpost/
CHICAGO
" » The power of cat and stdin (#tilPost)." Stefan Judis | Sciencx - Accessed 2023-02-09T02:37:48+00:00. https://www.scien.cx/2017/07/24/the-power-of-cat-and-stdin-tilpost/
IEEE
" » The power of cat and stdin (#tilPost)." Stefan Judis | Sciencx [Online]. Available: https://www.scien.cx/2017/07/24/the-power-of-cat-and-stdin-tilpost/. [Accessed: 2023-02-09T02:37:48+00:00]
rf:citation
» The power of cat and stdin (#tilPost) | Stefan Judis | Sciencx | https://www.scien.cx/2017/07/24/the-power-of-cat-and-stdin-tilpost/ | 2023-02-09T02:37:48+00:00
https://github.com/addpipe/simple-recorderjs-demo