Recursively find files containing carriage returns

| No Comments | No TrackBacks
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.

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

No TrackBacks

TrackBack URL: http://www.coderfoo.com/cgi-bin/mt/mt-tb.cgi/2

Leave a comment

Pages

About this Entry

This page contains a single entry by Andy published on July 7, 2009 10:42 AM.

Welcome was the previous entry in this blog.

Dynamically build SQL statements for Perl DBI is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.

Powered by Movable Type 4.261