Repair Your Filesystem With FSCK

Over the years, we’ve covered the fsck command fairly extensively but I’ve never really written an article about how you repair your filesystem with fsck. This may very well be the last time we have an fsck article on this site. It seems like I’ve covered everything you need to know.

This article is about using fsck when you’re booted into your operating system. You can’t run fsck against a mounted drive. So, if you want to check and repair your root filesystem, the following article may be of assistance:

Repair Your Linux Filesystem With a Live USB or DVD

That’s a fairly popular article, as it ranks well in search engines. I’ve also recently authored this article:

Enable fsck On System Start

This article will be fairly simple and reasonably short. We’ll see if that turns out to be true, ’cause I wrote this before I finished the article! We shall see!

The Tools:

You’ll only need a couple of tools to repair your filesystem with fsck. First and foremost, you’ll need a terminal. I’ll assume you have a terminal. All the tools in this article will require an open terminal. You can usually press CTRL + ALT + T to open your default terminal.

You will also need…

lsblk:

The lsblk application is the first tool we’ll be using. Using lsblk is how we’ll identify your partitions. You almost certainly won’t need to install anything as this is one of those tools installed by default. You can verify that lsblk is installed by running:

If you check the man page, you’ll see that this is indeed the tool for the job.

lsblk – list block devices

See? We want to list block devices – that will show us the partitions and various filesystems.

umount:

The next tool we’ll use is the umount command. You can’t run fsck against a mounted filesystem, so we’ll need first to unmount the devices before we can repair them with fsck. You can verify that umount is installed with this command:

Again, we’ll check the man page to ensure that this is the correct tool for the job.

umount – unmount filesystems

See? I wouldn’t steer you wrong. This is the tool we need to unmount filesystems before we use fsck, the next tool, to repair filesystem errors.

fsck:

This is the tool we’ll be using. We’re going to repair your filesystem with fsck, assuming your filesystem needs to be repaired. This must be a Linux filesystem, meaning Ext2, Ext3, Ext4, etc… You’ll need other tools for filesystems otherwise formatted. Once again, you can verify that fsck is installed with the following command:

Again, if we check the man page, you’ll see that this is the right tool for the job.

fsck – check and repair a Linux filesystem

I’d say that these are the best tools we can use for today’s task.

Repair Your Filesystem With FSCK:

I gave instructions above that told you how you can usually open your terminal. That keyboard combination is not true with every distro and I don’t know why. It seems to me that it should be a standard. If you’re using such a distro, you can open your terminal from the application menu.

With your terminal open, we first need to identify your filesystems. You can do that with the following command:

You’ll get an output similar to this one:

You can also run:

That has the added benefit of showing limited information and which filesystems are identified as Linux filesystems. I use lsblk above because I use that often and want a consistent set of directions across the site. However, an example output from the above command would contain information like this:

You can run that command if you prefer. You may have to scroll up to see all of the information. In the output I shared above, you’ll notice that it didn’t include any information about /dev/sda* filesystems. As those are mounted and need to be mounted (in my case) it doesn’t matter.

At this point, you need to identify the filesystem you want to check. If you use the first command, you’ll need to remember to add /dev to the front. So, it’s not sdb2, it’s /dev/sdb2. If you use the second command, it gives you that information.

Now, we’re just going to automatically check for problems and repair them with fsck, but first you need to unmount the filesystem. To do that, you just run the following command:

Then, you can just run fsck against the filesystem’s device ID, like so:

For example, I could run:

With the -p flag enabled, it will automatically check the filesystem for errors and repair them – unless they’re really serious. If they’re serious errors, it will ask you what to do.

NOTES:

Unfortunately, I don’t have any filesystems that need to be repaired. Linux is rather robust and our filesystems are usually fairly healthy. If you suffer from random reboots without properly shutting down, you might have a different experience.

