Visit Our UNIX and Linux User Community


Help with multiple file rename - change case of part of file name


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Help with multiple file rename - change case of part of file name
# 1  
Old 06-28-2005
Question Help with multiple file rename - change case of part of file name

Hi there,

I hope someone can help me with this problem :

I have a directory (/var/www/file/imgprofil) which contains about 10000 JPG files. They have a naming convention thus :

prefix-date-key-suffix.jpg

they all have the prefix p-20050608-
then AAAA is a 4 letter code
the suffix is either -g / -v / -m or no suffix
the file extension is always .jpg

sample :

p-20050608-AFWC-g.jpg
p-20050608-AFWC-m.jpg
p-20050608-AFWC-v.jpg
p-20050608-AFWC.jpg
p-20050608-AFWD-g.jpg
p-20050608-AFWD-m.jpg
p-20050608-AFWD-v.jpg
p-20050608-AFWD.jpg


My problem is that some of the files have 1 or more lower case letters in the 4 letter code, so they are like this :

p-20050608-Ajyd-g.jpg
p-20050608-Ajyd-m.jpg
p-20050608-Ajyd-v.jpg
p-20050608-Ajyd.jpg
p-20050608-Ajyw-g.jpg
p-20050608-Ajyw-m.jpg
p-20050608-Ajyw-v.jpg
p-20050608-Ajyw.jpg


Now, Unix being the case sensitive angel that it is Smilie I need to transform these lowercase "keys" filenames into uppercase, so the sample would be :

p-20050608-AJYD-g.jpg
p-20050608-AJYD-m.jpg
p-20050608-AJYD-v.jpg
p-20050608-AJYD.jpg
p-20050608-AJYW-g.jpg
p-20050608-AJYW-m.jpg
p-20050608-AJYW-v.jpg
p-20050608-AJYW.jpg


While of course keeping the lowercase prefix (p-) and suffix (-g /-m / -v) and the lowercase file extension (.jpg)

I hope this is clear and that someone can help with this,

Thanks,

Steve
# 2  
Old 06-28-2005
Use awk

for old_name in `ls -l path_to_dir/*jpg`
do
new_name=`awk -F"-" 'BEGIN { OFS="-"} {$3=toupper($3);print $0}' $old_name`
mv $old_name $new_name
done

Last edited by rahul123_libra; 06-28-2005 at 10:37 AM..
# 3  
Old 06-29-2005
Something using sed:

for filename in *.jpg;
do
echo $filename | sed -e 's/Aiyd/AIYD/g'
done
# 4  
Old 06-29-2005
THis is for 1 particular case

Quote:
Originally Posted by vibhor_agarwali
Something using sed:

for filename in *.jpg;
do
echo $filename | sed -e 's/Aiyd/AIYD/g'
done

This is for 1 particular case ( aiyd ) and not for all the options

Isnt it ?
# 5  
Old 06-29-2005
Apart from that, how will you change the name of the file with a sed. It requires a mv very specifically.

-vino
# 6  
Old 06-30-2005
Perfectly Agree,

But i could accomplish only that much with my knowledge of sed.
Can anyone give a general one with sed.

mv is taken for granted.

for filename in *.jpg;
do
newname=`echo $filename | sed -e 's/Aiyd/AIYD/g'`
mv $filename newname
done
# 7  
Old 06-30-2005
Quote:
Originally Posted by rahul123_libra
for old_name in `ls -l path_to_dir/*jpg`
do
new_name=`awk -F"-" 'BEGIN { OFS="-"} {$3=toupper($3);print $0}' $old_name`
mv $old_name $new_name
done
This has some errors in that awk line. Firstly, it's attempting to process the JPG file itself rather than the file name.
Also, using $3 sometimes matches more than the 4 letter code (where there is no suffix).

Perhaps try:
Code:
for i in *.jpg
do
  eval $(echo $i|awk '{printf "mv %s %s\n", $0,\
    substr($0,1,11) toupper(substr($0,12,4)) substr($0,16)}')
