Windows PATH to posix path conversion in bash

前端 未结 6 1548
北荒
北荒 2020-12-04 16:35

How can I convert a Windows dir path (say c:/libs/Qt-static) to the correct POSIX dir path (/c/libs/Qt-static) by

6条回答
  •  悲哀的现实
    2020-12-04 17:15

    My solution works with a list of folders/files and it's done in 2 steps. Suppose you would like to replace a path from D:\example to /example for a list of file where this Windows path has been repetead.

    The first step it changes the backlashes into slashes

    grep -lr "D:\\\\example" /parent-folder | xargs -d'\n' sed -i 's+\\+\/+g'
    

    Note that parent-folder could be root (/) or whatever you like and -d'\n' parameter is necessary if you have filenames or folder names with white spaces.

    Second step it substitutes the D:/example into /example:

    grep -lr "D:/example" /parent-folder | xargs -d'\n' sed -i 's+D:+/example+g'
    

    I wanted to share this solution since it tooks me some time to make this 2 lines but it has been really helpfull job (I'm migrating a Windows App to a Linux Server with tons of Windows paths inside').

提交回复
热议问题