【发布时间】: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 _ ' '。