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

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

script for changing file names

Reply to Message Icon

Name: Samer
Date: February 22, 2005 at 09:13:37 Pacific
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



Sponsored Link
Ads by Google

Response Number 1
Name: David Perry
Date: February 22, 2005 at 12:41:06 Pacific
Reply:

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?


0

Response Number 2
Name: Samer
Date: February 22, 2005 at 13:16:37 Pacific
Reply:

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


0

Response Number 3
Name: vibhor_agarwalin
Date: February 22, 2005 at 20:32:25 Pacific
Reply:

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


0

Response Number 4
Name: David Perry
Date: February 23, 2005 at 09:51:33 Pacific
Reply:

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

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


0

Response Number 5
Name: bon
Date: February 24, 2005 at 12:56:00 Pacific
Reply:

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


0

Related Posts

See More



Response Number 6
Name: bon
Date: February 24, 2005 at 12:58:56 Pacific
Reply:

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



0

Sponsored Link
Ads by Google
Reply to Message Icon

Testing ENV variable coping folder contents



Post Locked

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


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: script for changing file names

script to change file names www.computing.net/answers/unix/script-to-change-file-names/8327.html

Utility or script for renaming files on www.computing.net/answers/unix/utility-or-script-for-renaming-files-on-/2538.html

Shell script for Text file parsing www.computing.net/answers/unix/shell-script-for-text-file-parsing/4388.html