LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 12-06-2003, 08:57 PM   #1
AMMullan
Member
 
Registered: Sep 2003
Location: United Kingdom
Distribution: Ubuntu, Arch
Posts: 438

Rep: Reputation: 30
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....
 
Old 12-06-2003, 09:27 PM   #2
fr0zen
Member
 
Registered: Nov 2003
Location: 127.0.0.1
Distribution: xubuntu
Posts: 217

Rep: Reputation: 30
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.
 
Old 12-06-2003, 09:32 PM   #3
AMMullan
Member
 
Registered: Sep 2003
Location: United Kingdom
Distribution: Ubuntu, Arch
Posts: 438

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

 
Old 12-06-2003, 09:36 PM   #4
fr0zen
Member
 
Registered: Nov 2003
Location: 127.0.0.1
Distribution: xubuntu
Posts: 217

Rep: Reputation: 30
Oh. So, folders receive the case conversion as well as the files. I figured that. Not sure what you mean by spacing though.
 
Old 12-06-2003, 09:39 PM   #5
AMMullan
Member
 
Registered: Sep 2003
Location: United Kingdom
Distribution: Ubuntu, Arch
Posts: 438

Original Poster
Rep: Reputation: 30
Basically if there's a file called "I'm back" I need "i'm back"
 
Old 12-06-2003, 11:03 PM   #6
fr0zen
Member
 
Registered: Nov 2003
Location: 127.0.0.1
Distribution: xubuntu
Posts: 217

Rep: Reputation: 30
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
 
Old 10-18-2005, 07:32 PM   #7
elfoozo
Member
 
Registered: Feb 2004
Distribution: Debian Jessie
Posts: 259

Rep: Reputation: 32
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!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
UPPERCASE to lowercase command? pk21 Linux - General 4 06-12-2007 10:26 AM
samba replaces Uppercase char in filenames to lowercase srikz Other *NIX 3 03-03-2006 08:59 AM
samba replaces Uppercase char in filenames to lowercase srikz Red Hat 1 10-26-2004 03:47 AM
External harddrive FAT32- VFAT options (uppercase to lowercase file name translation) pauquetb Linux - General 1 03-20-2004 12:52 PM
Converting lowercase to uppercase noodle123 Programming 1 05-17-2002 02:12 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 09:04 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration