Bash Script which recursively makes all text in files lowercase

bash

Solution

To convert a file from lower case to upper case you can use `ex` (a good friend of `ed`, the standard editor):

ex -s file <<EOF
%s/[[:upper:]]\+/\L&/g
wq
EOF

or, if you like stuff on one line:

ex -s file <<< $'%s/[[:upper:]]\+/\L&/g\nwq'

Combining with `find`, you can then do:

find . -type f -exec bash -c "ex -s -- \"\$0\" <<< $'%s/[[:upper:]]\+/\L&/g\nwq'" {} \;

This method is 100% safe regarding spaces and funny symbols in the file names. No auxiliary files are created, copied or moved; files are only edited.

Edit.

Using glenn jackmann's suggestion, you can also write:

find . -type f -exec bash -c 'printf "%s\n" "%s/[[:upper:]]\+/\L&/g" "wq" | ex -- -s "$0"' {} \;

(the pro is that it avoids awkward escapes; the con is that it's longer).

Problem

I'm trying to write a shell script which recursively goes through a directory, then in each file converts all Uppercase letters to lowercase ones. To be clear, I'm not trying to change the file names but the text in the files. Considerations: - This is an old Fortran project which I am trying to make more accessible - I do not want to create a new file but rather write over the old one with the changes - There are several different file extensions in this directory, including .par .f .txt and others What would be the best way to go about this?

Original source