Computing.Net > Forums > Unix > script for changing file names

script for changing file names

Reply to Message Icon

Original Message
Name: Samer
Date: February 22, 2005 at 09:13:37 Pacific
Subject: script for changing file names
OS: Sun Solaris
CPU/Ram: Blade
Comment:

Hello,

I need a script that will change the names of all files in a folder by removing the last "X" number of characters, like


stuff_blah
other_things_blah

to

stuff
other_things

Any help would be greatly appreciated.
Thanks,
Samer


Report Offensive Message For Removal


Response Number 1
Name: David Perry
Date: February 22, 2005 at 12:41:06 Pacific
Subject: script for changing file names
Reply: (edit)

What have you done so far? Will the script be required to shorten the name 5 characters or merely everything from the "_" to the end?


Report Offensive Follow Up For Removal

Response Number 2
Name: Samer
Date: February 22, 2005 at 13:16:37 Pacific
Subject: script for changing file names
Reply: (edit)

Hey David,

All I have so far are results from a google search, which can change .jpg files to .JPG, however, I need to truncate the names. i.e. if the people that made those scripts had cut the .jpg and added .JPG I would be able to find my solution, however, they seemed to have used a change to upper command. I just basically need a truncate by 3 characters command, or 4 or whatever. Its is the same number for all the files.

By the way, here is the script I found:

#!/bin/sh

img_dir=$1

if [ -d $img_dir ]

then cd $img_dir
for image in *
do
lc_image=`echo $image | awk '{print toupper($0)}'`
mv ./$image ./$lc_image
done

else echo " $1 not a dir"
fi


Report Offensive Follow Up For Removal

Response Number 3
Name: vibhor_agarwalin
Date: February 22, 2005 at 20:32:25 Pacific
Subject: script for changing file names
Reply: (edit)

Try something like:

for filename in `find . -name *blah*`;
do
new_name=`echo $filename | sed -e "s/blah//g" $filename`
mv $filename $new_name
done

You can add the "if" prefixes if you like.

Vibhor Kumar Agarwal


Report Offensive Follow Up For Removal

Response Number 4
Name: David Perry
Date: February 23, 2005 at 09:51:33 Pacific
Subject: script for changing file names
Reply: (edit)

mv $filename `echo $filename | awk '{ print substr( $0, 0, length($0)-4)}'`

feel free to wrap this in a for .. find loop.


Report Offensive Follow Up For Removal

Response Number 5
Name: bon
Date: February 24, 2005 at 12:56:00 Pacific
Subject: script for changing file names
Reply: (edit)

hey sameer try this one out ....

for old_file_name in *.blah
do
new_file_name=${$old_file_name%.shd}
mv $old_file_name $new_file_name
done
Best of Luck!!


Report Offensive Follow Up For Removal


Response Number 6
Name: bon
Date: February 24, 2005 at 12:58:56 Pacific
Subject: script for changing file names
Reply: (edit)

hey sammer i m sorry i made a mistake in the last post. It goes like this ...
for old_file_name in *.blah
do
new_file_name=${$old_file_name%.blah}
mv $old_file_name $new_file_name
done
Best of Luck!!



Report Offensive Follow Up For Removal






Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: script for changing file names

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




How often do you use Computing.Net?

Every Day
Once a Week
Once a Month
This Is My First Time!


View Results

Poll Finishes In 4 Days.
Discuss in The Lounge