Make xargs handle filenames that contain spaces

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

    Given the specific title of this post, here's my suggestion:

    ls | grep ' ' | tr ' ' '<' | sed 's|<|\\ |g'
    

    The idea is to convert blanks to any unique character, like '<', and then change that into '\ ', a backslash followed by a blank. You can then pipe that into any command you like, such as:

    ls | grep ' ' | tr ' ' '<' | sed 's|<|\\ |g' | xargs -L1 GetFileInfo
    

    The key here lies in the 'tr' and 'sed' commands; and you can use any character besides '<', such as '?' or even a tab-character.

提交回复
热议问题