【问题标题】:Script to Remove a Letter at the End of some Directories删除某些目录末尾的字母的脚本
【发布时间】:2020-05-08 21:24:07
【问题描述】:

假设我有一个具有这种结构的目录:

我正在制作一个脚本,在末尾有一个字母“x”的情况下删除字母“x”,以便获得这样的结果:

所以,我正在考虑做一个 For 循环去每个目录,然后取文件夹路径的最后一部分

for d in Fruits_and_Vegetables/*/
do
     (cd "$d" && Fruit_or_Vegetable=`basename "$PWD"`)
done

问题是我不知道如何告诉去只拿最后一个目录

然后,我正在考虑修改字符串

echo $Fruit_or_Vegetable | awk '{print substr($1, 1, length($1)-1)}'  # substr(s, a, b) : it returns b number of chars from string s, starting at position a. 
                                                                      # The parameter b is optional, in which case it means up to the end of the string.

问题是我不知道如何告诉 AWK 将“Moscato Giallox”视为一个词,因为当我执行命令时,它返回“Moscat Giallox”而不是“Moscato Giallo”......我也猜我必须放置一个if语句来查看最后一个字母是否是x,并且只在这些情况下执行命令。

能给我一些建议吗,谢谢。

【问题讨论】:

标签: bash directory rename


【解决方案1】:

shoptParameter Expansion 解析可以让这变得非常简单。从上面的目录结构开始:

$: find @(Fruits|Vegetables)
Fruits
Fruits/Grapes
Fruits/Grapes/Muskat Grape
Fruits/Grapes/Muskat Grape/Muscat Ottonel
Fruits/Grapes/Muskat Grape/Muscato Gallox
Fruits/Mangoes
Fruits/Mangoes/Ataulfo Mango
Fruits/Mangoes/Tommy Atkins Mangox
Vegetables
Vegetables/Potatoes
Vegetables/Potatoes/Ratte Potatox
Vegetables/Potatoes/Yukon Gold Potato

您想要的更改代码。

shopt -s extglob  # allow fancy @(...) construct to specify dirs
shopt -s globstar # add double-asterisk for flexible depth
for d in @(Fruits|Vegetables)/**/*x/ # *EDITED* - added trailing / for dirs only
do echo "mv \"$d\" \"${d%x/}/\" "   # show the command first
         mv "$d" "${d%x/}/"         # rename the dirs
done

echo 语句的输出:

mv "Fruits/Grapes/Muskat Grape/Muscato Gallox/" "Fruits/Grapes/Muskat Grape/Muscato Gallo/"
mv "Fruits/Mangoes/Tommy Atkins Mangox/" "Fruits/Mangoes/Tommy Atkins Mango/"
mv "Vegetables/Potatoes/Ratte Potatox/" "Vegetables/Potatoes/Ratte Potato/"

结果:

$: find @(Fruits|Vegetables)
Fruits
Fruits/Grapes
Fruits/Grapes/Muskat Grape
Fruits/Grapes/Muskat Grape/Muscat Ottonel
Fruits/Grapes/Muskat Grape/Muscato Gallo
Fruits/Mangoes
Fruits/Mangoes/Ataulfo Mango
Fruits/Mangoes/Tommy Atkins Mango
Vegetables
Vegetables/Potatoes
Vegetables/Potatoes/Ratte Potato
Vegetables/Potatoes/Yukon Gold Potato

【讨论】:

  • 输出是这样的:mv "Fruits/Grapes/Muscat Grape/Moscato Giallox" "Fruits/Grapes/Muscat Grape/Moscato Giallox/" mv: cannot move 'Fruits/Grapes/Muscat Grape/Moscato Giallox' to a subdirectory of itself, 'Fruits/Grapes/Muscat Grape/Moscato Giallox/Moscato Giallox'
  • 您输入的mv "$d" "${d%x/}/" 准确吗?
  • 试着去掉最后一个 /: mv "$d" "${d%x/}" 在我的模拟中它工作正常,但 mv 有看似神秘的规则来确定正确的行为。 XD
  • 我完全输入了mv "$d" "${d%x/}/",我实际上只是复制和粘贴了..也许是我执行脚本的方式,我过去遇到过问题..我正在使用#!/ bin/bash .. 我以./renaming_names.sh 的身份执行它
  • 省略最后一个 / 会发生这种情况:mv "Fruits/Grapes/Muscat Grape/Moscato Giallox" "Fruits/Grapes/Muscat Grape/Moscato Giallox" mv: cannot move 'Fruits/Grapes/Muscat Grape/Moscato Giallox' to a subdirectory of itself, 'Fruits/Grapes/Muscat Grape/Moscato Giallox/Moscato Giallox'
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-23
  • 1970-01-01
  • 2021-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多