Linux下二进制文件的分割与合并

dd的作用是转换和拷贝文件,我们可以利用它来分割文件,相关的选项如下:
if=filename:输入的文件名
of=finename:输出的文件名
bs=bytes:一次读写的字节数,默认是512bytes
skip=blocks:拷贝前,跳过的输入文件的前blocks块,块的大小有bs决定
count=blocks:只拷贝输入文件的前blocks块
例如,现在有一个文件file,大小为116616字节:
[root]# du -b file 
116616 file 
将其分割为两文件file1和file2,那我们就设置每块为1024字节,将file的前60块放入file1,余下的放入file2:
[root]# dd if=file bs=1024 count=60 skip=0 of=file1 
[root]# dd if=file bs=1024 count=60 skip=60 of=file2 
然后用cat将两个文件合并为file.bak,要注意文件的顺序:
[root]# cat file1 file2 > file.bak 
可以用md5sum验证一下file和file.bak:
[root]# md5sum file 
3ff53f7c30421ace632eefff36148a70 file 
[root]# md5sum file.bak 
3ff53f7c30421ace632eefff36148a70 file.bak 
可以证明两个文件时完全相同的。
为了方便分割、合并文件,我写了两个脚本:
ddf.sh
#ddf.sh:分割文件,分割后的文件以数字结尾,例如file分割为两个文件:file1和file2 
#!/bin/sh 
#使用脚本是第一参数是要分割的文件名 
Filename=$1 
Filesize=0 
Path=`pwd` 
#验证文件名是否正确,然后计算文件的大小

if [ -z $Filename ];then 
echo "Error:The file name can not be empty" 
exit 
fi 
if [ -e $Filename ];then 
Filesize=`du -b $Filename | awk '{print $1}'` 
if [ $Filesize == 0 ];then 
echo "Error:The File size is zero!" 
exit 
fi 
echo "The file size is $Filesize Byte" 
echo "Plese enter the subfile size(KB):" 
else 
echo "Error:$Filename does not exist!" 
exit 
fi 
#输入分割后每个文件的大小,单位是KB 
read Subfilesize
if [ -z $Subfilesize ];then
echo "Error:Input can not be empty"
exit
fi 
echo $Subfilesize | grep '^[0-9]\+$' >> /dev/null
if [ $? -ne 0 ];then
echo "Error:The Input is not a number!"
exit
elif [ $Subfilesize -eq 0 ];then
echo "Error:The Subfile size is zero!"
exit
fi 
#计算需要分割为几个文件 
SubfileByte=`expr $Subfilesize \* 1024`
Subfilenum=`expr $Filesize / $SubfileByte`
if [ `expr $Filesize % $Subfilesize` -ne 0 ];then
Subfilenum=`expr $Subfilenum + 1`
fi

#将文件分割 
echo "$Filename will be divided into $Subfilenum" 
i=1
skipnum=0
while [ $i -le $Subfilenum ]
do
echo "$Filename$i"
dd if=$Filename of="$Path/$Filename$i" bs=1024 count=$Subfilesize skip=$skipnum
i=`expr $i + 1`
skipnum=`expr $skipnum + $Subfilesize`
done
echo "$Filename has been divided into $Subfilenum"
echo "Done !" 
caf.sh
#caf.sh:合并文件,需要合并的文件要放在一个文件夹里 
# 文件名分为两个部分,第一部分都相同,第二部分必须是从1开始的连续数字,例如file1,file2,file3 
# 合并后的文件名为file.bak 
#!/bin/sh 
#输入文件名的第一部分 
echo "Please enter file name:" 
read Filename

if [ -z $Filename ];then 
echo "Error:The file name can not be empty" 
exit 
fi 
#输入待合并文件的个数 
echo "Please enter the number of subfiles:" 
read Subfilenum
if [ -z $Subfilenum ];then
echo "Error:The number of subfiles can not be empty"
exit
fi
echo $Subfilenum | grep '^[0-9]\+$' > /dev/null
if [ $? -ne 0 ];then
echo "Error:Input must be a number"
exit
fi
if [ $Subfilenum -eq 0 ];then
echo "Error:The number of subfiles can not be zero"
exit
fi 
#合并文件 
i=1
Newfile=$Filename\.bak
while [ $i -le $Subfilenum ]
do
Subfilename=$Filename$i
if [ -e $Subfilename ];then
echo "$Subfilename done!"
cat $Subfilename >> $Newfile
i=`expr $i + 1`
else
echo "Error:$Subfilename does not exist"
rm -rf $Newfile
exit
fi
done
echo "Subfiles be merged into $Newfile"
echo "Success!" 
用这两个脚本完成对file的分割、合并:
[root]# ./ddf.sh file 
The file size is 116616 Byte 
Plese enter the subfile size(KB): 
60 
file will be divided into 2 
file1 
记录了60+0 的读入 
记录了60+0 的写出 
61440字节(61 kB)已复制,0.0352612 秒,1.7 MB/秒 
file2 
记录了53+1 的读入 
记录了53+1 的写出 
55176字节(55 kB)已复制,0.0316272 秒,1.7 MB/秒 
file has been divided into 2 
Done ! 
[root]# ls 
caf.sh ddf.sh file file1 file2
[root]# ./caf.sh 
Please enter file name:
file
Please enter the number of subfiles:

file1 done!
file2 done!
Subfiles be merged into file.bak
Success!
[root]# ls 
caf.sh ddf.sh file file1 file2 file.bak

 

image.php?url=0KPpUNohfT

转载于:https://my.oschina.net/u/3635497/blog/2451351

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Linux中,可以使用xxd命令来查看二进制文件。xxd命令是用于将文件转换成十六进制或反向操作的工具。通过xxd命令,我们可以查看二进制文件的内容和结构。 另外,还可以使用hexdump命令来查看二进制文件。hexdump是一个十六进制转储工具,可以将文件以十六进制和ASCII字符的形式显示出来,从而更好地理解二进制文件的内容。 如果你想要在Linux中生成和读取二进制文件,可以使用C或C++编程语言。通过在程序中使用文件操作函数,如fopen、fread、fwrite等,可以实现对二进制文件的读取和写入操作。例如,你可以使用main2.cpp来生成一个二进制文件,然后使用main3.c来读取该二进制文件。在读取二进制文件时,可以使用fgets函数来读取指定长度的字符串,用于读取格式化固定位址的二进制结构数据。 综上所述,在Linux中,可以使用xxd命令或hexdump命令来查看二进制文件的内容和结构。如果需要生成和读取二进制文件,则可以使用C或C++编程语言来实现。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Linux下查看二进制文件](https://blog.csdn.net/qq_19922839/article/details/115483499)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [linux c 读写二进制文件](https://download.csdn.net/download/sunflow/85483138)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值