SHELL脚本修改文件名移动到指定文件夹

来源:百度知道 编辑:UC知道 时间:2024/05/17 10:41:29
现在我TEST文件夹下有
ralph-config.xml.bb
ralph-log.cfg.bb
ralph-info.xml.bb
bessie-config.xml.bb
bessie-log.cfg.bb
bessie-info.xml.bb
现在想写一个SHELL脚本把ralph-config.xml.bb、ralph-log.cfg.bb、ralph-info.xml.bb移动到home/test/ralph文件夹下,并且把前缀ralph-去掉。
bessie-config.xml.bb、bessie-log.cfg.bb、bessie-info.xml.bb移动到
home/test/bessie文件夹下,并且把前缀bessie-去掉。
哪位高手能帮帮解决下,谢谢了
谢谢大家这么快的回答
少了一个条件,还要把 ".bb"去掉,再在SHELL中加什么语句

#! /bin/bash
# need bash 3.1 0r above
path=TEST
newpath_1=/home/test/ralph
newpath_2=/home/test/bessie
if [ ! -d "$newpath_1 ] && [ ! -d "$newpath_2" ]
then
mkdir "$newpath_1" "$newpath_2"
fi
cd "$path" && echo "We are in $(pwd)"
for i in *
do
if [[ $i =~ (ralph-)([a-z]+.*) ]]
then
name=${BASH_REMATCH[2]}
mv "$i" "$name" ; ret=$?
if (( $ret == 0 )) ; then
mv "$name" "$newpath_1"
fi
else
mv "$i" "$newpath_2"
fi
done
exit 0

简单的写了一次, 思路如下,
脚本需要放在 test文件夹下
主要是用变量赋值分割文件名, 按照-的前后来分割

#################################
#!/bin/bash
mkdir 'ralph'
mkdir 'bessie'

for name in