Make xargs handle filenames that contain spaces

前端 未结 12 1161
北恋
北恋 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:28

    The xargs utility reads space, tab, newline and end-of-file delimited strings from the standard input and executes utility with the strings as arguments.

    You want to avoid using space as a delimiter. This can be done by changing the delimiter for xargs. According to the manual:

     -0      Change xargs to expect NUL (``\0'') characters as separators,
             instead of spaces and newlines.  This is expected to be used in
             concert with the -print0 function in find(1).
    

    Such as:

     find . -name "*.mp3" -print0 | xargs -0 mplayer
    

    To answer the question about playing the seventh mp3; it is simpler to run

     mplayer "$(ls *.mp3 | sed -n 7p)"
    

提交回复
热议问题