done

 

Previous Thread | Next Thread
Test Your Knowledge in Computers #222
Difficulty: Easy
NetMarketShare reported that the combined versions of Microsoft Windows lead the worldwide market with close to a 93% market share at the end of August 2011, followed by OS X at 6%, and Linux with 1%.
True or False?

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to rename last part of file ?

Hi, I have large number of files like below - UNIX FY17 D21-1c Active user audit - NPP dir owner listing(vctmstt01)_072816 - Notepad.pdf UNIX FY17 D21-1c Active user audit - TTMS dir owner listing(pvcdmot35)_072816 - Notepad.pdf UNIX FY17 D21-1c Active user audit - PCP dir owner... (3 Replies)
Discussion started by: solaris_1977
3 Replies

2. Shell Programming and Scripting

Find and rename part of a file

hi, Need your help. I need to write a script for below.. i have two files in directory /home/abc as below: Watch_20140203_abc.dat Watchnow_20140203_abc.dat I have to copy this file from /home/abc to /home01/home02 after that i have to rename the date part in above two files... (1 Reply)
Discussion started by: Vivekit82
1 Replies

3. UNIX for Dummies Questions & Answers

[Solved] Rename file name / remove part of name

I have a whole file structure with jpeg files where I want to remove a part of the file name. An application added in many files a case conflict in the naming "xyz 017.jpg (Case Conflict 1)" So, can someone help me how to get rid of the " (Case Conflict 1)"? What I have is this: find . -name... (2 Replies)
Discussion started by: borobudur
2 Replies

4. Shell Programming and Scripting

Multiple file rename (change in filename in unix with single command

Dear All, Please help ! i ham having 300 file with E.G. PMC1_4567.arc in seq. like PMC1_4568.arc,PMC1_4569.arc ...n and so on.. i want all those file to be rename like PMC_4567.arc ,PMC_4568.arc .. mean i want to remove 1 from first file name .. pls help.. (6 Replies)
Discussion started by: moon_22
6 Replies

5. UNIX for Dummies Questions & Answers

Shell script to rename or change file extension case.

I searched the forum, but there was different type of rename. Hello. I have files in folder. Like: xxxxxxxx1.html or xxxxxxxx2.txt or xxxxxxxx3.tar.gz and how to rename or change file extension case to xxxxxxxx1.htm or xxxxxxx2.TXT or (5 Replies)
Discussion started by: Sheldon
5 Replies

6. Shell Programming and Scripting

rename file by removing some part of the file name

I am special requirements to rename file. I have files with names like below: 1_firstname1_lastname1.html 2_firstname2_lastname2.html 3_fistname3_lastname2.html I would like these file to be renamed as below firstname1_lastname1.html firstname2_lastname2.html... (5 Replies)
Discussion started by: McLan
5 Replies

7. Shell Programming and Scripting

Multiple rename part of scripts

i have about 30 scripts for example: test, test1,test2, test3, test4,..... inside every scripte is code like this: echo "input check OK" how to rename this line in multiple scripts in this: echo "input error!" (2 Replies)
Discussion started by: waso
2 Replies

8. Shell Programming and Scripting

Rename multiple file from file listing

I am new at Linux/UNIX programming. Here is my problem. I had one big file which I split using the command csplit -k -s -f april.split. april '/^ISA/' '{10000}' So now I have multiple files with names april.split.01 april.split.02 april.split.03 But I need the name of the file like... (5 Replies)
Discussion started by: yshahiac
5 Replies

9. Shell Programming and Scripting

Rename part of multiple files

Hello, I have about 200 files named like script1234_test.sh script3434_test.sh and so on... I am trying to write a script that will rename those files from test to dev, keeping the rest of the file name intact. Could anyone help? Thanks. (4 Replies)
Discussion started by: sajjad02
4 Replies

Featured Tech Videos