Filesystem For Linux And Mac

Sometimes bad things happen to good systems.

Fortunately, you're a Linux user and you have fsck (file system check) to help with a potentially corrupted filesystem. This utility is used for checking and (optionally) repairing the file system.

What Filesystem should I use for an external USB harddrive (500GB), with possibility to share with a Mac and Windows systems, for example at the office. Now it is NTFS but back in the day the NTFS support was not stable under Linux and I think it is not supported on a Mac. I have an external drive hooked up to my Mac, and I'm trying to determine things like, e.g., is this HFS or FAT, is it 32-bit or 64-bit, etc. It seems like there should be some trivial command that.

There are several scenarios where you may want to use fsck. Typically, you would want to run this command if your system will not boot, a device (external drives or storage media) is not functioning properly, or if you have seen evidence of file corruption.

Fsck is a actually a 'front-end' for a number of file system specific checkers like fsck.vfat, fsck.ext2, etc. These do not need to be specified, but you may be able to find more advanced options in the man pages of these more precise commands.

Introduction to the fsck command

The fsck command follows a pattern similar to most Linux commands.

If you do not specify a filesystem, the system will analyze your fstab file (/etc/fstab) for the devices to scan.

You will need to run the command either as root user or use it with sudo.

You can use fdisk or df command to list the hard drive in Linux. This way, you can specify which device to be checked with fsck command.

Unmount the device before running fsck

Do not run fsck on a mounted device, you will need to unmount the target first to avoid damage to your files.

If you try running fsck on a mounted device, you should see an error like this:

Running fsck on a normal, healthy drive looks like this:

While fsck accepts a device name like /dev/sda, you may elect to enter the UUID to avoid confusion with mounting and unmounting devices. The UUID is a fixed value assigned to your device and will not be affected by these system changes.

Understanding exit codes for the fsck command

This is a list of the codes that may be returned from fsck after inspecting a disk. Your exit code will be a sum of these codes if you analyze one disk. If you are using fsck on multiple devices, it will return the bit-wise OR of the two sums.

  • 0 - No errors
  • 1 - File system errors corrected
  • 2 - System should be rebooted
  • 4 - File system errors left uncorrected
  • 8 - Operational error
  • 16 - Usage or syntax error
  • 32 - Fsck canceled by user request
  • 128 - Shared library error

You can check the exit code of the last run command using echo $? command.

Practical usage of the fsck command

Now that you are a tad bit familiar with the fsck command, let's see practical use cases of this command.

Repair a USB disk and other removable devices

Handshaker for windows. For our purpose, let's assume that you have already identified the problematic device /dev/sdb.

First, you need to make sure that the drive has been unmounted:

Now run the fsck command:

Check the output for any errors. If none displayed, check the exit code with echo $?.

There are also some option flags that we can add to allow some automated correction. These commands aren't standardized though, and you should verify the filesystem type and compare documentation from that specific man page.

Despite that, generally you can use -p to allow fsck to automatically apply repairs.

Similarly, -y will apply corrections to any detected filesystem corruption.

Repair the root file system

You cannot unmount the root partition while the system is active. If you suspect your main file system is corrupted, you have to use a different approach here.

There are actually a few different options that you can use. You can run fsck at boot time, in rescue mode, or use a recovery-themed live cd.

Many Linux distributions will automatically force fsck at start up after a certain number of failed boot attempts. If you prefer to take matters into your own hands, you can schedule the system to do this ourselves.

Most modern Linux versions feature a tool called tune2fs.

Presuming your root device is dev/sda, this is the command you would enter.

Now, what's actually happening is that you're changing the system settings so that fsck is run every n number of boots (1 in the example). You could also set this to a standard time interval. The options are days, weeks, or months.

Let's say that you want fsck to run any time that you boot if there hasn't been a check in a week. You could use -i to specify the interval and the command would look like this.

If you're using systemd, you can force run fsck at your next boot by entering the following:

Conclusion

You can always turn to the man-pages for more information. Just use man fsck in the terminal.

I hope you learned something new about the fsck command. If you have any comments or questions, please leave them below.

Become a Member for FREE
Become a member to get the regular Linux newsletter (2-4 times a month) and access member-only content

Join the conversation.

Learn how to remount the file system in the read-write mode under Linux. The article also explains how to check if the file system is read-only and how to clean the file system

Most of the time on newly created file systems of NFS filesystems we see an error like below :

This is because the file system is mounted as read-only. In such a scenario you have to mount it in read-write mode. Before that, we will see how to check if the file system is mounted in read-only mode and then we will get to how to remount it as a read-write filesystem.

How to check if file system is read only

To confirm file system is mounted in read only mode use below command –

Grep your mount point in cat /proc/mounts and observer third column which shows all options which are used in the mounted file system. Here ro denotes file system is mounted read-only.

You can also get these details using mount -v command

In this output. file system options are listed in braces at last column.

Re-mount file system in read-write mode

Linux List File System

To remount file system in read-write mode use below command –

Observe after re-mounting option ro changed to rw. Now, the file system is mounted as read-write and now you can write files in it.

Note : It is recommended to fsck file system before re mounting it.

Filesystem for linux and mac os

Filesystem For Linux And Mac

You can check file system by running fsck on its volume.

Linux Filesystem Commands

Sometimes there are some corrections that need to be made on a file system that needs a reboot to make sure there are no processes are accessing the file system.