When you’re done with this, you can remount the filesystems you checked for errors. Frankly, that’s a whole other article. If you open your file manager, the GUI one, you can often mount filesystems right from there. You can also unmount them, but we did that right there in the terminal.

Again, you can’t unmount your root partition or any other important partitions. If you use separate partitions like /home or /dev, you’ll need to use a live USB to repair those mounted filesystems. This command will only work with Linux filesystems. It is not going to work with FAT, EXFAT, NTFS, etc., it will only work with Linux filesystems.

That might be a good reason to run the 2nd command ( sudo fsck -l) where you’ll get an output like this:

Look at /dev/sda1, where it properly identifies the type as an “EFI System”. That means that it’s not a supported filesystem and fsck will do you no good when you target it. I figure you’re smart enough to know the differences and what you have used, so lsblk is easier – and more consistent as we use that command frequently here on Linux-Tips.us.

Closure:

Well, it turns out that I was mistaken. I thought this was going to be a quick and easy article. I think it’s easy enough, but it wasn’t very quick. It has been a mixed bag lately as I play around with the formatting of various articles. This often makes them longer, but I’m okay with that. Plus, I type quickly!

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 site. If you scroll down, you can sign up for the newsletter, vote for the article, and comment.

Find The Last Filesystem Check

If you’ve been using Linux for any time, you may have done a filesystem check and today we’ll find out when you did the last filesystem check with the Linux terminal. This may seem a little complicated, at least at the start, but it should be relatively easy. New Linux users should be able to follow along as we try to make Linux more approachable.

Disks have corruption after a while. Given enough time and data, a filesystem check is likely. Fortunately, Linux has many tools that will help you verify the integrity of your data. These tools are generally installed by default, so we shouldn’t need to install any new tools in this tutorial.

The article will have been tested in Lubuntu. The directions in this article are going to work with Ubuntu, official Ubuntu flavors, Linux Mint, ElementaryOS, and many more distros. I’d say they’re fairly generic instructions and that all these tools will be installed by default in most major distros.

You need good data health. This isn’t about hardware health as much as you’d learn from S.M.A.R.T data, it’s about the integrity of your data. You might use the information from this article to decide to run an fdisk command or two. Of course, you might also be interested in one of my more popular articles:

Repair Your Linux Filesystem With a Live USB or DVD

What we’re going to do is find out when the last filesystem check. What you do with that information is up to you. 

We will be using some tools…

lsblk:

The first tool we’ll be using is one of the default Linux tools. The name of that tool is lsblk. You can verify that lsblk is installed with the following command:

The output should look similar to this:

If you run man lsblk in the terminal, you’ll find that it’s described as this:

lsblk – list block devices

We’ll be using this lsblk command to identify drive names. It’s not complicated and you’ll be able to follow along easily enough. I’m sure of it!

tune2fs:

We’ll also be using the tune2fs command in this exercise. As far as I can tell, tune2fs doesn’t have a formal version flag. If you run a typical version command against it, it does spit out the version but it also spits out some usage information. So, that will do for verifying that you do indeed have tune2fs installed. You can run this command:

The output will look weird, but it’ll confirm that tune2fs is installed. 

Of course, the man page describes tune2fs like this:

tune2fs – adjust tunable file system parameters on ext2/ext3/ext4 filesystems

While that might not seem useful, it is. We’ll be using it to get those system parameters to find out when you did the last system check. Trust me! It’ll come in handy.

We’ll also be using a pipe and grep, but we’ve used those many times before.

Find Your Last Filesystem Check:

If it’s not obvious, we’ll be using the terminal for this. So, press CTRL + ALT + T and let’s get this party started.

The first thing you do is you want to identify the name of the drive. Don’t blame me for calling it the name. That’s what the command outputs. That’s what I’m calling it. The command for this is nice and simple, just run this command:

I plugged a bunch of stuff in, so an example output might look like this:

Next, your command looks like this:

