shell readdir


原文链接: shell readdir

截取文件扩展名

$ fullfile=/the/path/mylib.1.0.1a.zip
$ fullname="${fullfile##/}"
$ dir="${fullfile%/
}"
$ extension="${fullname##.}"
$ filename="${fullname%.
}"
$ echo $dir , $fullname , $filename , $extension
/the/path , mylib.1.0.1a.zip , mylib.1.0.1a , zip

#!/bin/bash

src="./Private"
#源文件的父目录

dst="./timeLine3"
#目标目录

function read_dir(){
    for file in `ls $1`;do   #step1 遍历文件
        echo "判断 " $file "文件类型中..."
        if [ -d $1"/"$file ];then
	    echo $file "是目录,处理目录中的文件"
            read_dir $1"/"$file $2
        elif [ -f $1"/"$file ];then #step2 复制文件到目标目录
           echo "复制" $file "到" $2
            [[ `cp -f -v -p $1"/"$file $2` ]]
        else echo ".."
        fi
    done
}

read_dir $src $dst
#递归查找src中的文件,复制到dst中 (暂不能处理重名文件)
echo "复制完毕"

oss.sh

#!/bin/bash
# 接收参数
bucket=videos-buket
dest=$(date +%Y%m%d)
src=$(date +%Y%m%d)
ext=mp4
type=video

while getopts ":b:d:e:s:t:" optname
do
  case "$optname" in
    b) bucket=$OPTARG ;;
    d) dest=$OPTARG ;;
    e) ext=$OPTARG ;;
    s) src=$OPTARG ;;
    t) type=$OPTARG ;;
    *) echo "usage: $0 [-b] [-d] [-e] [-t]" ;;
  esac
done

# 遍历文件
for file in $(ls "$src" | grep "\.$ext") ; do
  # 计算文件名
  name="$(echo -n "${file//.$ext/}" | md5sum | cut -d " " -f1)"

  # 计算文件大小
  size="$(wc -c "$src/$file" | sed "s/^ *//" | sed "s/\([0-9]*\) .*/\1/")"

  # 上传文件
  echo "src_name = $file, size = $size, "dest_name = "$dest"/v2_"$name"_"$type"_"$ext" >> "$src"/result.log
  ossutil cp -u -f "$src/$file" oss://"$bucket"/"$dest"/v2_"$name"_"$type"_"$ext"
done
`