How do I convert all files in a folder to a different line ending? (on Windows)

Installed Cygwin and Dos2unix in Cygwin

find . -type f -exec dos2unix {} \;
Command Breakout:
  • . - Current directory
  • -type f - Only Files
  • -exec - Executes the command immediately following it
  • dos2unix - the command that converts windows line endings to unix line endings.
  • {} - Represents each result returned by find . -type f
  • \; - terminates the command.

Also, If you just wanted to do a specific file pattern, you could do something like this:

find . -name "*.java" -type f -exec dos2unix {} \;
  • -name "*.java" - Only files that end in .java.

You can install git bash instead of cygwin32 (or similar) on Windows. It comes with several unix-like commands (e.g., find, dos2unix, etc.,) in the bash emulation mode. Once installed, converting files from Windows to Unix file endings should be easy.

Let's say you have your files (ending with the suffix .java) in a folder tree called src and you want to convert their endings from Windows to Unix ones.

  1. Locate src in Windows Explorer.
  2. Right click and open Git Bash on that folder from Windows context menu.
  3. Run: find . -name "*.java" -exec dos2unix {} \;.

And that's it!