How To: Write Text To A File From The Terminal with “>” and “>>”

Rate this post

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!

Related Post:

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 CTRL + ALT + T and your default terminal should open. Once you have an open terminal, you can change to your Documents directory (avoiding making a mess in you ~/ directory) with:

Shell

Now, with your terminal open, you can try the following:

Shell

Now, you can verify that you’ve echoed foo to a file named ‘temp.txt’ with the following command:

Shell

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:

Shell

It’s also useful for things like generating a list of your files and/or directories. You could just as easily run:

Shell

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:

Shell

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’:

Shell

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:

Shell

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!

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x