bash 使用空格重命名文件时出错 - mv target 不是目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26519301/
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
bash error renaming files with spaces - mv target is not a directory
提问by Jim Hines
I'm trying to rename a bunch of files which contain spaces in them, getting rid of the spaces. I thought I found the correct bash command:
我正在尝试重命名一堆包含空格的文件,以摆脱空格。我以为我找到了正确的 bash 命令:
for f in *.txt; do mv \"$f\" ${f/ /}; done
However, this gives the error, "mv: target is not a directory" for each file. If I replace 'mv' with 'echo mv' in the command, it prints the proper mv command for each file, and if I type any of those mv commands individually, they work. For example, if I have 2 files, "a .txt", and "b .txt", and run the command above, I get:
但是,这会为每个文件提供错误“mv:目标不是目录”。如果我在命令中用 'echo mv' 替换 'mv',它会为每个文件打印正确的 mv 命令,如果我单独输入这些 mv 命令中的任何一个,它们就会起作用。例如,如果我有 2 个文件,“a .txt”和“b .txt”,并运行上面的命令,我得到:
mv: target 'a.txt' is not a directory
mv: target 'b.txt' is not a directory
If I type the command:
如果我输入命令:
for f in *.txt; do echo mv \"$f\" ${f/ /}; done
I get:
我得到:
mv "a .txt" a.txt
mv "b .txt" b.txt
I've found another way to do this, using "rename", but I would like to know why this doesn't work.
我找到了另一种方法来做到这一点,使用“重命名”,但我想知道为什么这不起作用。
回答by John1024
Try:
尝试:
for f in *.txt; do mv "$f" "${f// /}"; done
Three points:
三点:
The quotes around a shell variable should not be escaped.
In general, it is a good idea to put double-quotes around every reference to a shell variable.
${f/ /}
removes just the first occurrence of a space. To remove all spaces, use${f// /}
.
不应转义 shell 变量周围的引号。
通常,在对 shell 变量的每个引用周围加上双引号是一个好主意。
${f/ /}
只删除第一次出现的空格。要删除所有空格,请使用${f// /}
.
What went wrong
什么地方出了错
$ touch {a,b}" .txt"
$ ls *.txt
a .txt b .txt
$ for f in *.txt; do mv \"$f\" ${f/ /}; done
mv: target `a.txt' is not a directory
mv: target `b.txt' is not a directory
The expression \"$f\"
does not behave like it is double quoted. It expands to two arguments, such as "a
and .txt"
, where the double-quotes are treated as normal characters, just like the a
is a normal character. Because there are three arguments to mv
("a
and .txt"
and a.txt
), mv
believes that you are trying to move the first two arguments to the third and the third is required to be a directory. Since the third is not a directory, it issues an error message.
该表达式的\"$f\"
行为不像是双引号。它扩展为两个参数,例如"a
and .txt"
,其中双引号被视为普通字符,就像a
是普通字符一样。因为mv
( "a
and.txt"
和a.txt
)有三个参数,mv
相信你是想把前两个参数移到第三个,第三个需要是目录。由于第三个不是目录,因此会发出错误消息。
回答by sdfgeoff
Because this is the first thing that came up on google when googling this error, I thought I'd add a bit.
因为这是谷歌搜索这个错误时出现在谷歌上的第一件事,我想我会补充一点。
This error occurs if you have more than two arguments and the last result is not a directory.
如果您有两个以上的参数并且最后一个结果不是目录,则会发生此错误。
This works:
这有效:
mv file1.txt output.txt
This does not:
这不会:
mv file1.txt file2.txt
In my case I was doing: mv prefix_* output_file_name
to ensure a downloaded file had a consistant name, but another file had appeared in the directory, restulting in the "mv target is not a directory" error
在我的情况下,我正在做:mv prefix_* output_file_name
确保下载的文件具有一致的名称,但另一个文件出现在目录中,导致“mv target is not a directory”错误