Hi
I need to reanme the multiple file using unix script
I have multiple file
like:
201005_SAMPLE_20110512.xls
201005_NAME_20110512.xls
201005_TEST_20110512.xls
201005_FIRST_LAST_20110512.xls
I need to rename this file
SAMPLE.xls
NAME.xls
TEST.xls
FIRSTLAST.xls
can you guide me the script.
thanks
#!/usr/bin/ksh # or bash or dash or ...
for f in [0-9]*.xls
do
f2=$( echo "$f" | tr -d "[0-9]_" )
[ -f "$f2" ] && echo "$f2 exist" && continue
[ -d "$f2" ] && echo "$f2 dir exist" && continue
mv "$f" "$f2"
done
Hi kshji,
I have tested the above script
I have one problem
Suppose i have recived file like
201005_FIRST_LAST_20110512.xls
I need to populate target like
FIRST_LAST.xls
The above scripting is failing this file.
can you please updat me the script
My script suggestion:
#! /bin/bash
# clean.sh
for file in *.xls; do
filename=${file%.*}
file_clean=`echo $filename | tr -d "[:digit:]_" `
final="$file_clean.xls"
mv "$file" "$final"
echo $final
done
Hi kshji,
sorry for the confusion
I have tested the above script
I have one problem
Suppose i have recived file like
201005_FIRST_LAST_20110512.xls
I need to populate target like
FIRST_LAST.xls
The above scripting is failing this file.
can you please updat me the script
So after 1st defination you found mistake in your defination. Here is solution for new defination.
#!/usr/bin/ksh # or bash or dash or ...
for f in [0-9]*.xls
do
notfirst=${f#*_}
notlast=${notfirst%_*}
f2=$notlast.xls
[ -f "$f2 ] && echo "$f2 exist" && continue
[ -d "$f2 ] && echo "$f2 dir exist" && continue
echo "mv $f => $f2"
mv "$f" "$f2"
done
notfirst=${f#*_} notlast=${notfirst%_*}
What the special characters (i.e #, *,%,_) exactly do in the above expressions ??