John Mark Mitchell John Mark Mitchell - 1 year ago 114
Linux Question

mv command moves file but reports error: cannot stat no such file or directory

I am hoping that a more experienced set of eyes will find something obvious that I am missing or will be able to help me work around the errors that

are producing. Up for the challenge?

Basic idea:

I have a bash script in which I am automating the move of files from one directory to another.

The problem:

When I run the script, periodically I get the following error from the

mv: cannot stat `/shares/directory with spaces/test file.txt': No such file or directory
. The error code from the
command is 1. Even more odd, the file move actually succeeds sometimes.

In addition, I have a branch of logic in the script that will alternately use
to move/copy specific files (from the same local file system source and destination as the
command mentioned above). I get a similar error related to the stat() system call:

rsync: link_stat "/shares/directory with spaces/test file.txt" failed: No such file or directory (2)
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1070) [sender=3.0.9]

This error does not always manifest itself when the script is run. Sometimes it completes the file move without complaint, while other times it will return the error consistently when the script is run successive times.

There is one additional ingredient you should be aware of (and I am growing to suspect this as a key ingredient in my grief): the directory /shares/ is a directory that is being monitored by an installation of dropbox -- meaning it is watched and mirrored by an installation of dropbox. At this point, I am unable to determine if
is somehow locking the file, or the like, such that it cannot be stat-ed. To be clear, the files are eventually freed from this state without further intervention and are

The code:

mv -v --no-clobber "${SOURCEPATH}${item}" "${DESTINATIONPATH}${item}"

More info:

The following might, or might not, be relevant:

  • mount
    indicates the filesystem is ext4

  • Presumably, ownership and permissions shouldn't be an issue as the script is being run by root. Especially if the file system is not fuse-based.

  • The base "directory" in the path (e.g. /shares/) is a symlink to another directory on the same file system.

  • The flavor of linux is Debian.


In trying to eliminate any issues with the variable expansion or their contents, I tried hardwiring the bash script like such:

mv -v --no-clobber "/shares/directory with spaces/test file.txt" "/new destination/directory with spaces/test file.txt"
after verifying via
la -al
that "test file.txt" existed. For reference the permissions were: -rw-r--r--

Unfortunately, this too results in the same error.

Other possible issues I could think of and what I have done to try to rule them out:

>> possible issue: slow HDD (or drive is in low power mode) or external USB drive

>> findings: The drives are all local SATA disks set to not park heads. In addition, even when forcing a consistent read from the file system, the same error happens

>> possible issue: non-Linux, NFS or fuse-based file system

>> findings: nope, source and destination are on the same local file system and
indicates the file system is ext4

>> possible issue: white space or other unprintable chars in the file path

>> findings: verified that the source and destination paths where properly wrapped in quotes

>> possible issue: continuation issues after escaped newline (space after \ in wrapped command)

>> findings: made sure the command was all on one line, still the same error

>> possible issue: globbing (use of * in specifying the files to move)

>> findings: nope, each file is specified directly by path and name

>> possible issue: path confusion from the use of local path

>> findings: nope, file paths are fully qualified starting from /

>> possible issue: the files are not actually in the path specified

>> findings: nope, verified the file existed right prior to executing the script via
ls -al

>> possible issue: somehow the --no-clobber of mv was causing issues

>> findings: nope, tried it without, same error

>> possible issue: only files created via dropbox sync to the file system are problematic

>> findings: nope, created a local file directly via
touch new-local-file.txt
and it too produced the same stat() error

My analysis:

The fact that
produce similar stat() errors leads me to believe:

  • there is some systemic underlying boundary case (e.g. file permissions/ownership or file busy) that is not accounted for in the bash script; or

  • the same bug is plaguing me in both the
    and the

Desired outcomes:

1. The root cause of the intermittent errors can identified.

2. The root cause can be resolved or worked around.

3. The bash script can be improved to gracefully handle when the error occurs.

Answer Source

So, with a lot more troubleshooting I found an errant rsync statement some 200 lines earlier in the script that was conditionally executed (thus the seeming inconsistent behavior). That rsync --archive ... statement was being passed /shares/ as its source directory, therefore it effected the /shares/directory with spaces/ subdirectory. That subdirectory was the ${SOURCEPATH} of the troubling mv command mentioned in my post above.

Ultimately, it was a missing --dry-run flag on the rsync --archive ... statement that causing the trampling of the files that the script later expected to pass to mv to process.

Thanks for all who took the time to read my post. Though I am bummed to have spent my and your time on what turned out to be a bug in my script, it is reassuring to know that:
- computers are not irrational
- I am not insane
- there is not some nefarious, deep rooted bug in the linux file system

For those that stumble upon this post in the future because you are experiencing an error of cannot stat, please read my troubleshooting notes above. Much research went into that list. One of those might be your issue. If not, keep debugging, there is an explanation. Good luck!