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:

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

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

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:

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

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:

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

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:

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.

Meta: I’m Now An Official Lubuntu (And Ubuntu) Member!

It goes without saying that I’m a pretty big Lubuntu fan. The reason it goes without saying is because (as anyone that knows me knows) I have said it plenty of times already! This ‘award’, becoming an official Lubuntu Member, is recognition for past activity in the Linux (specifically in the Lubuntu/Ubuntu sphere) community.

My application was voted on and approved on the 14th of November, so just a few days before you saw this. I actually missed the email notification (just an automated message informing me that I’d been added to a group) and didn’t notice until the congratulations started pouring in. I’ve since received guidance from my mentor, thankfully.

If you don’t know what it means (and what responsibilities you have) to be an official Lubuntu Member, you can learn more about the Membership by clicking this link. There’s more to it and I’ve not yet gone through all the benefits, but I’m pretty happy to have been voted into a rather exclusive club.

Darned right! Not a whole lot of people on the planet can say they’ve been official Ubuntu Members! I guess there are ‘more than 500’ of us currently, which is still a tiny drop in the ocean that is Linux users and the general population at large!

So, yes… Yes, it does make me happy to be a member. The recognition is nice and it’s comfortable to say ‘my peers’ – even though they all pretty much know so much more than I do.

My Lubuntu History:

You should probably start by reading my article here:

What it’s Like To Beta-test Linux, Specifically Lubuntu

That’ll give you most of the information you might need.

Anyhow, Lubuntu has been around since its official recognition in 2011. I’ve been using it nearly as long, as I was really happy to have an Ubuntu official-flavor with LXDE – my preferred desktop at the time. I dare say that it’s still kinda my official favorite DE, but I really have grown to like LXQt. It grows on you in time and is maturing nicely.

About 14 months ago… You know, it’ll take a minute, but let’s get some numbers for posterity! 

Alright, it began officially in October of 2020, when I said the following:

I have some free time coming up. I can toss some hours at this, but not for this release. Do you want testing on bare metal, or is testing in a VM adequate? Is the #Lubuntu IRC the place to go?

Which is the official start of my testing – so to speak. I jumped in just a little while later that month, after 20.10 was released. That means 21.04 was my first full-cycle participation. We’re now testing 22.04 and watching the changes has been informative and interesting.

One of my continued goals has been to learn more while helping. And, man… Did I learn a lot. I’m still learning a lot, and I now have a much better understanding of how Linux works behind the scenes. My troubleshooting abilities have increased because of it. I highly encourage others to get involved. Jump in at the deep end. The immersion helps!

My Lubuntu Future:

After the first cycle, I was actually able to (and was heavily encouraged to do so) apply for official membership. I decided to not apply at that time and to give myself a additional cycle before applying. It seemed prudent to make sure that I was really going to keep helping. 

Sure, the membership is about past contributions but, to me, it implies a level of commitment to future contributions. I plan on keeping on doing what I’ve been doing for the duration. I plan on continuing my education and stepping up to help with the tasks I am able to complete.

Man… It does feel nice to say ‘my peers’, but so many of them know so much more about Linux than I do. I am not even a programmer, at least not a very good one – and time doesn’t seem to be improving that ’cause I don’t have time to learn more. So, I definitely have a bit of that Impostor Syndrome going on.

Just reading the #lubuntu_dev chat has been super informative. Fortunately, I can jump in at any time and ask questions. They’ll help me understand, and point me towards additional educational resources. Everyone I could hope for stood up to help me get my feet on the ground and become a better tester.

It probably doesn’t need saying, but the people in and around the Lubuntu project are pretty awesome. Without them, I’d not be here – of course. I’ve spent a goodly number of years in academia, and it’s comforting to be able to surround myself with the smart people that make up the Lubuntu project.

My contributions elsewhere probably won’t change. I’ve been able to, and fortunate enough to, manage my time – and I’ve been able to set aside blocks of time for different tasks. So, I suspect this means I’m just going to keep doing what I’m doing for the foreseeable future.

This also bodes well for the site. If I’m doing what I have been doing, that includes keeping this site active, interesting, and regularly updated with new content. I might as well… If I still have stuff to write about, I might just as well keep writing.

Still, this isn’t set in stone. This site eats a ton of my time. I’m still only planning on a full year – but it seems likely that I’ll just keep pounding the keyboard while hoping an article pops out the other side. It has been a pretty good run so far.

Closure:

