用法示例(人为的!):将 .mp3 文件重命名为 .txt。
我希望能够做类似的事情
find -name '*.mp3' -print0 | xargs -0 -I'file' mv file ${file%.mp3}.txt
这行不通,所以我求助于通过 sed -e 's/.mp3//g'
管道传输 find
的输出,这确实诀窍。
虽然看起来有点老套; 有没有办法使用通常的 bash ${x%y}
语法?
最佳答案
不,xargs -0 -I'file' mv file ${file%.mp3}.txt
将不起作用,因为 file
变量将由xargs 程序而不是 shell。实际上将其称为变量并不完全正确,在 xargs 的手册中将其称为“replace-str”。
更新:如果只使用 xargs 和 bash(没有 sed 或类似的),你当然可以让 xargs 启动一个 bash 进程,然后它可以做你想做的任何替换:
find -name '*.mp3' -print0 | xargs -0 bash -c \
'while [ -n "$1" ]; do mv "$1" "${1%.mp3}.txt" ; shift; done;' "bash"
关于bash - 将 bash 类型 `${i%.mp3}` 语法与 xargs 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3891651/