向文件名添加前缀/后缀而不需要重新输入?

向文件名添加前缀/后缀而不需要重新输入?

我想做如下的事情

$ tmp=name*
$ mv $tmp new_$tmp
$ ls new_*
$ new_name-with-stuff-i-dont-want-to-type name-with-stuff-i-dont-want-to-type

设置环境变量似乎有效

$ $tmp
$ name-with-stuff-i-dont-want-to-type

但当我这样做时不是mv

$ mv $tmp new_$tmp
$ ls new_*
$ new_name*

问题:有没有一种聪明简单的方法可以节省一些打字?

答案1

我一直很喜欢 bash 扩展:

ls
name-with-stuff-i-dont-want-to-type

mv name-with-stuff-i-dont-want-to-type{,_old}

ls
name-with-stuff-i-dont-want-to-type_old

这相当于:

mv name-with-stuff-i-dont-want-to-type name-with-stuff-i-dont-want-to-type_old

对于前缀:

ls
name-with-stuff-i-dont-want-to-type

mv {,new_}name-with-stuff-i-dont-want-to-type

ls
new_name-with-stuff-i-dont-want-to-type

为了方便文件重命名:

ls
fileFOOname

mv file{FOO,BAR}name

ls
fileBARname

答案2

对于zsh,这通常是您的用途zmv

autoload zmv # in ~/.zshrc
zmv 'name*' 'tmp_$f'

答案3

如果有多个文件匹配,name*$tmp包含所有这些名称,并用空格分隔。相反,将其$tmp视为数组并对其中的每个元素进行操作:

for file in $tmp; do 
    mv "$file" "new_$file"
done

使用双引号以避免将包含空格的名称拆分为多个名称。

相关内容