LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 08-29-2003, 05:50 AM   #1
jamie_barrow
Member
 
Registered: Jul 2003
Location: Gauteng, South Africa
Distribution: Windows XP, yup :D Will be back onto Ubuntu probably someday
Posts: 107

Rep: Reputation: 15
Batch Script to rename files...


Does anybody have a batch script to rename all the files in a directory to lowercase
 
Old 08-29-2003, 05:51 AM   #2
jamie_barrow
Member
 
Registered: Jul 2003
Location: Gauteng, South Africa
Distribution: Windows XP, yup :D Will be back onto Ubuntu probably someday
Posts: 107

Original Poster
Rep: Reputation: 15
I mean uppercase but please give me both, thanks!
 
Old 08-29-2003, 06:20 AM   #3
Bebo
Member
 
Registered: Jul 2003
Location: Göteborg
Distribution: Arch Linux (current)
Posts: 553

Rep: Reputation: 30
Code:
#!/bin/bash

dir=/whatever/directory

for file in `ls $dir` ; do
   # ANYCASE TO UPPERCASE:
   newname=`echo $file | tr '[a-z]' '[A-Z]'`

   mv $dir/$file $dir/$newname
done
To transform from anycase to lowercase, just switch '[a-z]' '[A-Z]' to '[A-Z]' '[a-z]'.

To test the script, exchange mv to cp instead, just to be on the safe side.



Last edited by Bebo; 08-29-2003 at 06:23 AM.
 
Old 09-09-2003, 08:30 PM   #4
Jalalabee
Member
 
Registered: Aug 2003
Distribution: Slackware 10.2
Posts: 102

Rep: Reputation: 15
how do i run a script like this?

i know theres probly obvious places to find it..but ive been unsuccessful...

thanks
 
Old 09-09-2003, 09:36 PM   #5
contrasutra
Guru
 
Registered: Mar 2003
Location: New Jersey
Distribution: Arch Linux
Posts: 1,445

Rep: Reputation: 46
Stick that text in any regular text file.

Then make the textfile executable by doing:

chmod +x foo.sh

then run it like:

./foo.sh
 
Old 09-12-2003, 06:30 PM   #6
afe_coda
LQ Newbie
 
Registered: Sep 2003
Distribution: MDK 9.1
Posts: 7

Rep: Reputation: 0
I found this script useful, but is there a variant that I could use to make it recurse subdirectories?

I could do it in Windows easily enough, with the /b /s options on dir, but I can't seem to find an equivalent to /b for ls.

Last edited by afe_coda; 09-12-2003 at 06:35 PM.
 
Old 09-12-2003, 07:44 PM   #7
J_Szucs
Senior Member
 
Registered: Nov 2001
Location: Budapest, Hungary
Distribution: SuSE 6.4-11.3, Dsl linux, FreeBSD 4.3-6.2, Mandrake 8.2, Redhat, UHU, Debian Etch
Posts: 1,126

Rep: Reputation: 58
Just a slight modification to rename all files recursively, but it worth nothing: there may be uppercase characters in the directory names, too, which should also be changed, so the problem needs a different approach.

As for Windows: it is really great in listing files (that is why so many people use it).
But what about renaming those files? Your mouse will burn away.
 
Old 09-13-2003, 05:17 AM   #8
afe_coda
LQ Newbie
 
Registered: Sep 2003
Distribution: MDK 9.1
Posts: 7

Rep: Reputation: 0
Bah, I don't use Windows Explorer for file handling; it's far too inconvenient (and slow) for mass operations. I stick to the command prompt.

And what IS this slight modification to the script?
 
Old 09-13-2003, 07:47 AM   #9
J_Szucs
Senior Member
 
Registered: Nov 2001
Location: Budapest, Hungary
Distribution: SuSE 6.4-11.3, Dsl linux, FreeBSD 4.3-6.2, Mandrake 8.2, Redhat, UHU, Debian Etch
Posts: 1,126

Rep: Reputation: 58
Here is the same script being slightly modified to be recursive:

#!/bin/bash
dir=/whatever/directory
for file in `find $dir` ; do
# ANYCASE TO UPPERCASE:
newshortname=`echo $file | sed "s/$dir//" | tr '[a-z]' '[A-Z]'`
mv $file $dir/$newshortname
done
Please note that the script in its present form has serious restrictions: it only works correctly if there are no uppercase letters in any directory names, and there are no spaces in either the directory or filenames. Otherwise it will fail or give unexpected result.
So, use it with special caution.

As I told you I found it difficult to overcome the restrictions with a simple script - though I am not a script guru. However it is easy to test whether the directory and filenames comply with the restrictions.
For this reason, you might insert these lines into the beginning of the script right after the 'dir=/whatever/directory' line:
# Test for uppercase in directory names
if [ `find $dir -type d | grep -c -E "([[:upper:]])"` -gt 0 ] ; then
echo "Directory names contain uppercase:"
find $dir -type d | grep -E "([[:upper:]])"
exit
fi
if [ `find $dir | grep -c -E "([ ])"` -gt 0 ] ; then
echo "Directory or file names contain spaces:"
find $dir | grep -E "([ ])"
exit
fi

Now, I am really curious how would you do that with the great command prompt of windows?
Please let me know!

Last edited by J_Szucs; 09-13-2003 at 09:06 AM.
 
Old 09-13-2003, 10:23 AM   #10
afe_coda
LQ Newbie
 
