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

查看:128
本文介绍了使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选项将其范围缩小到换行符('\ n'),如下所示:

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

ls *.mp3 | xargs -d '\n' mplayer

它仅适用于GNU xargs.

It works only with GNU xargs.

对于BSD系统,请使用-0选项,如下所示:

For BSD systems, use the -0 option like this:

ls *.mp3 | xargs -0 mplayer

此方法更简单,并且也可以与GNU xargs一起使用.

This method is simpler and works with the GNU xargs as well.

对于MacOS:

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

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

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