Make xargs handle filenames that contain spaces

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

    Alternative solutions can be helpful...

    You can also add a null character to the end of your lines using Perl, then use the -0 option in xargs. Unlike the xargs -d '\n' (in approved answer) - this works everywhere, including OS X.

    For example, to recursively list (execute, move, etc.) MPEG3 files which may contain spaces or other funny characters - I'd use:

    find . | grep \.mp3 | perl -ne 'chop; print "$_\0"' | xargs -0  ls
    

    (Note: For filtering, I prefer the easier-to-remember "| grep" syntax to "find's" --name arguments.)

提交回复
热议问题