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

Setting Up Coding Environments on Linux for Educational Use

With so many strong attributes, such as robustness and flexibility, Linux stands as a powerful…

18 hours ago

View Detailed Hardware Information

There are many tools for showing your hardware information and today we'll get to view…

3 days ago

How To: Install Wine In Lubuntu

Today's article isn't going to be complicated or long as we are going to cover…

5 days ago

Update Python Packages (PIP)

We've had a run of Python packages recently and you can tell that I'm a…

7 days ago

Save A Command’s Output To A File (While Showing It In The Terminal)

The title is the best I can come up with to describe this exercise as…

1 week ago

Demystifying journalctl: A Comprehensive Guide to Linux System Logging

It was suggested that I write an article about journalctl, which seemed like a large…

2 weeks ago