用Shell命令批量压缩指定目录下指定目录到目标目录中

来源:互联网 发布:手机淘宝评价怎么删 编辑:程序博客网 时间:2024/05/16 14:29
#!/bin/bash
function usage(){
    echo ""
    echo "introduction: tar of source dir start with pattern files or dirs to target dir"
    echo "patch_tar_pattern: usage: bash patch_tar_pattern -s [source dir] -t [target dir] -p [pattern]"
    exit 1
}
while getopts "s:t:p:" arg #选项后面的冒号表示该选项需要参数
do
    case $arg in
         s)
            source="$OPTARG" #参数存在$OPTARG中
            ;;
         t)
            target="$OPTARG"
            ;;
         p)
            pattern="$OPTARG"
            ;;
         ?)  #当有不认识的选项的时候arg为?
            echo "unkonw argument"
            usage
            ;;
    esac
done
if [ -z $source ]
then
    usage
fi
if [ -z $target ]
then
    usage
fi
if [ -z $pattern ]
then
    usage
fi


if test ! -d $source
then
    echo "$source not exists!"
    exit 1
fi


if test ! -d $target
then
    echo "$target not exists!"
    echo "mkdir -p -m 777 $target"
    mkdir -p -m 777 $target || exit 1
fi


cd $source


dir_list=`ls | grep "^$pattern"`
for dir in $dir_list
do
if test -d "$source/$dir"
then
#tar -zcvf "$target/${dir}.tar.gz" "$dir"
echo "tar"
if test -f "$target/${dir}.tar.gz"
then
echo "tar -zcvf $target/${dir}.tar.gz   $dir"
echo "$source/$dir" >> "$target/tar_success.log"
else
echo "$source/$dir" >> "$target/tar_error.log"
fi
fi
done
0 0
原创粉丝点击