Check Your Linux Kernel Log For Errors With ‘dmesg’

You can check your kernel logs for errors with dmesg, if you need to. I previously wrote an article about checking your error logs with KSystemLog and checking the error logs graphically is my preferred way to check for errors. KSystemLog also shows more than just kernel logs.

However, a GUI environment is not always an option and you can use ‘dmesg‘ in a pinch. This will only output the kernel ring buffer. There are other other error logs, so this is just one way. Future articles will cover ways to read other logs.

The ‘dmesg’ application stands for ‘diagnostic message’ and prints the ring buffer from the kernel. Run by itself, it’ll output  a ton of information, so this article will also help you try to make sense of the output.

If you’re curious, the man page defines dmesg as:

dmesg – print or control the kernel ring buffer

And, well, that’s what it does. It does what it says on the tin. The buffer it’s reading is full of information, messages the kernel has left. Not all that information is about errors, but in those messages are error notifications.

Running dmesg by itself is likely a bit overwhelming. Feel free to try it out right now. Crack open your terminal, just press CTRL + ALT + T, and enter the following:

The output is way more information than you probably want to wade through – especially while being frustrated. So, let’s see if we can make this data more useful.

Read Error Messages With ‘dmesg’:

I hope you opened your terminal in the entry section. If you didn’t, you should do so now. Go ahead and open it, I’ll wait here until you’re back…

We already know that running dmesg by itself throws a ton of output. It’s the data in the kernel ring buffer and it’d be exhausting to go through it line by line.

So, first, if you’re checking the logs then there’s likely a reason. That reason will help you with this next command. You’ll need a pipe and grep so that you can process the output more easily. Let’s say you’re having monitor issues, which would lead you to try:

You can grep for other things, such as “Audio” or even “Mouse”. Go ahead and give those a try. You may have errors you don’t even know about.

Most of the time, dmesg is pretty uninteresting. You can actually filter it further, often to just a few lines – by just showing the lines that contain errors. To do that, just try:

If you want to clear out the logged data, you can do that easily enough. You will need to use sudo, and the command is:

Those are really the most important aspects of checking your logs for errors with dmesg. You can run man dmesg to learn about a few more options. As far as my experience goes, you won’t really have a need for the other flags.

Again, I wrote an article about KSystemLog and that will graphically show you your logs – including the kernel’s buffer. If you have a choice, you might as well use a GUI for this sort of thing.

Closure:

Yup… There’s another article. It’s one more step towards reaching my goal. Before we know it, that year will have passed and I’ll need to make some decisions. Until then, let’s keep the party rocking.

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.

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.

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.

Count Word Frequency In A Text File

Have you ever wanted to count the word frequency in a text file? Have you ever wanted to know how many times you used a word in a document? Well, with just two simple commands (grep and wc) we’re going to learn how to do just that.

As a writer, I try to avoid using the same words over and over again. It’s not just bad form, using the same words repeatedly actually makes the document more difficult to comprehend. The brain needs variety (and key concept repetition) or else it just kinda checks out and doesn’t pay much attention.

There are other times when you may want to count the number of words used in a document, perhaps as a reference. “Your honor, the document in question contained my client’s name 245 times – and none of the uses were factual!” I’m sure you can concoct a scenario where you may want this information. You can probably come up with one that doesn’t even involve a judge!

You can also use it a little more broadly, and we’ll cover that. For now, let’s make sure we’re all working on the same page. Press CTRL + ALT + T and open up your terminal, and then enter the following commands:

There. Now that we’re on the same page (you have the text file in your downloads folder) we can all work with the same list of random numbers – instead of random words. (All we care about is that there are characters.)

Count Word Frequency:

Well, you already have the terminal open and you’ve already downloaded my random numbers file. We’ll substitute numbers for words – as words are just a string of characters. So, seeing as you’re prepared…

Trust me, it won’t make a difference that we’re using numbers. By the time I’m done explaining this, you’ll understand and apply it to words (or other characters) all on your own. It’s pretty straightforward and easy to understand.

Let’s say we wanted to count the instances of 62829. The command would look like this:

If you run that command, you’ll see that that string of characters occurs just once. That’s expected and correct.

You can also do things like finding all the instances of 1 (or any single character) in the list with this command:

You can be even more complicated and find all the times a 7 immediately follows a 2. That command would look like this:

(There are three instances of 27.)

So, what’s going on? Well, you are using grep (to search) the contents of the file. You are then piping the output to wc where the number of lines (instances) are being counted.

You can probably be pretty fancy with this, but I just wanted to give a quick overview. Mostly, I figured it’s a good excuse to dig out grep and wc – and who doesn’t like panning for nuggets in text?

Closure:

Yup… This one isn’t a very long (or complicated) article. That’s okay. I like articles of all shapes and sizes. This article will help you count word frequency, something we all may need at one point or another. Sure enough, Linux makes this a pretty simple task.

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.

Time A Command To See How Long It Takes To Run

Have you ever wanted to time a command? Well, you can! Linux includes the ability to time a command, so that you can know how long it took for a command to be processed. 

Today, we’ll be using the ‘time‘ command. Time is simply (and accurately) described in the man page as:

time – run programs and summarize system resource usage

The time command is a pretty nifty tool and the output will tell you the real time that it took, as well as the system time, to run the command you’re timing. There’s really not a whole lot more to say about the time command. It does what it says on the tin. It times stuff!

This is going to just be a quick article and should be easy to follow. There’s not a whole lot to explain and it’s pretty straightforward. If you’re looking to get your feet wet playing in the terminal, this is probably a good article to start with.

Time A Command:

Just like oh so many of these articles, you’ll need an open terminal. So, let’s crack open your default terminal by pressing CTRL + ALT + T on your keyboard.

Now, let’s take the simple command to list everything in a directory:

To find out how long it took to list all the files and folders in a directory, you could use:

The output of that command tells you how long it took to list all the items in a directory beneath the results. It should not have taken long. If you want to try something bigger, something that lasts longer, you can take a look at this command:

That should take a just a little more time to run in your terminal, but how much longer? Well, you can can actually see how long it really took by adding ‘time’ in front of it. Obviously, it’d look something like this:

The output at the end is something like this:

real 0m0.566s
user 0m0.423s
sys 0m0.143s

The ‘real‘ is how much time it really took. The ‘user‘ is how much time it took for the user. The ‘sys‘ is how much time it took for the system – the amount of time that the kernel actually devoted to running that command.

You can time all sorts of stuff. Like, here’s an example output from me updating my system with time sudo apt update (not all of us have big, fat pipes from the ISP):

When I purged Krita (from a previous article), the output from time sudo apt purge krita looks like:

Have fun with it, if you want. Find out how long those tasks actually are, so that you can get a reasonable estimate of where you’re spending your time in the terminal. You can even use it like time nano <file_name> and see how long it took you to edit a file in the terminal!

Like I said, have fun with it. You might find some of those tasks that seem to go slow really don’t take all that much time. Maybe you’ll find out that the reverse is true? One thing is pretty certain, however. If you didn’t have it before, you now have ‘time’ as a new tool in your Linux toolbox.

Closure:

Well, there’s another article. This one has taught you how to time a command in the Linux terminal. It’s a little tool, probably not all that useful, but it’s one that’s there. If you happen to use the time command on a regular basis, please leave a comment letting us know why. I’m sure there’s some real-world uses, but other than looking for bottlenecks or real-time optimizations I can’t really think of any.

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.