Published at
Updated at
Reading time
1min
This post is part of my Today I learned series in which I share all my web development learnings.

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.

Cat stdin magic

If you enjoyed this article...

Join 5.5k readers and learn something new every week with Web Weekly.

Web Weekly โ€” Your friendly Web Dev newsletter
Reply to this post and share your thoughts via good old email.
Stefan standing in the park in front of a green background

About Stefan Judis

Frontend nerd with over ten years of experience, freelance dev, "Today I Learned" blogger, conference speaker, and Open Source maintainer.

Related Topics

Related Articles