Linux mv:不能用非目录覆盖目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20705677/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
mv: cannot overwrite directory with non-directory
提问by anarchist
Is it possible to get around this problem?
是否有可能解决这个问题?
I have a situation where I need to move some files to 1 directory below.
我有一种情况,我需要将一些文件移动到下面的 1 个目录。
/a/b/c/d/e/f/g
problem is that the filename inside g/
directory is the same as the directory name
问题是g/
目录内的文件名与目录名相同
and I receive the following error:
我收到以下错误:
mv: cannot overwrite directory `../297534' with non-directory
Example:/home/user/data/doc/version/3766/297534 is a directory, inside there is a also a file named 297534
示例:/home/user/data/doc/version/3766/297534 是一个目录,里面还有一个名为 297534 的文件
so I need to move this file to be inside /home/user/data/doc/version/3766
所以我需要把这个文件移到 /home/user/data/doc/version/3766
CommandThis is what I am running: (in a for loop)
命令这是我正在运行的:(在 for 循环中)
cd /home/user/data/doc/version/3766/297534
mv * ../
采纳答案by lreeder
You can't force mv to overwrite a directory with a file with the same name. You'll need to remove that file before you use your mv command.
您不能强制 mv 用同名文件覆盖目录。在使用 mv 命令之前,您需要删除该文件。
回答by merlin2011
Add one more layer in your loop.
在循环中再添加一层。
Replace mv * ../
with
替换mv * ../
为
for f in `ls`; do rm -rf ../$f; mv $f ..; done
This will ensure that any conflict will be deleted first, assuming that you don't care about the directory you're overwriting.
这将确保首先删除任何冲突,假设您不关心要覆盖的目录。
Note that this willblow up if you happen to have a file inside the current directory which matches the current directory's name. For example, if you're in /home/user/data/doc/version/3766/297534
and you're trying to move a directory called 297534
up. One workaround to this is to add a long suffix to every file, so there's little chance of a match
请注意,如果您碰巧在当前目录中有一个与当前目录名称匹配的文件,这将会爆炸。例如,如果您在/home/user/data/doc/version/3766/297534
并尝试移动调用的目录297534
。一种解决方法是为每个文件添加一个长后缀,这样匹配的可能性很小
for f in `ls`; do mv $f ../${f}_abcdefg; done