Renaming multiple files, specifically changing their case


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Renaming multiple files, specifically changing their case
# 1  
Old 01-24-2011
Question Renaming multiple files, specifically changing their case

Hey all,

I'm running a BASH shell and I was wondering if anyone knows a code to rename all the files in a directory. All I need to do is change the first character in the file from uppercase to lowercase and some of the files are already lowercase so they don't need to change.

-Thanks
# 2  
Old 01-24-2011
Code:
 $ ruby -e 'Dir["*"].each{|x| File.rename(x,  x[0].downcase+x[1..-1]) if x[/^[A-Z]/]   }'

This User Gave Thanks to kurumi For This Post:
# 3  
Old 01-24-2011
Using less exotic languages (*poke*) :
Code:
for file in * ; do
   newname="$(echo $file | cut -c 1 | tr 'a-z' 'A-Z')${file#?}"
   /bin/mv "$file" "$newname"
done

The cut extracts the first character from $file, and the tr transforms it into uppercase. The ${file#?} leaves behind all but the first character in the filename. Double-quotes are used in case the filename has funny characters or spaces.
This User Gave Thanks to otheus For This Post:
# 4  
Old 01-25-2011
since you are using some bash syntax, why not do the "cutting" using bash ?
This User Gave Thanks to kurumi For This Post:
# 5  
Old 01-25-2011
My script doesn't nicely handle the case where the the file already has the proper name. In those cases, mv will complain about target and source being the same file. To prevent that, simply change the for:
Code:
for file in [a-z]* ;do

As far as a "pure" bash solution, I don't think one exists without writing a big case-Waac to handle lower - upper case conversion. Luckily, cut and tr are standard tools on every system. But it's pretty cool to see how ruby is apt for more than just Web Development.
This User Gave Thanks to otheus For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Renaming multiple files in sftp server in a get files script

Hi, In sftp script to get files, I have to rename all the files which I am picking. Rename command does not work here. Is there any way to do this? I am using #!/bin/ksh For eg: sftp user@host <<EOF cd /path get *.txt rename *.txt *.txt.done ... (7 Replies)
Discussion started by: jhilmil
7 Replies

2. UNIX for Dummies Questions & Answers

Renaming Multiple files

Hello, I have multiple files that I want to change the names to. Let's say for example that I want to rename all the files in the left column to the names in the right column: What would be the easiest way to go about doing this? Thanks. (1 Reply)
Discussion started by: Scatterbrain26
1 Replies

3. Shell Programming and Scripting

Renaming multiple files

I have 34 file in a directory that all have different names, however, they do have 1 pattern in commmon. They all have "-10-11-2010" date format in the name. I want to replace the date in the file name with a supplied date or maybe even the system date. I am sure I will be using awk or sed to... (9 Replies)
Discussion started by: Harleyrci
9 Replies

4. UNIX for Dummies Questions & Answers

Renaming files by changing date order

I'm looking for a simple solution to rename a batch of files. All of the files in this directory start with a date in the format mm.dd.yy followed by a space and then additional descriptive text. Example: 01.21.10 742 P.xlsx 02.24.09 730 Smith.xlsx The information following the date can... (3 Replies)
Discussion started by: kreisel
3 Replies

5. UNIX for Dummies Questions & Answers

Renaming multiple files

Hi, Can we rename multiples files using find or awk utility? Now I am doing it using for loop and getting the file name and in side the loop using the mv command. Like ine need t rename all txt files to doc file. For example a1.txt => a1.doc a2.txt => a2.doc a3.txt => a3.doc myfile.txt... (2 Replies)
Discussion started by: siba.s.nayak
2 Replies

6. Shell Programming and Scripting

Renaming multiple files

Hi, I have several hundred files I need to rename, and I'm would rather not hit F2 for each file individually to rename them. Example of file: large1961.jpg What I need the file to be renamed as: 1961.jpg I don't know what type of command I can execute within a shell script that would... (7 Replies)
Discussion started by: jayell
7 Replies

7. Shell Programming and Scripting

Renaming multiple files

I have a bunch of files txt1.csv--2008 thru to txt3.csv--2008. If i wanted to rename these files all at the same time to txt*.csv-2008 what would be the best way to do it... Just need to get rid of the extra - in each file name.. not all files are going to be called txt*.csv--2008. Just... (6 Replies)
Discussion started by: Jazmania
6 Replies

8. Shell Programming and Scripting

Renaming multiple files

hi, I've a machine running RHEL3,kernel version 2.4. i need to rename multiple files under one directory as follows: ls demo.c demo.S demo-1243.sw demo.xyz and now i need to replace the occurrence of demo with demo_1 for each of the above file. the tedious way is to go ahead and do mv on... (2 Replies)
Discussion started by: amit4g
2 Replies

9. UNIX for Dummies Questions & Answers

Renaming multiple files

Help! I was trying to rename multiple files. Like in DOS, i decided to use wildcards and now i am missing some files. Any ideas on how to recover them? Or find out where the files went? I had these 3 files resume1.log elecresume.log compresume.log The command I ran was mv *.log *.log.bak... (6 Replies)
Discussion started by: rmayur
6 Replies

10. UNIX for Dummies Questions & Answers

renaming multiple files

Hi to everyone!!. Here's my stupid question of the day. When I have to rename a file I use "mv filename newfilename". But what about renaming multiple files, for example if I want to add the prefix "old" to several image files (in fact it's what I wanted to do..). Thanks in advance.... :D (6 Replies)
Discussion started by: piltrafa
6 Replies
Login or Register to Ask a Question