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
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,
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-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:
If the above command returns with errors about a "bad superblock" then run the following command:
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
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.