Bash 通过剥离已知前缀重命名多个文件夹,如果文件夹名称包含空格,也是如此

Bash 通过剥离已知前缀重命名多个文件夹,如果文件夹名称包含空格,也是如此,bash,directory,rename,prefix,Bash,Directory,Rename,Prefix,我要将一些IMAP帐户移动到另一台服务器。旧服务器已设置为添加前缀.INBOX。到每个文件夹,但不是新文件夹。因此,我尝试重命名所有文件夹,剥离.INBOX。前缀 我在bash中所做的: for name in .INBOX.*; do newname="$(echo "$name" | cut -c8-)"; mv '$name' '$newname'; done 但是这个脚本只重命名了这些不包含空格的文件夹。带空格的会导致错误消息 诀窍是什么?使用引

我要将一些IMAP帐户移动到另一台服务器。旧服务器已设置为添加前缀.INBOX。到每个文件夹,但不是新文件夹。因此,我尝试重命名所有文件夹,剥离.INBOX。前缀

我在bash中所做的:

for name in .INBOX.*;
do
newname="$(echo "$name" | cut -c8-)";
mv '$name' '$newname';
done
但是这个脚本只重命名了这些不包含空格的文件夹。带空格的会导致错误消息


诀窍是什么?

使用引号。在下面的示例中,我更喜欢在阅读时使用

范例

$ ls -l *.txt
-rw-rw-r-- 1 ftpcpl ftpcpl 0 Jul 24 11:36 hello and goodbye.txt
-rw-rw-r-- 1 ftpcpl ftpcpl 0 Jul 24 11:36 hello.txt
$ echo "hello.txt" > lista.txt
$ echo "hello and goodbye.txt" >> lista.txt
$ more lista.txt
hello.txt
hello and goodbye.txt
$ while IFS= read -r file; do mv -- "$file" "$file".new ; done < lista.txt
$ ls -l *.new
-rw-rw-r-- 1 ftpcpl ftpcpl 0 Jul 24 11:36 hello and goodbye.txt.new
-rw-rw-r-- 1 ftpcpl ftpcpl 0 Jul 24 11:36 hello.txt.new
$
$ls-l*.txt
-rw-rw-r--1 ftpcpl ftpcpl 0 Jul 24 11:36 hello and bye.txt
-rw-rw-r--1ftpcpl ftpcpl 0 Jul 24 11:36 hello.txt
$echo“hello.txt”>lista.txt
$echo“hello and debye.txt”>>lista.txt
$more lista.txt
hello.txt
你好,再见.txt
$while IFS=read-r文件;do mv--“$file”$file.new;完成
mv“$name”“$newname”
?是的,你说得对!在将vars$name和$newname的单引号改为双引号后,它对我起了作用。谢谢。问题是单引号而不是双引号。但你的例子也能起作用。