This isn’t always going to spit out the right information. If a drive isn’t an EXT* formatted partition/drive, it’s not going to spit out any useful information. There will be no information if you try to use it with your EFI partition, for example.

An example EFI command and output might be:

On the other hand, an example of a good output might be like this:

As you can see, that was checked just a couple of months ago.

Alternatively, as I plugged some rather random stuff into the USB ports, you might see something as old as this:

That’s been a minute or two since the last filesystem check.

If you’re using an NVMe M.2 SSD, this will still work:

So, it’s not all that difficult to find out when you did your last filesystem check.

Closure:

Careful observation may show you that the site has changed a little. There’s now a way to do sponsorship, seeing as I get asked so many times. Nobody ever follows through, but I have had some contact with a couple of agencies. You may start seeing sponsored content soon. Feel free to about sponsoring Linux-Tips if you want.

As for the article, it’s just another easy enough article. I hope I’ve distilled it down enough to make it approachable for even the newest Linux users. It may be good to check to see when you performed your last file system check. If you have data in cold storage, you may want to check that now and again and check your filesystem’s health once in a while. The goal is to prevent data loss through corruption and this should help with that.

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 site. If you scroll down, you can sign up for the newsletter, vote for the article, and comment.

How To: Enable NTP In Linux

Time is very important to the Linux operating system so keeping accurate time is important, which is why we want to enable NTP in Linux. This won’t be a major article and it should be simple enough to follow along. It’s pretty easy to enable NTP in Linux.

If you don’t know, NTP stands for Network Time Protocol. This allows your computer to connect to a networked device that tells your computer the accurate time and then syncs your computer’s clock with that accurate time.

This networked time server is usually just on the public web, but you could do things like run your own NTP server with a GPS device. (GPS uses really, really, really accurate time. In fact, that really accurate time is how GPS works.) Your company may even run its own NTP servers, but there are plenty of public servers available.

While we’re on the subject, your Linux computer keeps time in a very funny fashion. It counts the seconds since the epoch. The “UNIX Epoch” is an arbitrary date and time. Specifically, it counts the seconds since January 1st, 1970 at 00:00:00 UTC. I’ll write an article on the subject at some point. It is also sometimes referred to as “POSIX Time”. In the meantime, you can always look it up at your favorite search engine. 

Time is very important to Linux. After all, the kernel is a task scheduler. It’s also important for accurate record keeping, among other things. It’s also not difficult to enable NTP in Linux.

NOTE: This article assumes you’re using systemd, as most mainstream distros do. If this isn’t true, this article isn’t for you.

Enable NTP In Linux:

Yes, this is another article that requires an open terminal. You can usually just press CTRL + ALT + T to open your default terminal. If that’s not true, fix the keybindings!

If you’re a desktop user, you can probably skip this article. If you’re a desktop Linux user, odds are good that your distro came preconfigured to sync the time already. Let’s go ahead and verify that with this command:

What you’re looking for is these lines:

You’re especially interested in the last line. If that line says that the NTP service is active, you can ignore this article and read one of the prior articles on the timedatectl command:

How To: Find Your Timezone In The Terminal
How To: Change The Timezone

If the NTP service is not active, you can start the service with this command:

That’s all you need to do to enable NTP in Linux.

If you want to disable NTP, that’s just as easy. That command looks like this:

Next, you can run this command to confirm that you’ve enabled NTP:

That should output information that says the NTP service is active. It may not say that the clock has synchronized as that may take some time before the scheduled task runs and syncs your time with a dedicated time-keeping server.

If you want to muck about with the settings, they’re viewed here:

You’d edit that file with Nano, or some other terminal text editor. Before doing so, you should first read the man page, with this command:

There you go! You can enable NTP in Linux!

Closure:

It seems that I’ve developed a few different styles for my article writing. Longer articles get treated differently, as in they’re formatted differently. The shorter articles have been formatted like this for quite a while. I think that’s a comfortable mix, though it is (as always) subject to change. As I learn and grow, so too may the formatting.

