使 xargs 处理包含空格的文件名 [英] Make xargs handle filenames that contain spaces

查看:37
本文介绍了使 xargs 处理包含空格的文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$ ls *mp3 | xargs mplayer  

Playing Lemon.  
File not found: 'Lemon'  
Playing Tree.mp3.  
File not found: 'Tree.mp3'  

Exiting... (End of file)  

我的命令失败,因为文件Lemon Tree.mp3"包含空格,因此 xargs 认为它​​是两个文件.我可以让 find + xargs 使用这样的文件名吗?

My command fails because the file "Lemon Tree.mp3" contains spaces and so xargs thinks it's two files. Can I make find + xargs work with filenames like this?

推荐答案

xargs 命令将空白字符(制表符、空格、换行符)作为分隔符.

The xargs command takes white space characters (tabs, spaces, new lines) as delimiters.

您可以使用 -d 选项仅缩小换行符 (' ') 的范围,如下所示:

You can narrow it down only for the new line characters (' ') with -d option like this:

ls *.mp3 | xargs -d '
' mplayer

它仅适用于 GNU xargs.

It works only with GNU xargs.

对于 MacOS:

ls *.mp3 | tr \n \0 | xargs -0 mplayer

更简单实用的方法(当不需要进一步处理文件名时):

The more simplistic and practically useful approach (when don't need to process the filenames further):

mplayer *.mp3

这篇关于使 xargs 处理包含空格的文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