Remove last blank line of file

I have a number of files (arranged in directories) which have last line blank,

I am trying to synchronize my code with other env and due to this blank lines, all files error out as different although only difference is that of balnk line at end of file.

Is there a way I can recursively remove this blank line from all files, or atleast from all files in a folder.

Please Note I donot want any other blank lines to be removed from file except the last.

Please help!

for i in *.ext; do lines=`wc --chars $i | cut -d\  -f 1`; let lines-=1; dd if=$i of=$i.new bs=1 count=$lines ; mv $i.new $i; done

You could try this, even though I reckon it's a rather trivial and bumpy approach. The idea behind it is to copy the file but omit the last character and thus remove the blank line.

More elegant/performant solutions might be achieved with awk, which is a science of its own :wink:

I would ask how your comparing the files? If you use 'diff' then add the '-B' option to ignore blank lines and the '-q' option to just report if the files are different. With that approach the blank lines are no longer an issue.