Quickly and easily determine if a file has DOS (0x0d 0x0a) or Unix (0x0a) line endings.
This perl script only reads a file's first line (up to 0x0a). If it ends in 0x0d 0x0a, it will report the file as a »dos file«, otherwise as a »unix file«.
dos-or-unix *
might print something like
$dos-or-unix.pl *
admin.bat is unix
aqua.bat is unix
black.bat is dos
blue.bat is dos
cb.bat is dos
cdcb.bat is dos
find-equal-files.test is a directory
hex_dump.pl is unix
kill-procs.pl is unix
dos-or-unix.pl
#!/usr/bin/perl
use warnings;
use strict;
#
# Tells wheater a file is dos or unix
#
# See also show-newline.pl
#
for my $file (@ARGV) {
if (-d $file) {
print "$file is a directory\n";
next;
}
die "$file is not a file" unless -f $file;
open(my $fh, '<', $file) or die;
binmode($fh);
my $last_c = '';
while (read($fh, my $c, 1)) {
if ($c eq "\x0a") {
if ($last_c eq "\x0d") {
print "$file is dos\n";
}
else {
print "$file is unix\n";
}
last;
}
if ($c eq "\x0d" and $last_c eq "\x0a") {
print "$file is mac\n";
last;
}
$last_c = $c;
}
close $fh;
}