LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-07-2007, 07:53 AM   #1
PaxRomana
LQ Newbie
 
Registered: May 2007
Location: SE London, UK
Distribution: Ubuntu with Xfce
Posts: 9

Rep: Reputation: 0
Need script for changing DIRECTORY names only to upper case


EDIT: Good start - I meant to say in the title 'change to LOWER case' not upper duhh.

Hello all

I have just installed Ubuntu and am moving a lot of files from my Windows machine.

Now, as I have many, many directories with upper case characters in them, I'd like to change them so they are only lower case. However, I only want the DIRECTORIES changed this way, NOT the filenames (got my reasons).

I've made a script (or I got one off the net I should say ), here:

Code:
#!/bin/bash
ls | while read file
do
new=`echo $file | tr [:upper:] [:lower:]`
mv -v "$file" "$new"
done
This is fine, but does both filenames and directories. Now, if I do ls -F I get the directory list output with directory names appended with a '/' . So perhaps I could check the name string and only echo those that end with a '/'. Except, of course, I have no clue how to do this, and need showing (I've looked, but no joy) .

Or perhaps my whole approach is wrong and it can be done a different way?

Thx for replies

Last edited by PaxRomana; 05-07-2007 at 07:56 AM.
 
Old 05-07-2007, 08:21 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
#!/bin/bash
for i in *
do
[ -f "$i" ] && mv -v "$file" "$(echo $file | tr [:upper:] [:lower:])"
done


should do it.
 
Old 05-07-2007, 11:32 AM   #3
cfaj
Member
 
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mint, Mandriva
Posts: 221

Rep: Reputation: 31
Quote:
Originally Posted by PaxRomana
EDIT: Good start - I meant to say in the title 'change to LOWER case' not upper duhh.

I have just installed Ubuntu and am moving a lot of files from my Windows machine.

Now, as I have many, many directories with upper case characters in them, I'd like to change them so they are only lower case. However, I only want the DIRECTORIES changed this way, NOT the filenames (got my reasons).

I've made a script (or I got one off the net I should say ), here:

Code:
#!/bin/bash
ls | while read file
do
new=`echo $file | tr [:upper:] [:lower:]`
mv -v "$file" "$new"
done
This is fine, but does both filenames and directories. Now, if I do ls -F I get the directory list output with directory names appended with a '/' . So perhaps I could check the name string and only echo those that end with a '/'. Except, of course, I have no clue how to do this, and need showing (I've looked, but no joy) .

Or perhaps my whole approach is wrong and it can be done a different way?

Code:
for dir in */
do
  new=`printf "%s\n" "${dir%/}" | tr [:upper:] [:lower:]`
  mv -v "$dir" "$new"
done
You can speed it up with a dynamically loadable builtin command (lcase: external commands, e.g., tr, are slow). See http://www.unixreview.com/documents/...6a/ur0606a.htm for details. Then you can use:

Code:
for dir in */
do
  lcase -p new ${file%/}"
  mv -v "$file" "$new"
done
 
Old 05-07-2007, 02:00 PM   #4
PaxRomana
LQ Newbie
 
Registered: May 2007
Location: SE London, UK
Distribution: Ubuntu with Xfce
Posts: 9

Original Poster
Rep: Reputation: 0
acid_kewpie's code didn't work - much appreciated tho m8, as I just spent a couple of hours analysing what you did and finding out more for myself and got it done .

cfaj - maybe tomorrow if I get time I'll see about working yours in just for the learning experience . Thx folks.

Here's mine:

Code:
#!/bin/bash

echo

for i in *; do

if [ -d "$i" ]
then
	echo "$i is a directory"
	new=`echo $i | tr [:upper:] [:lower:]`
	if [[ "$i" == "$new" ]]
	then		
		echo "...but is already lower case, so will not be changed"
		echo
	else
		echo "...changing to lower case."
		mv -v "$i" "$new"
		echo
	fi
else
	echo "$i is not a directory"
	echo
fi
done
 
Old 05-07-2007, 02:16 PM   #5
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
oh yeah, i didn't replace all the "$file"'s with "$i"'s. my code is *much* simpler though... there's no point using intermediate variables like you are, just a waste of space.
 
Old 05-07-2007, 08:02 PM   #6
PaxRomana
LQ Newbie
 
Registered: May 2007
Location: SE London, UK
Distribution: Ubuntu with Xfce
Posts: 9

Original Poster
Rep: Reputation: 0
Your code still doesn't work dude if I replace the "file"s with "i"'s . Changes filenames as well as directories .
 
Old 05-07-2007, 08:14 PM   #7
PaxRomana
LQ Newbie
 
Registered: May 2007
Location: SE London, UK
Distribution: Ubuntu with Xfce
Posts: 9

Original Poster
Rep: Reputation: 0
Oh I see it it's 'cos it has "-f" instead of "-d" . I must be learning something .
 
Old 05-07-2007, 08:18 PM   #8
cfaj
Member
 
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mint, Mandriva
Posts: 221

Rep: Reputation: 31
Quote:
Originally Posted by PaxRomana
Your code still doesn't work dude if I replace the "file"s with "i"'s . Changes filenames as well as directories .

Code:
for i in */
do
  mv -v "$i" "$(printf "%s\n" "$i" | tr [:upper:] [:lower:])"
done
 
Old 05-07-2007, 08:21 PM   #9
cfaj
Member
 
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mint, Mandriva
Posts: 221

Rep: Reputation: 31
Quote:
Originally Posted by acid_kewpie
oh yeah, i didn't replace all the "$file"'s with "$i"'s. my code is *much* simpler though... there's no point using intermediate variables like you are, just a waste of space.

Intermediate variables serve various useful purposes. They can:
  • make code clearer
  • encapsulate a task
  • make lines shorter
  • make modifications easier

 
Old 05-07-2007, 11:19 PM   #10
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
#!/bin/bash
ls -l | awk '/^d/ && $9 ~ /[[:upper:]]/{
		new = tolower($9)
		cmd = "mv " "\047" $9 "\047" " " "\047" new "\047"	
		system(cmd)	
}'
 
Old 05-08-2007, 01:42 AM   #11
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
Quote:
Originally Posted by cfaj

Intermediate variables serve various useful purposes. They can:
  • make code clearer
  • encapsulate a task
  • make lines shorter
  • make modifications easier

yes, but not in such a trviial case as this.
 
Old 05-08-2007, 01:42 AM   #12
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
Quote:
Originally Posted by ghostdog74
Code:
#!/bin/bash
ls -l | awk '/^d/ && $9 ~ /[[:upper:]]/{
		new = tolower($9)
		cmd = "mv " "\047" $9 "\047" " " "\047" new "\047"	
		system(cmd)	
}'
mr Sledgehammer, meet mr Nut.
 
