This is a "one-liner" that I use sometimes to find files that contain carriage returns. Some files containing carriage returns were committed to a Subversion repository that I manage. These files caused the diff to be corrupt when diffing against a file without the \r. Not a huge issue, but annoying.
This command basically uses a for loop with find to recursively list all files starting from the current directory. In the loop, each file is dumped into a small Perl one-liner that checks each line for a \r and increments a counter. The grep at the end filters out all files with a zero count. You could change \r to anything you want to find.
This produces a list of files with a count of characters found.
This command basically uses a for loop with find to recursively list all files starting from the current directory. In the loop, each file is dumped into a small Perl one-liner that checks each line for a \r and increments a counter. The grep at the end filters out all files with a zero count. You could change \r to anything you want to find.
prompt:~# for a in `find . -type f`; do echo -n "$a: "; perl -e 'my $cr = 0; while (<STDIN>){ $cr += tr/\r//; } print $cr;' < $a; echo; done | grep -v '0$'
This produces a list of files with a count of characters found.
./someapp.cgi: 36
./anotherapp.cgi: 41
./somedir/yetanother.cgi: 199
./anotherapp.cgi: 41
./somedir/yetanother.cgi: 199

Leave a comment