Command Line

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

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

cd Documents

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

echo "foo" >> temp.txt

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

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:

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:

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:

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

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:

> 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.

KGIII

Retired mathematician, residing in the mountains of Maine. I may be old and wise, but I am not infallible. Please point out any errors. And, as always, thanks again for reading.

Recent Posts

Enable/Disable Your Network Interface

Today we'll cover one way to enable or disable your network interface in the Linux…

7 months ago

Check Your NIC Speed In The Terminal

Today's exercise is a nice and simple exercise where we check your NIC speed in…

7 months ago

Easily Monitor Your Wireless Connection

Have you ever wanted to easily monitor your wireless connection? Well, now you can learn…

7 months ago

Count The Files In A Directory

I think I've covered this before with the ls command but this time we'll count…

7 months ago

Get System Information With The ‘uname’ Command In Linux

Today we'll be learning about a basic Linux command that's known as 'uname' and it…

7 months ago

hardinfo Has Been Rebooted As hardinfo2

If you've used hardinfo in the past, it may interest you to know that hardinfo…

7 months ago