博客首页 注册 建议与交流 排行榜 加入友情链接
推荐 投诉 搜索: 帮助

剑心通明的资料库

文章均为转载,本人不负因参考它所导致的一切后果,请谨慎参考!如您的文章不愿被转载,请点击此处联系本人!
  jxtm.cublog.cn

关于作者
姓名:剑心通明
职业:高级工程师(专修灵魂^_^)
年龄:20出头30不到
位置:网络上一节点
个性介绍:努力学习每一天!
倾心打造:http://www.bsdlover.cn
http://bbs.bsdlover.cn
BSD爱好者的乐园!
|| << >> ||
我的分类


将一个大文件分割成N个小文件
#!/bin/sh

do_help()
{
    echo "syntax:"
    echo "split <orig_file> <split_num> <dest_path>"
}

orig_file=$1
split_num=${2:-2}
dest_path=${3:-.}

if ! ( [ -f "$orig_file" ] &&
[ -r "$orig_file" ] && [ -s "$orig_file" ] ); then
    echo "file $orig_file is invalid."
    do_help
    exit 1
fi

if ! [ $split_num -gt 1 ]; then
    echo "split_num is invalid."
    do_help
    exit 1
fi

if ! ( [ -d $dest_path ] && [ -x $dest_path ] );then
    echo "dest_path is invalid."
    do_help
    exit 1
fi

size=$(ls -l $orig_file | awk '{print $5}')
split_size_avr=$(($size/$split_num))
bs=1

if [ $size -gt $((1024*1024)) ]; then
    bs=1024
    split_size_avr=$(($split_size_avr/1024))
fi

for i in $(seq $split_num);do
    if [ $i -eq 1 ]; then
        dd if=$orig_file \
of=$dest_path/${orig_file}.$i bs=$bs count=$split_size_avr
    elif [ $i -eq  $split_num ];then
        dd if=$orig_file \
of=$dest_path/${orig_file}.$i bs=$bs \
skip=$(($split_size_avr*$(($i-1)))) \
count=$(( $size - $(($split_size_avr*$(($i-1)))) ))
    else
        dd if=$orig_file \
of=$dest_path/${orig_file}.$i bs=$bs \
skip=$(($split_size_avr*$(($i-1)))) \
count=$split_size_avr

    fi
done

命令格式:
./split filename N dest_path

合并N个小文件为一个大文件:

代码:
cat $(ls |grep -E 'filename\.') > filename

 原文地址 http://www.bsdlover.cn/html/36/n-836.html
发表于: 2008-05-03,修改于: 2008-05-03 11:04,已浏览376次,有评论0条 推荐 投诉


网友评论
 发表评论