Registered: Sep 2003
Distribution: MDK 9.1
Posts: 7

Rep: Reputation: 0
Well, in Windows it's relatively unnecessary because it's case-insensitive on filenames. Anyway, this script requires you to have sed installed, but that's a simple matter.

dir /s /b /l > files.txt
sed "s/(*.?)\n/ren \1 \1\n/" < files.txt > fix.bat
call fix.bat
del fix.bat
del files.txt

Actually I haven't tested this script, but the only thing I'm not sure about is the regular expression syntax for sed.

Edit: And actually, none of those shell scripts listed above work either... just gives me file-already-exists errors.

Last edited by afe_coda; 09-13-2003 at 10:51 AM.
 
Old 09-13-2003, 11:46 AM   #11
J_Szucs
Senior Member
 
Registered: Nov 2001
Location: Budapest, Hungary
Distribution: SuSE 6.4-11.3, Dsl linux, FreeBSD 4.3-6.2, Mandrake 8.2, Redhat, UHU, Debian Etch
Posts: 1,126

Rep: Reputation: 58
Sorry...This happens when you do not test the script you write...
It was because of sed: since full filenames contain '/', sed cannot use / as delimiter for the s command (now it uses '!' as the delimiter).
I corrected the bug, and also made little additions to avoid unnecessary attempts to rename files that do not contain uppercase, and $dir itself.

This time I tested the script, and it worked fine. Here it is:

dir=/some/directory
# Test for uppercase in directory names
if [ `find $dir -type d | grep -c -E "([[:upper:]])"` -gt 0 ] ; then
echo "Directory names contain uppercase:"
find $dir -type d | grep -E "([[:upper:]])"
exit
fi
# Test for spaces or sed delimiter in directory or filenames
if [ `find $dir | grep -c -E "([ !])"` -gt 0 ] ; then
echo "Directory or file names contain spaces or delimiter used for sed:"
find $dir | grep -E "([ !])"
exit
fi
for file in `find $dir` ; do
# ANYCASE TO UPPERCASE:
newshortname=`echo $file | sed "s!$dir/!!" | tr '[A-Z]' '[a-z]'`
if [ $file != $dir/$newshortname ] && [ $file != $dir ] ; then
mv $file $dir/$newshortname
fi
done

Last edited by J_Szucs; 09-13-2003 at 03:40 PM.
 
Old 09-13-2003, 03:28 PM   #12
J_Szucs
Senior Member
 
Registered: Nov 2001
Location: Budapest, Hungary
Distribution: SuSE 6.4-11.3, Dsl linux, FreeBSD 4.3-6.2, Mandrake 8.2, Redhat, UHU, Debian Etch
Posts: 1,126

Rep: Reputation: 58
I think that the windows sample script would only work on windows, because it seems to rely much on the case insensitivity of winDOS.

The problem is: on unix, the order of renaming the files and (sub)directories is not arbitrary.
Since you create the recursive directory listing at the beginning of the script, it might happen that you first rename a directory (since it had an uppercase), only then you try to rename a file in it. Naturally this would not succeed, since the file is in other directory by that time.

However, I found out how could a simple unix script do this.
In two turns:
First: find only the files (find $dir -type f) and rename them.
Second: find only the directories (find $dir -type d), order (sort) them according to their level in the directory hierarchy (sub-directories first!) (maybe grep or awk could count '/' characters in the full directory names and order the directories based on this count), then rename the directories in this order.
Since I am not a script guru, however, it would take me several hours of reading and experimentation to write this script.
So, either you will be lucky to get help from someone who is more skillful than me or you do the trick on your own.

Bye

Last edited by J_Szucs; 09-13-2003 at 03:30 PM.
 
Old 09-13-2003, 03:39 PM   #13
afe_coda
LQ Newbie
 
Registered: Sep 2003
Distribution: MDK 9.1
Posts: 7

Rep: Reputation: 0
I SAID that it was designed for Windows and it worked by being case-insensitive. I wasn't expecting it to be a viable solution for *nix.
 
Old 09-13-2003, 04:24 PM   #14
J_Szucs
Senior Member
 
Registered: Nov 2001
Location: Budapest, Hungary
Distribution: SuSE 6.4-11.3, Dsl linux, FreeBSD 4.3-6.2, Mandrake 8.2, Redhat, UHU, Debian Etch
Posts: 1,126

Rep: Reputation: 58
Quote:
I SAID that it was designed for Windows and it worked by being case-insensitive.
Great. If you said that, then we agree. Bye.
 
Old 09-13-2003, 11:20 PM   #15
afe_coda
LQ Newbie
 
Registered: Sep 2003
Distribution: MDK 9.1
Posts: 7

Rep: Reputation: 0
Anyway, thanks for the script. It got a lot of stuff cleaned up for me.
 
  


Reply

Tags
linux, rename


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
Help with Bash Script - Rename Multiple Files embsupafly Programming 16 04-02-2010 03:50 AM
rename script all files in dir Longinus Linux - Newbie 8 08-02-2004 12:24 AM
Batch rename question hellblade Linux - Software 4 05-03-2004 03:57 PM
Need script to rename files joe_stevensen Programming 5 12-05-2003 06:12 PM
Got a script to rename a batch of files? jamie_barrow Linux - General 1 08-08-2003 06:52 AM


All times are GMT -5. The time now is 08:10 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration