1

Given:

  • a large directory tree containing a mixture of:
    • files with LF line endings
    • files with CRLF line endings
    • files with a weird mix of both LF and CRLF line endings (due to a bug)
    • binary files
    • other files that I don't want to touch even if broken
  • paths that might contain spaces

My goal was to leave the LF-only and CRLF-only files alone, and convert the hybrid files to CRLF-only files, while leaving the final two groups alone as well.

This incantation works:

find . (some conditions to exclude stuff I don't want to mess with) |
    xargs -d '\n' unix2dos -ic | cut -c 3- |
    xargs -d '\n' dos2unix -ic | cut -c 3- |
    xargs -d '\n' unix2dos

But now I'm curious whether there is a better / more efficient way to do it. (Using only standard shell tools, not Perl/Python/etc.)

FWIW, the first unix2dos -ic is to filter the initial list to just those files containing CRLF, then dos2unix -ic subfilters to those also containing LF, and the final one actually does the conversion. The cut calls are to trim off the two leading spaces that confuse xargs, and the -d is to resolve issues with spaces in the filenames, since the output isn't quoted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.