Bash 删除目录下文件的下划线
我想删除一些目录中的文件下划线 我有这个:Bash 删除目录下文件的下划线,bash,sed,rename,Bash,Sed,Rename,我想删除一些目录中的文件下划线 我有这个: Divulgation ├── Biology │ └── Dawkins, C. Richard │ └── Books │ ├── The_Blind_Watchmaker.pdf │ ├── The_God_Delusion.pdf │ └── The_Selfish_Gene_(3rd_Ed.).pdf ├── Chemistry │ └── Gray, Theod
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是对书籍的评论,而不是书籍本身。很棒的脚本,就在那里
- 不鼓励使用反勾号。不要使用它们。改用
$(..)
- 请始终记住br引用扩展名(如果使用反勾号,也要引用反勾号)
- 请记住,
对于像echo“$f”
这样的奇怪文件名可能会失败。但是因为它是bash,所以您可以只-e
-为此使用这可能适合您(查找、重命名和GNU并行): 如果要参数化顶级目录,请使用:
parallel --dry find {} -path '*/Books/*.pdf' \| rename -v 's/_/ /g' ::: Divulgation SomeotherVulation
如果命令正确,请删除
选项请不要发布文本图像。请以文本形式发布文本<代码>首先显示命令--dryrun
。不要使用“backticks”,而是使用设置-x
。引用您的扩展-反勾号的结果会在空格上进行分词,因此这将导致来自$(…)
的大量“nota directory”消息。我想知道,为什么需要mv
-extglob
只是一个元素,只是@(泄露)
。很难显示目录的树结构。。只是我犯的错误guess@RobSweet:您确实希望使用双引号,因为单引号会产生影响,不会计算反勾号。请确保确实希望删除文件名中留下空白的下划线。虽然它可能会使事情“看起来”更好,但它会为直接使用命令行中的文件创建一个引用噩梦。(“噩梦”可能有点强,所以我们只需说,在处理名称中没有空格的文件时,它会使事情变得复杂一些。)所以,除非你有一个很好的理由想把这些复杂的事情注入你的文件管理生活中——省去你的悲伤,在文件名上留下下划线……你到底为什么要这样做?与其把名字改成不可行的乱七八糟,不如在你想读的时候把它改一下。也就是说,将下划线保留在适当的位置,这样您就有了合理的名称,当您想让人看到它们时,只需执行泄露
即可。如何重命名djvu和epub文件。。我是否在结尾处添加星号(.而不是*.pdf)为什么不在泄漏中为f添加tree | tr"
/**/Books/*.pdf泄漏/**/Books/*.djvu泄漏/**/Books/*.epub代码>?有了extglob,我想你可以
。但是对于任何更复杂的问题,请考虑使用<代码>查找<代码> >例如管道<代码> xARGs<代码>。泄露/**/Books/*.@(pdf | djvu | epub)
find Divulgation -path '*/Books/*.pdf' | rename -v 's/_/ /g'
parallel --dry find {} -path '*/Books/*.pdf' \| rename -v 's/_/ /g' ::: Divulgation SomeotherVulation