将 bash 类型 `${i%.mp3}` 语法与 xargs 一起使用?
使用示例(人为的!):将 .mp3 文件重命名为 .txt。
我希望能够做这样的事情,
find -name '*.mp3' -print0 | xargs -0 -I'file' mv file ${file%.mp3}.txt
这不起作用,所以我通过 sed -e 's/.mp3//g 管道
,就可以了。find
的输出'
不过看起来有点老套; 有没有办法使用常用的 bash ${x%y}
语法?
Example of usage (contrived!): rename .mp3 files to .txt.
I'd like to be able to do something like
find -name '*.mp3' -print0 | xargs -0 -I'file' mv file ${file%.mp3}.txt
This doesn't work, so I've resorted to piping find
's output through sed -e 's/.mp3//g'
, which does the trick.
Seems a bit hackish though; is there a way to use the usual bash ${x%y}
syntax?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不,
xargs -0 -I'file' mv file ${file%.mp3}.txt
将不起作用,因为file
变量将由 xargs 程序扩展,并且不是外壳。实际上,将其称为变量并不完全正确,在 xargs 手册中将其称为“replace-str”。更新:要仅使用 xargs 和 bash (而不是 sed 或类似的),您当然可以让 xargs 启动 bash 进程,然后该进程可以执行您想要的任何替换:
No,
xargs -0 -I'file' mv file ${file%.mp3}.txt
will not work because thefile
variable will be expanded by the xargs program and not the shell. Actually it is not quite correct to refer to it as a variable, it is called "replace-str" in the manual of xargs.Update: To just use xargs and bash (and no sed or similar) you can of course let xargs start a bash process which then can do whatever substitution you want:
如果您安装了 GNU Parallel:
如果您的文件名不包含 \n(换行符),这也可以:
观看介绍视频以了解有关 GNU Parallel 的更多信息 http://www.youtube.com/watch?v=OpaiGYxkSuQ
If you have GNU Parallel installed:
If your file names do not contain \n (newline) this will work too:
Watch the intro video to learn more about GNU Parallel http://www.youtube.com/watch?v=OpaiGYxkSuQ
只是添加另一个建议,即使它不使用 shell 的替换机制,而是使用 sed 的替换机制。
换句话说,为每个文件名创建一个
mv
命令行,并将结果传递给sh
。如果任何文件的文件名中包含撇号(单引号),此操作将会失败。我发布这篇文章主要是为了对抗“如果你只有 xargs,那么每个问题都会看起来像钉子”综合症。
并非所有 sed 都具有完全相同的语法。特别是,我不能 100% 确定您可以在一些粗糙的旧 SunOS 上使用多个 -e 参数(在
/usr/ucb/blah/barf/vomit/xpg4/bin 等内容中查找与 XPG 兼容的 sed /sed
)Just to add another suggestion, even though it doesn't use the shell's substitution mechanism, but instead, sed's.
In other words, create an
mv
command line for each file name, and pass the result tosh
.This will fail if any of the files have an apostrophe (single quote) in their file name. I mainly post this to counter the "if all you have is xargs, every problem will look like a nail" syndrome.
Not all seds have exactly the same syntax. In particular, I'm not 100% sure you can have multiple -e arguments on some crufty old SunOS (look for the XPG-compatible sed in something like
/usr/ucb/blah/barf/vomit/xpg4/bin/sed
)重击 4+
Bash 4+
http://people.sc.fsu.edu/~jburkardt/pl_src /重命名/重命名.html
http://people.sc.fsu.edu/~jburkardt/pl_src/rename/rename.html