I need to convert filenames to lowercase. What is the best way to do this? I wrote a script which runs in background but it fails sometimes. Here it is:
#! /bin/sh
DIR=$1
if [ ! -e "$DIR" ]; then
echo "No such dir: $DIR"
exit 1
fi
cd $DIR
ls | while read origfile; do lowerfile=`echo "${origfile}" | tr '[:upper:]' '[:lower:]'`; mv "$origfile" "$lowerfile"; done
If a file name exist which already consist of only lowercase letters so no lowercase conversion required, mv command will give you an error (but script continue with other files, so not a big problem) like that “xyz and xyz are the same file”
If filename contains newline character or spaces your while loop will be fail
You can use something like that:
#!/bin/bash
DIR=$1
cd "$DIR"
for file in *; do
if [[ "$file" != "${file,,}" ]]; then
mv -- "$file" "${file,,}"
fi
done