LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Converting Uppercase to Lowercase (https://www.linuxquestions.org/questions/linux-software-2/converting-uppercase-to-lowercase-123398/)

AMMullan 12-06-2003 08:57 PM

Converting Uppercase to Lowercase
 
Hey all :-)

I've got a whole bunch of files that i want to convert to lower case, when I started scripting in Linux I read the Linux Shell Scripting Tutorial and use the script that I made from there to rename uppercase filenames to lowercase.

The question I have is how would you go about changing the filenames if they have spaces or are directories? The script that is in the tutorial works but not for these files....

fr0zen 12-06-2003 09:27 PM

So you have a set of files and directories that may or may not be in capital letters, and may or may not contain spaces. You want the capital letters converted to lowercase letters, and the spaces removed entirely?

Just making sure I am understanding you correctly.

AMMullan 12-06-2003 09:32 PM

Almost, basically I want an unconditional change from uppercase to lowercase, no matter about spacing or if it's a folder.....

:)

fr0zen 12-06-2003 09:36 PM

Oh. So, folders receive the case conversion as well as the files. I figured that. Not sure what you mean by spacing though.

AMMullan 12-06-2003 09:39 PM

Basically if there's a file called "I'm back" I need "i'm back"

fr0zen 12-06-2003 11:03 PM

Here's an example of it working...

Code:

[frozen@Fr0ZeN scripts]$ ls test
file space  lowercasefile  n FILE sPAcE  normaldir/  UppER SpAcES DIR/  with spaces dir/
[frozen@Fr0ZeN scripts]$ ./utol test
[frozen@Fr0ZeN scripts]$ ls test
file space  lowercasefile  n file space  normaldir/  upper spaces dir/  with spaces dir/

Finally, here's the code:

Code:

#!/bin/sh
#
# Rename all files and directories within a directory to have lowercase letters
# + skips over files that lack upper case letters
# - non recursive, possibly just add "-R" to 'ls'
#
# (c) 2003 Kyle Gibson, free for non-commercial use
# may be redistributed provided this header is left
# in tact. Feel free to modify.
#
# License: GPL
##################################

usage()
{
        echo "Usage: $0 [directory]"
        exit 1;
}

test -d "$1" || usage

DIR="$1"

ls "$DIR" | grep -e "[A-Z]" | \
while read file; do
        N=`echo -n "$file" | tr "[:upper:]" "[:lower:]"`
        mv "$DIR/$file" "$DIR/$N"
done


elfoozo 10-18-2005 07:32 PM

Sorry to bump such an old thread. Just goes to show what kind info you can locate on LQ with the search function!

I'm hunting a solution for converting files to lowercase and a friend pointed me to this URL.

Source:
ftp://garbo.uwasa.fi/unix/ts/namedown

Code:


#!/bin/sh
#
# To make this Bourne shell script operative apply once:
#  chmod 700 namedown
#  rehash

# Alter the general path for BSD vs. Sys V compatibility
#
if [ -d /usr/ucb ] ; then
  PATH=/usr/ucb:$PATH ; export PATH
fi

case $# in

  0)
    echo
    echo "====================================================="
    echo "namedown, Convert file names to lower case and ; to ."
    echo "By Hannu Hirvonen and Timo Salmi Sun 18-Mar-2001"
    echo "http://www.uwasa.fi/~ts/ and http://www.uwasa.fi/~hh/"
    echo "====================================================="
    echo
    echo "Usage: namedown [FILENAME(S)]"
    echo
    echo "Converts file names only.  Directory names are not affected"
    echo
    ;;

  *)
    for oldname in $*
    do
      newfile=`basename ${oldname} | tr '[A-Z;]' '[a-z.]'`
      dirname=`dirname ${oldname}`
      newname="${dirname}/${newfile}"
      oldname="${dirname}/`basename ${oldname}`"
#Don't convert a file into itself
      if [ "${newname}" = "${oldname}" ]; then
        echo > /dev/null
#Don't convert directory names
      elif [ -d "${oldname}" ]; then
        echo > /dev/null
#Don't convert if the file does not exist
      elif [ ! -f "${oldname}" ]; then
        echo > /dev/null
#Don't overwrite existing files
      elif [ -f "${newname}" ]; then
        echo "${oldname} not converted, file ${newname} already exists" 2>&1
#Don't move to subdirectories if they happen to exist
      elif [ -d "${newname}" ]; then
        echo "${oldname} not converted, directory ${newname} already exists" 2>&1
#Do it
      else
        mv "${oldname}" "${newname:-${oldname}}"
        echo "File ${oldname} converted to file ${newname:-${oldname}}"
      fi
  done
    ;;
esac



With this script it doesn't seem to convert filenames with spaces in them.

Kyle's script doesn't protect against overwriting lowercase versions of the file if one exists already.

But both are pretty cool scripts especially when not having a mastery of shell scripting to create one!


All times are GMT -5. The time now is 07:19 PM.