Search code examples
findgit-bashrm

How to Programatically Add Quotes to Windows File/Directory Names w


I am using Git for windows to find and remove files using the linux find and rm commands. Some of the file/directory names in the paths returned by find command have spaces such as in the case of "program files" windows directory.

Running the find and rm commands as shown below will return errors because of the spaces in file/directory names. How can I remove these files programatically without receiving errors from the rm command?

$ find /c/'program files'/ ~ -type f -iname "securecrt.vbs" 2>/dev/null
/c/program files/VanDyke Software/Clients/Scripts/securecrt.vbs
$
$ find /c/'program files'/ ~ -type f -iname "securecrt.vbs" 2>/dev/null | xargs rm
rm: cannot remove '/c/program': No such file or directory
rm: cannot remove 'files/VanDyke': No such file or directory
rm: cannot remove 'Software/Clients/Scripts/securecrt.vbs': No such file or directory

Solution

  • It is better to use a -exec directive:

    find /c/'program files'/ ~ -type f -iname "securecrt.vbs" -exec rm {} \;
    

    That does work even with files inclulding space in their name.