dd作用
dd作用是用指定大小的块拷贝一个文件,并在拷贝的同时进行指定的转换。
dd的由来
由来(全称):本来应根据其功能描述“Convert an copy”命名为“cc”,但“cc”已经被用以代表“CComplier”,所以命名为“dd”。
用法:dd [操作数]或:dd 选项
bs=比特数 一次读写的比特数
cbs=比特数 一次转换的比特数
conv=CONVS 依照每个逗号分割的标志列表转换文件
count=块数 只将指定个块数复制到块
ibs=比特数 一次读取的比特数(默认:512)
if=文件 从指定文件中读取
iflag=符号 按照以逗号分隔的符号列表指定的方式读取
obs=比特数 一次写入指定比特数(默认:512)
of=文件 写入到指定文件
oflag=符号 按照以逗号分隔的符号列表指定的方式写入
seek=块数 在输出开始处跳过指定的块数
skip=块数 在输入开始处跳过指定的块数
status=noxfer 禁止传输统计
块和字节数后可能带有以下的一个或多个后缀:
c =1, w =2, b =512, kB =1000, K =1024, MB =1000*1000, M =1024*1024, xM =M
GB =1000*1000*1000, G =1024*1024*1024, and so on for T, P, E, Z, Y.
每个 CONV 符号可能为:
ascii 由EBCDIC 码转换至ASCII 码
ebcdic 由ASCII 码转换至EBCDIC 码
ibm 由ASCII 码转换至替换的EBCDIC 码
block 将结束字符块里的换行替换成等长的空格
unblock 将cbs 大小的块中尾部的空格替换为一个换行符
lcase 将大写字符转换为小写
ucase 将小写字符转换为大写
swab 交换每一对输入数据字节
sync 将每个输入数据块以NUL 字符填满至ibs 的大小;当配合block
或unblock 时,会以空格代替NUL 字符填充
excl fail if the output file already exists
nocreat do not create the output file
notrunc 不截断输出文件
noerror 读取数据发生错误后仍然继续
fdatasync 结束前将输出文件数据写入磁盘
fsync 类似上面,但是元数据也一同写入
FLAG 符号可以是:
append 追加模式(仅对输出有意义;隐含了conv=notrunc)
direct 使用直接I/O 存取模式
directory 除非是目录,否则 directory 失败
dsync 使用同步I/O 存取模式
sync 与上者类似,但同时也对元数据生效
fullblock 为输入积累完整块(仅iflag)
nonblock 使用无阻塞I/O 存取模式
noatime 不更新存取时间
nocache 丢弃缓存数据
noctty 不根据文件指派控制终端
nofollow 不跟随链接文件
解读:
1. bs ibs obs cbs
bs: 一次 读写 = ibs+obs
ibs: 一次读
obs: 一次写
cbs: 一次转换
2. if of
if 是读取的文件
of 是输出的文件
3. seek skip
skip 在从if中读取时 跳过多少比特
seek 在写入of时, 跳过多少比特
使用实例
1、将本地的/dev/hdb整盘备份到/dev/hdd
1 |
|
2、将/dev/hdb全盘数据备份到指定路径的image文件
1 |
|
3、备份/dev/hdb全盘数据,并利用gzip工具进行压缩,保存到指定路径
1 |
|
4、把一个文件拆分为3个文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
5、把拆分出的文件合并为1个?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
6、在输出文件中指定的位置插入数据,而不截断输出文件
需要使用conv=notrunc
参数
1 |
|
————————————————————————————————————————————————————————————————————————————————————————————————————————————————
1. 批量生成随机名称的测试文件
1 2 3 4 |
|
2. 备份和恢复
备份
1 2 3 |
|
恢复方法如下:
1 2 3 |
|
3. 转换大小写
生成大写:dd if=dd.txt of=my.log bs=1M count=1 conv=ucase
生成小写:为了不懒惰,留给读者试试
4. 拷贝自己
复制代码 代码如下:
file_subscript=copy
dd if=$0 of=$0.$file_subscript 2>/dev/null
5.要把一张软盘的内容拷贝到另一张软盘上,利用/tmp作为临时存储区。把源盘插入驱动器中,输入下述命令:
$ dd if =/dev/fd0 of = /tmp/tmpfile
拷贝完成后,将源盘从驱动器中取出,把目标盘插入,输入命令:
$ dd if = /tmp/tmpfile of =/dev/fd0
软盘拷贝完成后,应该将临时文件删除:
$ rm /tmp/tmpfile
6. 把net.i这个文件写入软盘中,并设定读/写缓冲区的数目。
(注意:软盘中的内容会被完全覆盖掉)
$ dd if = net.i of = /dev/fd0 bs = 16384
7.将文件sfile拷贝到文件 dfile中。
$ dd if=sfile of=dfile
8.创建一个100M的空文件
dd if=/dev/zero of=hello.txt bs=100M count=1
python实现:
def creatfilesize(n):
local_time = time.strftime("%Y%m%d%H%M%S",time.localtime())
file_name = "E:\\testFile\\"+str(local_time)+".txt"
bigFile= open(file_name, 'w')
bigFile.seek(1024*1024*n)
bigFile.write('test')
bigFile.close()
if __name__ == '__main__':
n = input("输入你要生成的文件大小(单位为M):")
creatfilesize(n)
————————————————————————————————————————————————————————————————————————————————————————————————————————————————
linux的一些基本常识
1. shell脚本以.sh 为扩展名,通常运行 ./${filename}.sh 或者 sh ${filename}.sh
2. shell 脚本开头以 #!/bin/bash #!读作 “shebang”
3. 开启调试 #!/bin/bash –xv
4. /dev/null 任何东西丢进去都会消失,linux黑洞。 /dev/zero 用于初始化,会产生0
/dev/null------它是空设备,也称为位桶(bit bucket)。任何写入它的输出都会被抛弃。如果不想让消息以标准输出显示或写入文件,那么可以将消息重定向到位桶。
/dev/zero------该设备无穷尽地提供0,可以使用任何你需要的数目——设备提供的要多的多。他可以用于向设备或文件写入字符串0。