Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git ls-files, how to escape spaces in files paths?

Tags:

A whole BUNCH of files got checked into our git repo before we introduced the .gitignore file. I'm currently trying to clean it up with:

git rm --cached `git ls-files -i --exclude-from=.gitignore`

The thing is MANY of the file paths have spaces in them.

For example:

......
Assets/Aoi Character Pack/Viewer/Resources/Aoi/Viewer BackGrounds/bg12.png.meta
Assets/Aoi Character Pack/Viewer/Resources/Aoi/Viewer BackGrounds/bg13.png.meta
Assets/Aoi Character Pack/Viewer/Resources/Aoi/Viewer BackGrounds/bg14.png.meta
Assets/Aoi Character Pack/Viewer/Resources/Aoi/Viewer BackGrounds/bg15.png.meta
Assets/Aoi Character Pack/Viewer/Resources/Aoi/Viewer BackGrounds/bg16.png.meta
Assets/Aoi Character Pack/Viewer/Resources/Aoi/Viewer BackGrounds/bg17.png.meta
Assets/Aoi Character Pack/Viewer/Resources/Aoi/Viewer BackGrounds/bg18.png.meta
.......

There's a WHOLE LOTTA SPACES in a whole lotta files that I need to get rid of.

I'm looking for an elegant way of either:

  • A: Escape the spaces with a "\", for example

    Assets/Aoi\ Character Pack/Viewer/Resources/Aoi/Viewer\ BackGrounds/bg18.png.meta

-or-

  • B: Have git ls-files pump out my list of files all snuggled neatly in between quotes.

    'Assets/Aoi Character Pack/Viewer/Resources/Aoi/Viewer BackGrounds/bg18.png.meta'

-EDIT-

So far I have tried git ls-files -i --exclude-from=.gitignore | sed 's/\ /\\\ /g'

While that happily outputs the file paths looking as I'd expect, with the spaces escaped..... When I try to execute

git rm --cached `git ls-files -i --exclude-from=.gitignore | sed 's/\ /\\\ /g'`

I get - error: unknown switch `\'

Where I expect something wonky is going on with the pipe.