bash script to copy all files in a directory and convert uppercase characters to lowercase


So, I recently found out that the version of PHPList that we were using on a client site (2.10.2) had a nasty bug in it where it would convert all URLs in a text version of an email message to lowercase. This bug is fixed in the latest version (2.10.3) but it left a number of subscribers complaining that the links in their email were broken.

I wrote this handy bash script to fix those links for emails that had already been sent out.

To use this script, follow these steps on your server command line. This is a bash script.

First, copy this code to your clipboard:


#!/bin/sh

# This script takes the contents of a directory and will make a copy
# of every file converting any uppercase characters to lowercase
# (Useful because PHPList had a bug where it converted URLs to
# lowercase in text-only mailing list messages and the links were
# therefore broken. This script will enable those links to work.)

for file in *
do
if [ -f "$file" ]; then
filelower=`echo $file | tr "[:upper:]" "[:lower:]"`
if [ -f "$filelower" ]; then
echo "You are trying to copy a file that has the same name as an existing file."
echo "$filelower file was NOT copied."
echo ""
else
if [ -d "filelower" ]; then
echo "You are trying to copy a file that has the same name as an existing directory - $filelower"
echo "$filelower file was NOT copied."
echo ""
else
# not a file or directory. make a copy of the file
cp "$file" "$filelower"
fi
fi
fi
done

Next, you are going to create a bash script file.

$ vi cp_files_to_lowercase.sh

Press i and then paste in the code.

Press ESC then :wq to write the file and quit vi.

Next you are going to set the executable permission on the file.

$ chmod 700 cp_files_to_lowercase.sh

Next, change directory to the place you want to run the script on. In this case, we’ll change to the images directory.

$ cd images/

Let’s see what’s in the directory currently:

$ ls

$ Image1.jpg IMAGE2.jpg image3.jpg

Now run the script.

$ ../cp_files_to_lowercase.sh

The script will make a copy of any file that has an uppercase character in it and name it the same thing except all lowercase. It will tell you if the file already exists as a completely lowercase file (it won’t copy a file onto itself).

Now let’s see what it did:

$ ls

$ Image1.jpg image1.jpg IMAGE2.jpg image2.jpg image3.jpg

Now any URLs should work and any previous text-version emails that were sent out using the old version of PHPList should now have working links again.

DISCLAIMER: This script will copy files on your server. I am not responsible for any problems that this causes. I’ve tested this on my server, but you’d be wise to test what will happen first. If you add an echo in front of the cp command in the script you will see displayed what it would do without actually doing it.

, , , ,

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

sell diamonds