开发者

Moving files and directories to a sub-folder in UNIX

When I type

mv ../* . 
mv: cannot move '../<dir name>' to a subdirectory of itself, './<dir name>'

How does the shell/mv command detect this behav开发者_开发问答iour?


As far as I know, mv uses the rename() POSIX syscall, for which the specification says:

The rename() function shall fail if:

(...)

[EINVAL] [CX] The new directory pathname contains a path prefix that names the old directory.

... 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.


The shell is responsible for expanding wildcards like * before passing the command line to mv 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>. Then mv 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