将文件和目录移动到 UNIX 中的子文件夹
当我输入
mv ../* .
mv: cannot move '../<dir name>' to a subdirectory of itself, './<dir name>'
shell/mv 命令如何检测此行为?
When I type
mv ../* .
mv: cannot move '../<dir name>' to a subdirectory of itself, './<dir name>'
How does the shell/mv command detect this behaviour?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
据我所知,mv 使用
rename()
POSIX 系统调用,规范中表示:...以及无数其他详细的故障模式。
操作系统大概是通过比较层次结构中的中间目录的inode编号来实现通用VFS层的检测。
As far as I know, mv uses the
rename()
POSIX syscall, for which the specification says:... along with a myriad of other detailed failure modes.
The operating system presumably implements the detection at the general VFS layer by comparing the inode numbers of the intermediate directories along the hierarchy.
shell 负责在将命令行传递给
mv
之前扩展*
等通配符,并且它直接这样做,仅基于存在的文件/目录,而不知道什么该程序是什么或者它可能想用这些名称做什么。因此,在这种情况下,../*
会扩展到父目录中的每个文件/目录名称,包括当前目录mv
遍历它接收到的参数列表,尝试将除最后一个之外的所有参数移到最后一个,这会导致您看到的错误。The shell is responsible for expanding wildcards like
*
before passing the command line tomv
and its does so directly, based solely on what files/directories exist, with no knowledge of what the program is or what it might want to do with those names. So in this case,../*
is expanded to every file/directory name in the parent directory, including the current directory <dir name>. Thenmv
goes through the list of arguments it receives, trying to move every one except for the last into the last, which causes the error you see.