Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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_blahto
stuff
other_thingsAny help would be greatly appreciated.
Thanks,
Samer

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?

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
doneelse echo " $1 not a dir"
fi

Try something like:
for filename in `find . -name *blah*`;
do
new_name=`echo $filename | sed -e "s/blah//g" $filename`
mv $filename $new_name
doneYou can add the "if" prefixes if you like.
Vibhor Kumar Agarwal

mv $filename `echo $filename | awk '{ print substr( $0, 0, length($0)-4)}'`
feel free to wrap this in a for .. find loop.

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!!

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!!

![]() |
Testing ENV variable
|
coping folder contents
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |