sed - awk, sed : one liner command for removing spaces from _all_ file names in a given folder?

标签 sed awk command

前:

eng-vshakya:scripts vshakya$ ls
American Samoa.png                  Faroe Islands.png                   Saint Barthelemy.png

后:
eng-vshakya:scripts vshakya$ ls
AmericanSamoa.png                   FaroeIslands.png                    SaintBarthelemy.png

在原型(prototype)下面尝试过,但它不起作用 :( 抱歉,在 awk/sed 方面不是很好 :(
ls *.png | sed 's/\ /\\\ /g' | awk '{print("mv "$1" "$1)}'

[以上是原型(prototype),我猜真正的命令是:
ls *.png | sed 's/\ /\\\ /g' | awk '{print("mv "$1" "$1)}' | sed 's/\ //g'

]

最佳答案

当您可以在纯 bash 中执行此操作时,无需使用 awk 或 sed。

[ghoti@pc ~/tmp1]$ ls -l
total 2
-rw-r--r--  1 ghoti  wheel  0 Aug  1 01:19 American Samoa.png
-rw-r--r--  1 ghoti  wheel  0 Aug  1 01:19 Faroe Islands.png
-rw-r--r--  1 ghoti  wheel  0 Aug  1 01:19 Saint Barthelemy.png
[ghoti@pc ~/tmp1]$ for name in *\ *; do mv -v "$name" "${name// /}"; done
American Samoa.png -> AmericanSamoa.png
Faroe Islands.png -> FaroeIslands.png
Saint Barthelemy.png -> SaintBarthelemy.png
[ghoti@pc ~/tmp1]$ 

请注意 ${foo/ /}符号是 狂欢 ,并且在经典的 Bourne shell 中不起作用。

关于sed - awk, sed : one liner command for removing spaces from _all_ file names in a given folder?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11752878/

相关文章:

linux - 用新行替换字符串中的单个字符

linux - 在 shell 脚本中打印命令名称

linux - 将匹配模式的第一个字母大写

sed - Unix to tail 等待特定字符串或超时后退出的解决方案

python - 使用 python 替代 awk 来代替 Linux 命令

linux - 在命令行中将 TSV 文件合并在一起

wpf - 命令参数引用MVVM模式中的 View 控件是否正确?

swift - swift 中命令下拉列表的名称

awk - 如何减少文本文件中的所有数组索引?

linux - 在 UNIX 中删除 CSV 文件中字段之间的空格