2、可以先缺则按_分割取第二段
echo $file|awk -F\_ '{print $2}'
3、然后再取第4位后面拦扮仿的8位
4、简纤例如:vi a.sh
#!/bin/sh
for file in `ls *.jpg`do
echo $file
datedir=$(echo $file|awk -F\_ '{print $2}'|awk '{print substr($1,4,8)}')
mkdir $datedir
/bin/mv $file $datedir
done
polly@nowthen:~/test$ cat mv.sh#!/bin/bash
usage() {
echo "`basename $0` filetomove targetlocation"
exit 1
}
if [ $# -ne 2 ] then
usage
else if [ -e $1 -a -f $1 -o -e $1 -a -d $1 ] then
echo "$1 exist"
temp=`basename $1`
echo "file to mv: ${temp}"
脊棚if [ -e ${temp} ] then
while [ -e ${temp} ]
do
echo 铅野码"${temp} exists in current dir"
sleep 1
done
槐哪echo "${temp} removed, and copy begin"
cp -r $1 $2
exit 0
else
cp -r $1 $2
fi
fi
fi
程序没有检查目标文件夹是否存在,可以移动文件及文件夹
#!/bin/bashrp=$(pwd)
echo rootpath-----------------:${rp}
# function
moveFile(){
# get dirs
ls -F | grep "/$" > temp_dirs.txt
cat temp_dirs.txt | while read d
#for d in $(cat temp_dirs.txt)
#while read d
do
cp=$(pwd)/${d}
cd ${cp}
echo currentPath----------------------:${cp}
ls -al | grep "^-" > temp_files.txt
cat temp_files.txt | while read f
#for f in $(cat temp_files.txt)
#while read f
do
fileName="${f##* }"
echo filename--------------------:${fileName}
newFileName=${rp}/${d%*/}-${fileName}
echo newfilename---------------------:${newFileName}
fullPath=${cp}${fileName}
echo fullPath-------------------------:${fullPath}
mv ${fullPath} ${newFileName}
done
#< <带扮差(ls -al | grep "^-")
# delete temp file
test -a temp_files.txt && rm -rf temp_files.txt
#
moveFile
test -a temp_dirs.txt && rm -rf temp_dirs.txt
cd ..
done
# < <(ls -F | grep "/$")
}
# exec
echo 'start move files ...'
moveFile
rm -rf *temp_dirs.txt
rm -rf *temp_files.txt
echo 'move files end'
技术有限,此代码效率不高蠢皮;在linux测试没问题,mac上没测,你可以先测一下;文件移到根目录会被重命名为它原先所在的文件夹加上短杠加上它原先的名缺帆称
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)