专注各种脚本编程
Baidu
加入收藏夹
本站内容有下面分类知识,欢迎您的到来^_^
shell相关:指令篇 基础篇 脚本欣赏 编程实例 shell问问 shell视频教程 技巧篇 水平测试 E文资料 vi编辑器 高级Bash脚本编程指南
其他:mysql perl c语言 oracle
当前位置:| 主页>shell脚本欣赏>

将一个大文件分割成N个小文件

百度收藏 QQ搜藏

代码:

#!/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

上一篇:一个丢硬币的脚本 下一篇:天气预报(限国内城市)

power by soyo123 2007-2008