For the record: I sure as heck didn’t get here by myself. In fact, if it wasn’t for the many, many positive messages and prompting me to apply, I probably still wouldn’t have applied. I don’t think I’d have felt qualified, if it hadn’t been for the urging. 

The two members I’d like to thank the most for that aspect are Leok and guiverc. Of the two, I consider guiverc to be my mentor. I’m pretty sure there is an official title of “Mentor” in and among the official members. I don’t think guiverc really holds that title, but they have put up with my many questions and given me great guidance over the past year. 

So, I’d like to thank especially both Leok and guiverc, as well as all the other members who have encouraged me, educated me, or just plain tolerated me when I asked questions. I told ’em back at the start that I’d do my best to make sure their time spent helping me learn would not end up as wasted time, and I’d like to think I’ve demonstrated that and made true on my claim.

To the rest of the well-wishing folks, thanks! You too have likely given me reason to keep going with this, in one way or another. Just reading the site is helping to motivate me to continue learning and publishing. Also, please feel free to leave any congratulatory comments here on this site, avoiding leaving them across the various sites. 

As always, 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.

Automatically Logout Of Your Shell

For security reasons, you’ll possibly automatically logout of your user sessions. If you didn’t know, you can actually do that with your shell, in the terminal. There’s already a variable (TMOUT) just for this reason, should you want to add it as a layer of security.

Basically, for today, we’re going to set it up so that it automatically logs inactive users out of their shell session. It doesn’t log you out of your complete user session, it just logs you out of your shell – after a set period of activity. It even closes the open terminal windows when it does so.

So, depending on the interval you use, you can set it up to log you out of your shell instances after just a few minutes of inactivity. If you have nosy neighbors, like people physically near your computer, it can be a nice way to make sure things are all locked before you head off to the bathroom.

It’s useful for that sort of stuff. It’s just an added layer of security. I think that it is a pretty handy feature. I’ll explain how to enable it on a user-by-user basis and how to make it system-wide, giving you a choice. It’s actually pretty easy, so read on!

Automatically Logout Of Your Shell:

Like most good things in the Linux world, you’ll need an open terminal to take advantage of this article. If you don’t know how to open the terminal, you can do so with your keyboard – just press CTRL + ALT + T and your default terminal should open.

Both of these ways are pretty simple, in each case you add some text (using nano) to a profile file. The text in either case is the same. If you want to do it for just one user, the user you’re currently using, then run the following:

Add the following:

So, if you wanted it to be 10 minutes of inactivity before being logged out, you’d use TMOUT=600, because 600 seconds is 10 minutes. As you’re using nano, you can press CTRL + X, then Y, and then ENTER to save the file.

You’ll then force the profile to load, the command taking effect immediately, with this:

If you want to do it with the full system, the online guides will tell you to edit /etc/profile and that it’ll work if you do. My experiences are different and this is tested across multiple systems. You’ll be editing /etc/bash.bashrc, just like you did above but with sudo. (Using /etc/profile has not worked for me.)

Again, you add ‘TMOUT=600″ or however many seconds you want to wait. Personally? I scrolled to the bottom of the file, made a new line, and added the text that way. You could be all professional and add a comment indicating when and why you were there. I did nothing of the sort.

Unlike the first command, you’ll not be able to reload the second method (system-wide configuration) with ‘source ~/…’. As near as I can tell, you’ll have to restart the system for the changes to take place. If someone has a way to load it without rebooting, I’ll update the article. Please leave a comment if you do know of a way!

Closure:

There you have it, another article! This one tells you how to automatically logout from your shell. I’m not sure if it works for all shells, so feel free to test and see what sort of results you get. I’m pretty sure the 2nd option could be reloaded without rebooting, but I can’t think of which command. Which service would need restarting? I dunno?

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.

Find Your Username In Linux

Today’s article is going to be fairly brief and easy, as it just covers how to find your username in Linux. For this exercise, we’ll be using a terminal and some basic commands. It shouldn’t be too stressful or difficult. In fact, it should be just the opposite.

When you open your terminal, you’re usually greeted with some information. In that information is typically your username. However, it’s possibly that this is no longer true. You could have it display anything you wanted, plus there’s a chance you’re logged in remotely and just can’t remember which terminal window is connected to which device.

So, there are some reasons why these commands exist. I mean, you should probably know your username. That’s not the kind of thing I forget, but I am getting older. Still, the commands exist and must exist for a reason.

My motto is that they wouldn’t have provided a path if they didn’t want you to get to the destination. (That’s not really my motto.) So, if there’s a command that’ll help you find your username in Linux, you might as well know it and know how to use it.

