【问题标题】:Renaming multiple folders by stripping a known prefix, also if folder names contain whitespaces [duplicate]通过去除已知前缀重命名多个文件夹,如果文件夹名称包含空格[重复]
【发布时间】:2020-11-14 03:49:08
【问题描述】:

我即将将一些 IMAP 帐户移至另一台服务器。旧服务器设置为添加前缀 .INBOX。到每个文件夹,但不是新文件夹。所以我尝试重命名所有文件夹,删除.INBOX。前缀。

我在 bash 中做了什么:

for name in .INBOX.*;
do
newname="$(echo "$name" | cut -c8-)";
mv '$name' '$newname';
done

但是这个脚本只重命名了不包含空格的文件夹。带空格的会导致错误消息。

有什么诀窍?

【问题讨论】:

  • mv "$name" "$newname" ?
  • 是的,你说得对!将单引号更改为变量 $name 和 $newname 的双引号后,它对我有用。

标签: bash directory rename prefix


【解决方案1】:

使用引用。在下面的示例中,我更喜欢使用while read

例子

$ 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
$

【讨论】:

  • 谢谢。问题是单引号而不是双引号。但是您的示例也可以完成这项工作。
猜你喜欢
  • 1970-01-01
  • 2016-01-17
  • 1970-01-01
  • 2014-03-05
  • 1970-01-01
  • 2014-01-19
  • 2016-02-05
  • 2018-09-06
  • 2015-03-16
相关资源
最近更新 更多