cannot stat error in Linux – meaning and fixes

Using mv to move files and directories has become an everyday task for every Linux user, regardless of the distro they use. Sometimes, users can come across several common error messages written in weird languages, which causes confusion for beginners. In this article, we want to tackle one of the most popular error when using mv or cp in Linux : the “cannot stat” error.

What does “cannot stat” means

Stat is a program that obtains information about a file or directory in Linux. Because in Linux, each program should do one thing only and rely on one another to do other things, programs that perform file system operation should use stat as a standard way to retrieve details like metadata, size, modified date, permissions, etc.

Like any other program, before performing any operation, mv or cp must know some information about the source file/directory and the destination one. The default way to do that is by using stat.

Calling stat on a directory only requires execute permission of the directory containing the file. No read permission of the files is needed.

With that in mind, _every time you see “cannot stat” when using mv, you can safely assume that stat failed to obtain information about the file. In this case, the problem should be in the file/directory itself or the storage device.

Fixing cannot stat ‘filename’: No such file or directory

The error usually means the destination file or directory cannot be found by the system, so it cannot retrieve information.

If you come across “cannot stat” with “No such file or directory” message, checks the destination path first and then the source path for their correctness.

To get the correct full path of any directory in Ubuntu, you can cd to it, then run pwd to obtain the path. Remember to quote this path in any command to avoid errors caused by whitespaces and special characters.

Fixing cannot stat: permission denied

This error means that stat cannot read information about the file because it was denied by disallowing “execute” permission.

You should check the permission of the directory containing the file and the file itself. A quick and dirty solution is using chmod to recursively change the directory permission and all of its contents to 755.

You may need to elevate to root privileges using sudo if you’re not the owner of the folder. However, any changes to protected directories could cause system instability, and we advise carefully examine the directory before recursively chmod it.

Fixing cannot stat ‘foldername’: Input/output error

Among all “cannot stat” errors, the one with “Input/output error” messages is the hardest to debug and fix. In most of the cases, getting this error means there is some kind of file system corruption.

Input/output error in NTFS partitions

Usually, when beginners encounter “Input/output error”, they are accessing files from NTFS-formatted partitions. 

NTFS is Microsoft’s file system standard and has been famous for its complex permission system. Linux is able to manage these partitions thanks to a system program named ntfs-3g, but sometimes it cannot read the disk because of a minor corruption in data.

In this case, you should consider using running a program bundled in ntfs-3g called ntfs-fix. But before performing any fixes to the file system, please be aware that it could result in serious data loss.

If you need an open-source backup solution that could work with cloud storage services, be sure to check out rclone. You can get an easy head start without learning too much about the tool with our guide on rclone common commands.

If you see a “device is corrupted” message, back up your files immediately while you still can, then format the whole drive and check for errors before storing any important data on it.

Input/output error in ext partitions

First, ensure the filesystem is unmounted. If the file system is your root partition then you need to boot into rescue mode or use a live CD/USB flash drive so you can check the filesystem while it is unmounted. 

To run a filesystem check, use the command e2fsck. For example, if you are getting the error on a filesystem that is mounted on the partition /dev/hda4 run the following command:

e2fsck /dev/hda4

If the above command returns with errors about a “bad superblock” then run the following command:

dumpe2fs /dev/hda4

Locate the block number of Backup superblock in the output:

Group 1: (Blocks 8193-16384) **Backup superblock** at **8193**, Group descriptors at 8194-8194 Block bitmap at 8195 (+2), Inode bitmap at 8196 (+3) Inode table at 8197-8447 (+4) 7937 free blocks, 2008 free inodes, 0 directories

Run e2fsck again, this time with the backup superblock number:

e2fsck -b **8193** /dev/hda4
Code language: JavaScript (javascript)

If this does not solve your problem, then reboot the system. Upon booting, a filesystem check will probably be forced. If you boot to a shell, run e2fsck on the partition that is reporting the errors.

2 thoughts on “cannot stat error in Linux – meaning and fixes”

  1. Lots of good information. There maybe something else that causes “cannot stat ‘filename’: No such file or directory”
    Here’s an observation I made after encountering this problem. First the task: copying large quantity of movie/music files from an NTFS USB 3Tb drive to a 7Tb ext4 USB drive. Because the NTFS drive had many file names with spaces I used the following command string IFS=”$(printf ‘\n\t’)” | sudo cp -rnv “Source” “Destination” this worked exceptionally well but would fail after about a half hour with the dreaded cannot stat. Immediately re-entering the command string works fine but this time fails sooner and each subsequent try immediately work and fails sooner yet. But opening a new terminal works just like the first terminal instance. So after each failure I start a new terminal to get through the complete copy . So, its not corruption on the ntfs drive its something that interferes in the process on long sessions. Maybe a memory issue of sorts. I’m a average user with limited experience and welcome your feedback.

    • Since you’ve used a fairly complex command to get the job done, you should switch to the proper tool to do the job. Using cp or mv to copy large quantity of data is tricky sometimes, especially when external storage device is involved. There’s a tool dedicated to do that named rsync and it’s pretty awesome, you should check it out. Let me know if it solves your problem!


Leave a Comment