How do I convert all files in a folder to a different line ending? (on Windows)
How do I convert all files in a folder to a different line ending (linux or mac)? I am using Windows.
Right now my approach is to use Winscp to copy to a linux box and then run dos2unix recursively on the folder.
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 byfind . -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.
- Locate src in Windows Explorer.
- Right click and open Git Bash on that folder from Windows context menu.
- Run:
find . -name "*.java" -exec dos2unix {} \;
.
And that's it!