在 Bash 中,我想重命名一个文件,以便-
删除前缀 up to ,但为什么它不能与大括号扩展一起使用?
$ ls
Thomas Anderson, Michael Dahlin-Operating Systems
$ mv {Thomas\ Anderson,\ Michael\ Dahlin-,}Operating\ Systems
mv: target ‘Operating Systems’ is not a directory
答案1
您的文件包含,
大括号扩展所特有的内容,因此您的大括号扩展扩展为3字符串,而不是您想要的两个。
你可以试试:
$ printf '%s\n' {Thomas\ Anderson,\ Michael\ Dahlin-,}Operating\ Systems
Thomas AndersonOperating Systems
Michael Dahlin-Operating Systems
Operating Systems
看看大括号扩展是如何扩展的。
快速解决方法是转义,
:
$ printf '%s\n' {Thomas\ Anderson\,\ Michael\ Dahlin-,}Operating\ Systems
Thomas Anderson, Michael Dahlin-Operating Systems
Operating Systems
答案2
也许最简单的方法是使用printf
和set --
。
只是简短的版本:
$ set -- {"Thomas Anderson, Michael Dahlin-",}"Operating Systems"
$ mv "$@"
$ ls
Operating Systems
或者更详细的描述:原来不是你想要的:
$ printf '%s\n' {Thomas\ Anderson,\ Michael\ Dahlin-,}Operating\ Systems
Thomas AndersonOperating Systems
Michael Dahlin-Operating Systems
Operating Systems
当它变成你想要的(引用是最简单的方法):
$ printf '%s\n' {"Thomas Anderson, Michael Dahlin-",}"Operating Systems"
Thomas Anderson, Michael Dahlin-Operating Systems
Operating Systems
只需更改printf
为set --
并使用mv "$@"
$ mkdir mydir
$ cd mydir
$ touch 'Thomas Anderson, Michael Dahlin-Operating Systems'
$ ls
Thomas Anderson, Michael Dahlin-Operating Systems
$ printf '%s\n' {"Thomas Anderson, Michael Dahlin-",}"Operating Systems"
Thomas Anderson, Michael Dahlin-
Operating Systems
$ set -- {"Thomas Anderson, Michael Dahlin-",}"Operating Systems"
$ printf '%s\n' "$@"
Thomas Anderson, Michael Dahlin-
Operating Systems
$ mv "$@"
$ ls
Operating Systems