Linux Move/Rename file

C

captain747480

Guest
I need to be able to strip the _DateStamp from a filename, use the resulting filename to create a directory and then move the file to the created directory without the Date Stamp in the filename.

So 'blah_120709.rec' moves to a file 'blah.rec' in directory 'blah'.

I've done some searching and found lots of posts about awk and sed but I'm afraid they're over my head.

Could someone help me with a Linux script that will do this.

Thanks in advance
 


I need to be able to strip the _DateStamp from a filename, use the resulting filename to create a directory and then move the file to the created directory without the Date Stamp in the filename.

So 'blah_120709.rec' moves to a file 'blah.rec' in directory 'blah'.

I've done some searching and found lots of posts about awk and sed but I'm afraid they're over my head.

Could someone help me with a Linux script that will do this.

Thanks in advance

try this:

for i
do
base_name=`echo $i | sed -n 's/\(.*\)_[0-9]*\.rec/\1/p'`
if [ -n "${base_name}" ]
then
mkdir ${base_name}
mv $i ${base_name}/${base_name}.rec
else
echo "$i is bad"
fi
done



from within a directory containing the files:
script.sh *.rec
 

Members online


Latest posts

Top