unixnewlines.pl

MOTIVATION

I find it hard to believe that, in the fourth quarter of the year 2008, there is a text editing tool which does not always properly handle files with various line endings, and even harder to believe that Apple's Xcode is that tool.  Yet, it is true.  Also, I could not find a tool which would recursively change the line endings of all text files in a directory tree to unix line endings.  So, I wrote unixnewlines.pl.

HOW TO USE IT

This tool will search recursively (deeply, all subdirectories) all Paths, and for all files found with a filename extension in Qualified Extensions, will list the number of DOS (0x0d, 0x0a) or Old Macintosh (0x0d) newlines.  After you interact with your approval, the files listed with these Non-Unix newlines will be ovewritten with edited versions that have all Non-Unix newlines changed to Unix (0x0a) newlines.

Usage is:

   unixnewlines -ext1 [-ext2] ... [path/to/dir1] [path/to/dir2] ...

Arguments beginning with a dash '-' are interpreted as filename extensions.  A filename extension is the part of the filename after the last dot/period '.'.  The file extensions so found in arguments together form the list of Qualified Extensions.  Use just a dash '-' to list filenames with no extension.

Arguments not beginning with a dash are interpreted as Paths, which together form the list of Paths.

Files will be decoded using UTF8 and scanned for the DOS newline 2-character sequence and the Old Macintosh newline 1-character sequence.  Therefore, files with qualified File Extensions that are not encoded UTF8 or ASCII (ASCII is a subset of UTF8) may be mangled undesirably.

After you interact with your approval and files are overwritten, there is no going back, unless of course you are running Time Machine.

BUGS

I tested it on several directories and fixed all the bugs I found.  Test it on something non-critical and let me know if you find any more bugs!

REPOSITORY

Visit this project's GitHub Repository to download, fetch, or clone this code.


SYSTEM REQUIREMENT

I've tested this in Mac OS 10.5, but it should work on any unix system.

If it complains that it can't find Find::File, you can download that from cpan.org by typing the command:

   sudo perl -MCPAN -e shell

(If that indicates an error, get the cpan module from here.)  The sudo is used since some modules want to install either source code or, more frequently, documentation, in system directories such as /Library/Perl.  Perl will launch and give you a cpan shell with a prompt

   cpan>

At this prompt, use the 'install' command to download and install a module you need.  In this case:

   install Find::File

and then watch the show until it is over, indicated by the cpan> prompt returning.  Leave this command-line window open in case this requires additional pre-requisites.

Try to run unixnewlines.pl again.

When you are done with cpan, type quit.