Find Your Username In Linux:

This article requires an open terminal, like many other articles on this site. If you don’t know how to open the terminal, you can do so with your keyboard – just press CTRL + ALT + T and your default terminal should open.

With that terminal open, let’s try echoing the $USER variable. It’s nice and easy, and it looks like this:

You can also use who and whoami as commands:

And:

There’s also w, which shows logged in users – so you may be able to deduce your username from that list. It just looks like a:

The ‘w’ command nice and handy, and has a bit more information about the user. It would look similar to this:

w command's output showing username
See? It’s pretty easy to see that there’s a user logged in – and more!

As you can see, there are a number of ways. I’m sure that I’m missing some. Feel free to chime in and add to the list. Basically, if you want to share it with the world, leave a comment. Otherwise, many readers know where to find me.

Closure:

And now you have another article. This one isn’t fancy, nor is it something you’re going to need all the time. However, it’s still a very basic and useful tool to add to your Linux toolbox. Things like these are the fundamentals. How to find your username in Linux is an absolute beginner move and a move that leads you forward to more knowledge.

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.

Use A GUI To Manage Debian’s Software Repositories

You can manage Debian’s software repositories through the terminal, even by just editing some files. It’s not terribly hard, but there’s an “easier” way, and that’s to do so graphically. It may require some prep work, but it’s remarkably easy after you’ve taken care of that.

People sometimes ask if we prefer the GUI or a terminal, and that’s a complex answer. This is one of those times when it’s split up the middle, at least for me. For example, it’s absurdly easy to add repositories in the terminal, but it’s slightly more complex if you want to then remove those repositories. You need to remember things like the repo’s name – when you can just look in a GUI application and quickly see which repositories you want to remove.

If you’re new to Linux, Debian is a Linux-based operating system that is quite popular to build off. There are tons of derivatives, and even some derivatives of derivatives. Debian was released in 1993 and is the parent of popular distros like Ubuntu, and the grandparent of the many distros based on Ubuntu.

You know, I suspect more people use Debian derivatives than directly use Debian itself.

GUI Manage Debian’s Software Repositories:

I’m going to assume two things:

  1. You have a brand new copy of Debian freshly installed. 
  2. You only downloaded the first .iso and it is no longer mounted. 

Some folks can possibly skip ahead to Step 2.

Step 1: Remove CDROM from your sources

The first thing you’re going to need to do is get rid of the cdrom entries in your apt sources. If you try to install (or update) and have cdrom listed in your sources then you’ll bump into some errors. So, let’s take care of that.

Start your terminal with the trusty CTRL + ALT + T.

To fix that cdrom thing we’re going to need to edit your ‘sources.list’ file. To do that, we enter this in the terminal:

Find the line that starts with ‘cdrom’ and put a # in front of the line to comment it out. It should look a bit like this:

edit debian's sources list
That’s opened after editing. Your version may look different.

Next, save it. Seeing as we’re using nano, you do that pressing CTRL + X, then Y, and then ENTER.

At this point, we need to make sure the system knows we made that change. So, we’re going to update the lists of software available with this command:

That shouldn’t take all that long, especially if it’s a new installation. It takes even less time if you grabbed updates during the installation process. 

Step #2: Install software-properties-gtk

Seeing as you already have your terminal open, you needn’t open a new one. We can do the rest of this in just a single command. It’s actually really easy to do the rest, to set it up to graphically manage Debian’s repositories. Just enter the following command:

Press the ENTER key, type your password if asked, and press on the ENTER key again after entering the ‘y’ response if/when asked.

That’s it. You’re done. When you look in your menu, you should see a new entry called “Software & Updates”. Root around in the tabs and revel in your new tool to manage repos and a few other things.

Software & Updates on Debian
See? Mission accomplished and it wasn’t even all that painful! Congratulations!

You manage the main repositories on the first tab and can manage other repositories with the second tab. You can graphically manage Debian’s repositories without a whole lot of extra work. The hardest part was in removing the cdrom from the software sources. There are some other tools included, but those are the two main tools – or the two main tools that this article focuses upon.

Closure:

And there you have it! You have a new article. This one told you how to graphically manage Debian’s repositories. All in all, I’d say it’s pretty easy and something a fairly new user should be able to do on their own. Good luck and ask questions if you get stuck.

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.

Linux Tips
Creative Commons License
This work is licensed under a Creative Commons Attribution 4.0 International License.