Anyhow, this seemed like a fun article to write. I doubt it’ll be all that popular. It’s unlikely to rank all that well in the search engines. Still, it’ll be information that’s on the site, and the more information I have the better I think I’ll be doing.

It’s not always about the traffic. Indeed, it was never really about the traffic. The traffic is secondary. My primary objective is to share information. Today’s article covered how to enable NTP in Linux. Further, it is limited to just those who use systemd.

Most of my readers will have no use for this information. But, there will be someone – and maybe only that one someone – who will find this information and need it to enable NTP on their Linux device. Good. To that reader and my regulars, I say thanks for visiting.

Now my usual blurb at the bottom of every article…

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 site. If you scroll down, you can sign up for the newsletter, vote for the article, and comment.

A Quick Look At The Cat Command

If you want to view the contents of a text file in Linux, you have something called the ‘cat’ command. Being able to use the cat command is important. This article will give you a quick look at the cat command. After all, this was written during the weekend and a quick article seems like a good idea.

It’s possible to view text in the terminal. Many files are just that, able to be read as text. This isn’t true for all files, of course. Trying to read a .tar.gz file as text is unlikely to be fruitful. On the other hand, a .sh file would be trivially read as plain text.

When you’re manipulating text in the terminal, you want a tool like Nano. When viewing text in the terminal, you can use a tool like the cat command. It’s a handy tool.

The cat Command:

You shouldn’t have to install anything. If you enter cat --version in the terminal, you’ll get an output like this:

As you can see, the cat command is a part of the GNU core utilities and, as such, should be installed on almost all Linux systems by default. This means you won’t need to install anything.

If you check the man page, with man cat, you’ll see that it’s described like so:

cat – concatenate files and print on the standard output

Other than the big word concatenate (which means to link files together), you can see that this is the correct tool for the job. What you type in is standard input and what comes out in the terminal is standard output. (That’s often referred to as stdin and stdout, respectively.)

As we’re going to be taking a quick look at the cat command, you might as well understand what it’s for and what it’s expected to do.

Using The cat Command:

As suggested above, this requires an open terminal. Press CTRL + ALT + T and your terminal should open. That’s not always true, but it’s true more often than not.

With your terminal now open, let’s start with something you should have – that is a ~/.bash_history file. If you don’t have a ~/.bash_history, pick a different text file that’s in your current directory (PWD) by using the ls command to search for such. You might have a ~/.bashrc for example.

Now, let’s just view that ~/.bash_history file in your terminal.

If you’re like me, that’s a great deal of text. You’ll need to scroll up to view all of the text, but the cat command is pretty helpful like this.

Let’s say you wanted to create a new file. You may want to read this article:

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

Using the information from that, you can use the cat command to create a new file with this command:

So, try the following:

You should see that there’s now a file named ‘foo’ in your PWD.

There’s a lot one can do with this cat command. You can show the contents of more than one file at a time, like so:

Using the same concepts from above, you can even use > or >> to take the output from cat and insert it into another file.

Finally, and cat has so much more to offer – but this is just a quick look – you can tell cat to number the lines in the output. For example:

See? Nice and easy! The cat command will happily show the output with line numbering enabled. This stuff is easy enough to commit to memory because the cat command is surprisingly powerful for such a little command.

Closure:

So, yeah… It’s a weekend. I have written some larger articles during the past few weeks. That means an easy day is always welcome. Don’t forget that you can make it easier on me by writing an article for the site! I do welcome contributions and it doesn’t matter much if it’s something I’ve already covered – so long as you do a better job than I did!

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 site. If you scroll down, you can sign up for the newsletter, vote for the article, and comment.

Find A File While Ignoring Case Sensitivity

Today we’ll have a fairly short article as we’re simply discussing a way to find a file while ignoring case sensitivity. This can be a pretty handy command to have in your toolbox, so I’ll do what I can to turn this into an article. If you’re into finding files, especially with the terminal, you might just want to read this article!