Old 05-08-2007, 04:10 AM   #13
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
if you only want directories, using
the shell wildcard glob */ is what you want (in bash and ksh anyhow)
eg:

ls -d */
 
Old 05-08-2007, 04:33 AM   #14
PaxRomana
LQ Newbie
 
Registered: May 2007
Location: SE London, UK
Distribution: Ubuntu with Xfce
Posts: 9

Original Poster
Rep: Reputation: 0
Hehe code wars . Guess it's always like this around here eh? . All good for educating myself though, even 'Mr Sledgehammer' . FWIW, I wanted to put the error checking in my version to help me see what was going on and for practice (not written any code in about 5 years).

One final question - can somebody give me an example of a negative IF conditional line? In C you'd just use "!=". Doesn't seem to work here and I can't find a single example of it (every tutorial I've found just does an '=='). Thx again folks .
 
Old 05-08-2007, 04:51 AM   #15
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
in shell scripting
!= == > < are for string comparison

-eq -lt -ne

etc. are for numerical expressions.


man bash or ksh
 
  


Reply



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
Weird, file names changing case after restore... s2cuts Linux - General 2 03-03-2007 01:39 PM
forcing lower case directory names with mkisofs Earl Parker II Linux - Software 2 02-12-2007 03:49 PM
Mount DVD While Preserve Upper Case File Names xptools Linux - Software 2 12-12-2005 10:18 AM
Why are all my upper case files being shown as lower case?? [Kernel 2.6.9-1.667 FC3] t3gah Fedora 4 03-11-2005 04:09 PM
chaging directory names to lower case andy753421 Linux - General 2 05-11-2004 02:12 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:15 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
Open Source Consulting | Domain Registration