Make xargs handle filenames that contain spaces

前端 未结 12 1154
北恋
北恋 2020-11-29 15:21
$ ls *mp3 | xargs mplayer  

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

Exiting... (End of file)  
12条回答
  •  無奈伤痛
    2020-11-29 15:48

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

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

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

    It works only with GNU xargs.

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

    ls *.mp3 | xargs -0 mplayer
    

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

    For MacOS:

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

提交回复
热议问题