I might even give you a bonus method! Maybe… It depends on how well you behave yourself! Don’t make me pull this car over!

Alright… Back on topic…

So, what is case sensitivity? We usually have lowercase files and commands. Linux is case-sensitive. If you type sudo APT update nothing is going to happen. Why? Because apt is lowercase. Similarly, a file named  FOO.bar is not the same as foo.bar.

If you try to perform an operation on a file with the wrong case, the operation will not take place. The file isn’t found, because you’ve used the wrong case. In the above command, not even SUDO apt update will work. You have to use lowercase.

Much of Linux is in lowercase. You can’t even use capital letters in your username, by default. Sure, you can force Linux to accept a bad username, but that’s not what the system is expecting.

So too are filenames. Well, mostly… Sometimes you will come across files that use mixed case or maybe all capital letters. You might even have created a file yourself with mixed case letters. When performing an operation on those files, you need to use the correct case.

Now, you can find files while ignoring the case. That’s what this article is all about accomplishing. It shouldn’t be too long.

The Find Command:

The find command is used in the terminal, so we will have to open the terminal to follow along with today’s article. However, on the plus side, you absolutely shouldn’t need to install anything. This find command should be available by default – unless you’re on an extremely stripped-down version of Linux.

If you check the man page, you’ll see that find is the right application for this.

find – search for files in a directory hierarchy

As we’re trying to find files (while ignoring case sensitivity) that seems like one of the good tools for the job – and it is.

Find A File While Ignoring Case Sensitivity:

As mentioned above, we do this in the terminal. More often than not, you can just press CTRL + ALT + T and your default terminal should open. Otherwise, you can open your terminal via your application menu.

With your terminal now open, let’s change directories and create a file.

You can confirm that the file exists by running the ls command.

Next, try this command:

Unless you have another file with that name, you will not get any results from that command. That’s why you need the -iname flag. Simply add that to your command and you can find the file while ignoring case sensitivity.

So, in our case, it’d be:

Sure enough, that finds HeLLo.txt.

If you don’t feel like changing the directory, you can change the command up a bit. The syntax would look like this:

So, in our example:

And, sure enough, that finds the file while ignoring case sensitivity! 

Pretty neat and pretty easy, right?

How about that bonus?

Find A File While Ignoring Case Sensitivity (with locate):

You probably don’t have locate installed by default. You’ll need to install it.

To that end, read this previous article:

And Still Another Way To Find Files By Extension

Specifically this part is of interest (saving you a click):

With your terminal open, we can get mlocate installed with one of the following commands:

Debian/Ubuntu:

Arch/Manjaro/etc:

RHEL/CentOS/etc:

OpenSUSE/GeckoLinux/etc:

There are other package managers. If your package manager isn’t covered, just go ahead and search for “mlocate” and you’ll likely find that it’s available by default.

So, with locate now installed, the syntax is also very simple. You simply need the -i flag to find files while ignoring case sensitivity.

First, we should update the locate database:

Next, try this command:

That will not find the file in your ~/Documents directory. Add the flag and run the command again, like so:

If you want to run that locate command in a specific directory, you can do that. The syntax is a little different than many other commands, but it’s easy enough.

Or, in our case, it’d look like this:

Sure enough, that outputs the information exactly as planned. It will happily ignore the case sensitivity and find your file named HeLLo.txt.

And now you’ve learned two ways to find a file while ignoring case sensitivity. One of the ways is even a nice bonus way to do accomplish this goal.

Closure:

As the headline and opening paragraph suggested, you can find a file while ignoring case sensitivity. It’s not particularly taxing and there are multiple ways to accomplish this goal. I figured I’d give some ‘bonus’ information with this one and share how to find a file while ignoring case sensitivity with the locate command used in the previous article. Why not?

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