【问题标题】:Removing Underscore of Files under a Directory删除目录下文件的下划线
【发布时间】:2020-05-19 18:43:33
【问题描述】:

我想删除某些目录中的文件的下划线

我有这个:

Divulgation
├── Biology
│   └── Dawkins, C. Richard
│       └── Books
│           ├── The_Blind_Watchmaker.pdf
│           ├── The_God_Delusion.pdf
│           └── The_Selfish_Gene_(3rd_Ed.).pdf
├── Chemistry
│   └── Gray, Theodore W
│       └── Books
│           ├── Molecules_The_Elements_and_the_Architecture_of Everything.epub
│           └── The_Elements,_A_Visual_Exploration_of_Every_Known_Atom in_the_Universe.pdf
└── Physics
    ├── Hawking, Stephen
    │   └── Books
    │       ├── A_Brief_History_of_Time_from_the_Big_Bang_to_Black Holes.djvu
    │       ├── My_Brief_History.epub
    │       └── The_Universe_in_a_Nutshell.pdf
    └── Sagan, Carl E
        └── Books
            ├── Billions_and_Billions_Thoughts_on_Life_and_Death_at_the Brink_of_the_Millennium.pdf
            ├── Cosmos.pdf
            └── The_Dragons_of_Eden.epub

我想要这个:

我的尝试:

#!/bin/bash


shopt -s extglob  # allow fancy @(...) construct to specify dirs
shopt -s globstar # add double-asterisk for flexible depth

for f in @(Divulgation)/**/Books/*.pdf           # Search for Filed under Books Directory
do echo "mv "$f" `echo "$f" | sed 's/_/ /g'` "   # Show the command first
         mv "$f" `echo "$f" | sed 's/_/ /g'`     # Rename File by removing Underscores using the s Command
done

错误:

mv Divulgation/Biology/Dawkins, C. Richard/Books/The_Blind_Watchmaker.pdf Divulgation/Biology/Dawkins, C. Richard/Books/The Blind Watchmaker.pdf 
mv: target 'Watchmaker.pdf' is not a directory
mv Divulgation/Biology/Dawkins, C. Richard/Books/The God Delusion.pdf Divulgation/Biology/Dawkins, C. Richard/Books/The God Delusion.pdf 
mv: target 'Delusion.pdf' is not a directory
mv Divulgation/Biology/Dawkins, C. Richard/Books/The Selfish Gene (3rd Ed.).pdf Divulgation/Biology/Dawkins, C. Richard/Books/The Selfish Gene (3rd Ed.).pdf 
mv: target 'Ed.).pdf' is not a directory
mv Divulgation/Chemistry/Gray, Theodore W/Books/The Elements, A Visual Exploration of Every Known Atom in the Universe.pdf Divulgation/Chemistry/Gray, Theodore W/Books/The Elements, A Visual Exploration of Every Known Atom in the Universe.pdf 
mv: target 'Universe.pdf' is not a directory
mv Divulgation/Physics/Hawking, Stephen/Books/The Universe in a Nutshell.pdf Divulgation/Physics/Hawking, Stephen/Books/The Universe in a Nutshell.pdf 
mv: target 'Nutshell.pdf' is not a directory
mv Divulgation/Physics/Sagan, Carl E/Books/Billions and Billions Thoughts on Life and Death at the Brink of the Millennium.pdf Divulgation/Physics/Sagan, Carl E/Books/Billions and Billions Thoughts on Life and Death at the Brink of the Millennium.pdf 
mv: target 'Millennium.pdf' is not a directory
mv Divulgation/Physics/Sagan, Carl E/Books/Cosmos.pdf Divulgation/Physics/Sagan, Carl E/Books/Cosmos.pdf 
mv: target 'E/Books/Cosmos.pdf' is not a directory

帮助...嗯,PDF 是对书籍的评论,而不是书籍本身..

【问题讨论】:

  • 请不要发布文字图片。请将文本作为文本发布。 Show the command first - 为此使用 set -x。不要使用 ` 反引号,而是使用 $(...)。引用你的扩展 - 反引号的结果会在空格上进行分词,所以这一切都会导致来自mv 的大量“不是目录”消息。我想知道,为什么你需要extglob - @(Divulgation) 只是一个元素,只是Divulgation
  • 很难显示目录的树结构.. 只是我猜的错误
  • @RobSweet:您肯定要说 引号,因为单引号会产生不计算反引号的效果。
  • 确保确定真的想要删除下划线留下文件名中的空格。虽然它可能会让你“看起来”更好,但它会为直接从命令行处理文件创造一个引用噩梦。 (“噩梦”可能有点强,所以可以说它比使用名称中没有空格的文件复杂得多。)所以除非你有充分的理由想要将这些复杂性注入到你的文件中-管理生活 - 为自己省去悲伤,并在文件名下划线...
  • 你到底为什么要这样做?与其将名称更改为无法使用的混乱,不如在您想阅读它们时更改演示文稿。也就是说,将下划线保留在适当的位置,以便您有合理的名称,当您希望有人看到它们时,只需使用 tree | tr _ ' '

标签: bash sed rename


【解决方案1】:

很棒的剧本,差不多了。

  • 不鼓励使用反引号`。不要使用它们。请改用$(..)
  • 请始终记住引用扩展名(如果您使用它们,还包括反引号)。
  • 请记住,echo "$f" 对于像 -e 这样的奇怪文件名可能会失败。 Use printf "%s\n" "$f" instead。但由于它是 bash,你可以只 <<<"$f" 使用 here string
  • 您可以使用set -x 查看正在发生的事情。我认为简单的mv -v 可能更...简单。

您需要引用反引号。如果您不引用它们,shell 将执行 word splitting - 将空格上的结果拆分为多个参数。例如,ls $(echo "filename with spaces.txt") 将使用 3 个参数运行 lsls 将解释为 3 个单独的文件 - filenamewithspaces.txt

并用$(...)替换反引号:

for f in Divulgation/**/Books/*.pdf; do
     mv -v "$f" "$(<<<"$f" sed 's/_/ /g')"
done

对于替换单个字符,对我来说使用tr '_' ' ' 似乎比sed 更“合适”的命令。

【讨论】:

  • 如何重命名 djvu 和 epub 文件? ..我是否在末尾添加星号( . 而不是 *.pdf )
  • 为什么不只是 for f in Divulgation/**/Books/*.pdf Divulgation/**/Books/*.djvu Divulgation/**/Books/*.epub; ?使用 extglob 我想你可以Divulgation/**/Books/*.@(pdf|djvu|epub)。但对于更复杂的事情,请考虑使用find,例如管道xargs
  • 没有 extglob for f in Divulgation/**/Books/*.{pdf,djvu,epub}
【解决方案2】:

这可能对你有用(查找、重命名和 GNU 并行):

find Divulgation -path '*/Books/*.pdf' | rename -v 's/_/ /g'

如果要参数化顶层目录,请使用:

parallel --dry find {} -path '*/Books/*.pdf' \| rename -v 's/_/ /g' ::: Divulgation SomeotherVulation

当命令正确时,删除--dryrun 选项

【讨论】:

    猜你喜欢
    • 2023-03-19
    • 2013-07-25
    • 1970-01-01
    • 2016-08-26
    • 2017-03-23
    • 2016-04-15
    • 2013-11-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多