The title of this article probably should have been, “How To: Write Text To A File From The Terminal With The Redirection Operators “>” and “>>””, but that’s just too long. With that length would also come some ugly formatting. WordPress has its benefits, but customizing that sort of stuff on a per-post basis is not one of them.
There are many redirection operators in Bash. That link will take you to some dense information regarding these redirection operators. Also, that’s the formal name – they’re a ‘redirection operator’, both > and >>. It took me a minute to even recall that they had a formal name, so you should probably mark that in your notes just in case there’s a quiz!
The only two operators we’ll cover in this article. We’ll learn to write text to a file from the terminal – or, more accurately, we’ll learn to redirect text to a file from the terminal. It’ll be easy and is very much a beginner-level skill to have in Linux.
Entirely off-topic: But you can download a free book (PDF) called The Linux Command Line by William Shotts. It is in its 5th edition at the time of writing and is worth downloading and reading.
Write Text To A File:
Obviously, you’ll need an open terminal for this. If you don’t know how to open the terminal, you can do so with your keyboard. Press
1 | cd Documents |
Now, with your terminal open, you can try the following:
1 | echo "foo" >> temp.txt |
Now, you can verify that you’ve echoed foo to a file named ‘temp.txt’ with the following command:
1 | cat temp.txt |
As you can see, it wrote ‘foo’ to the file. You may not think that all that interesting, but it can be useful. If you go back to my article about “How To: Generate a List of Installed Applications in Linux”, you’ll see it used there. Like, if you wanted to create a list of installed applications and save it to a text file, your command would look something like this:
1 | dpkg -l >> installed_applications.txt |
It’s also useful for things like generating a list of your files and/or directories. You could just as easily run:
1 | ls -a >> files.txt |
Now, if you run any of these commands back-to-back, you’ll notice that the >> operator appends the data at the end of the file. So, if you ran the first command (echo “foo”) twice, your text document would say:
1 2 | foo foo |
If you want to overwrite the data, you are looking for the > operator. If you use just the > then it will overwrite the existing data, so use with care.
You can test this by using the ls command above with the same output file name as the command where you echo foo and you’ll see that just the last command’s output was written to the file. So, try the following, where your final results will be ‘foo’:
1 2 3 4 | ls -a > test.tmp cat test.tmp echo "foo" > test.tmp cat test.tmp |
You can actually use both > and >> to create a new file with zero bytes. It’s not like recommended or anything, it’s just something interesting that you can do with it. Just use the following:
1 | > new_file.txt |
Sure enough, you have a new file and now you know how to write text to a file from the terminal in Linux.
Closure:
And there you have it, another article. This article explained how to write text to a file. The redirection operators are handy tools to have in your toolbox. They’re useful tools when you want to work with large amounts of text. They’re useful tools when you want to keep track of a command’s output over time. You’ll find a use for it, I have faith in you!
Thanks for reading! If you want to help, or if the site has helped you, you can donate, register to help, write an article, or buy inexpensive hosting to start your own site. If you scroll down, you can sign up for the newsletter, vote for the article, and comment.