简体   繁体   English

mv命令移动文件但报告错误:无法统计没有这样的文件或目录

[英]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 mv and rsync are producing. 我希望更有经验的一组眼睛能找到一些我不知道的东西,或者能够帮助我解决mvrsync产生的错误。 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. 我有一个bash脚本,我可以自动将文件从一个目录移动到另一个目录。

The problem: 问题:
When I run the script, periodically I get the following error from the mv command: 当我运行脚本时,我会定期从mv命令中收到以下错误:
mv: cannot stat `/shares/directory with spaces/test file.txt': No such file or directory . mv: cannot stat `/shares/directory with spaces/test file.txt': No such file or directory The error code from the vm command is 1. Even more odd, the file move actually succeeds sometimes. vm命令的错误代码是1.更奇怪的是,文件移动实际上有时会成功。

In addition, I have a branch of logic in the script that will alternately use rsync to move/copy specific files (from the same local file system source and destination as the mv command mentioned above). 另外,我在脚本中有一个逻辑分支,它将交替使用rsync来移动/复制特定文件(来自与上面提到的mv命令相同的本地文件系统源和目标)。 I get a similar error related to the stat() system call: 我得到一个与stat()系统调用相关的类似错误:
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. 还有一个你应该注意的额外成分(我越来越怀疑这是我悲伤中的一个关键因素):目录/ shares /是一个由Dropbox安装监控的目录 - 这意味着它被监视并通过安装Dropbox进行镜像。 At this point, I am unable to determine if dropboxd is somehow locking the file, or the like, such that it cannot be stat-ed. 此时,我无法确定dropboxd是否以某种方式锁定文件等,使其无法进行统计。 To be clear, the files are eventually freed from this state without further intervention and are mv -able. 为了清楚起见,文件最终会在没有进一步干预的情况下从此状态中释放出来并且是mv -able。

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 mount表示文件系统是ext4
  • Presumably, ownership and permissions shouldn't be an issue as the script is being run by root. 据推测,由于脚本是由root运行的,因此所有权和权限不应成为问题。 Especially if the file system is not fuse-based. 特别是如果文件系统不是基于熔丝的。
  • The base "directory" in the path (eg /shares/) is a symlink to another directory on the same file system. 路径中的基本“目录”(例如/ shares /)是同一文件系统上另一个目录的符号链接。
  • The flavor of Linux is Debian. Linux的味道是Debian。

Troubleshooting: 故障排除:
In trying to eliminate any issues with the variable expansion or their contents, I tried hardwiring the bash script like such: 在试图消除变量扩展或其内容的任何问题时,我试图像这样硬连接bash脚本:
mv -v --no-clobber "/shares/directory with spaces/test file.txt" "/new destination/directory with spaces/test file.txt" after verifying via ls -al that "test file.txt" existed. mv -v --no-clobber "/shares/directory with spaces/test file.txt" "/new destination/directory with spaces/test file.txt"在通过ls -al验证“test file.txt”存在之后。 For reference the permissions were: -rw-r--r-- 作为参考,权限是:-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 >>可能的问题:慢速HDD(或驱动器处于低功耗模式)或外部USB驱动器
>> findings: The drives are all local SATA disks set to not park heads. >>调查结果:驱动器都是本地SATA磁盘,设置为不停放。 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 >>可能的问题:非Linux,NFS或基于熔丝的文件系统
>> findings: nope, source and destination are on the same local file system and mount indicates the file system is ext4 >>调查结果: nope,source和destination在同一个本地文件系统上, mount表示文件系统是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) >>可能的问题: globbing(在指定要移动的文件时使用*)
>> findings: nope, each file is specified directly by path and name >>调查结果: nope,每个文件都由路径和名称直接指定

>> possible issue: path confusion from the use of local path >>可能的问题:使用本地路径的路径混乱
>> findings: nope, file paths are fully qualified starting from / >>调查结果: nope,文件路径完全合格,从/开始

>> 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 >>调查结果: nope,在通过ls -al执行脚本之前验证了文件的存在

>> possible issue: somehow the --no-clobber of mv was causing issues >>可能的问题:不知何故,mv的--no-clobber引起了问题
>> findings: nope, tried it without, same error >>调查结果:不,没试过,同样的错误

>> possible issue: only files created via Dropbox sync to the file system are problematic >>可能的问题:只有通过Dropbox同步到文件系统创建的文件才有问题
>> findings: nope, created a local file directly via touch new-local-file.txt and it too produced the same stat() error >>调查结果: nope,直接通过touch new-local-file.txt创建了一个本地文件,它也产生了相同的stat()错误

My analysis: 我的分析:
The fact that mv and rsync produce similar stat() errors leads me to believe: mvrsync产生类似stat()错误的事实让我相信:

  • there is some systemic underlying boundary case (eg file permissions/ownership or file busy) that is not accounted for in the bash script; 在bash脚本中没有考虑到一些系统性的基础边界情况(例如文件权限/所有权或文件繁忙); or 要么
  • the same bug is plaguing me in both the mv and the rsync scenarios. 同样的bug在mvrsync场景中都困扰着我。

Desired outcomes: 预期的结果:
1. The root cause of the intermittent errors can be identified. 1.可以确定间歇性错误的根本原因。
2. The root cause can be resolved or worked around. 2.根本原因可以解决或解决。
3. The bash script can be improved to gracefully handle when the error occurs. 3.可以改进bash脚本以便在发生错误时正常处理。

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). 因此,通过更多的故障排除,我发现错误的rsync语句在脚本中早先约200行有条件地执行(因此看似不一致的行为)。 That rsync --archive ... statement was being passed /shares/ as its source directory, therefore it effected the /shares/directory with spaces/ subdirectory. rsync --archive ...语句正在传递/shares/作为其源目录,因此它/shares/directory with spaces/子目录实现了/shares/directory with spaces/ That subdirectory was the ${SOURCEPATH} of the troubling mv command mentioned in my post above. 那个子目录是上面帖子中提到的令人不安的mv命令的$ {SOURCEPATH}。

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. 最终,它是rsync --archive ...语句中缺少的--dry-run标志,导致脚本稍后期望传递给mv处理的文件的践踏。

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 - linux文件系统中没有一些邪恶的,根深蒂固的错误

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. 对于那些因为您遇到cannot stat错误而未来偶然发现这篇文章的人,请阅读我上面的故障排除说明。 Much research went into that list. 很多研究都进入了这个清单。 One of those might be your issue. 其中一个可能是你的问题。 If not, keep debugging, there is an explanation. 如果没有,继续调试,有一个解释。 Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM