关于我们
技术分享
技术分享
linux的dd命令:文件复制与备份、快速生成大文件、大小写转换
linux的dd命令:文件复制与备份、快速生成大文件、大小写转换
2020-08-04
dd的意思是covert and copy a file,也就是转换和复制一个文件,它除了用来备份之外,还可以转换文件。这里介绍几种最常见的用法,包括单个文件的复制、单个文件系统的复制、快速生成一个大文件、大小写转换
NAME dd - convert and copy a file SYNOPSIS dd [OPERAND]... dd OPTION DESCRIPTION Copy a file, converting and formatting according to the operands. bs=BYTES read and write up to BYTES bytes at a time conv=CONVS convert the file as per the comma separated symbol list count=N copy only N input blocks ibs=BYTES read up to BYTES bytes at a time (default: 512) if=FILE read from FILE instead of stdin of=FILE write to FILE instead of stdout seek=N skip N obs-sized blocks at start of output
示例1:备份单个文件,if指定输入文件,of指定输出文件
root@db2a:/tmp# dd if=dir2.tar.gz of=dir2.tar.gz.bak
37266+1 records in
37266+1 records out
19080487 bytes (19 MB) copied, 0.284904 s, 67.0 MB/s
示例2:备份整个文件系统:
lv1的文件系统是ext4的,挂载点是/tmp/lv1
root@db2a:/tmp# mount /dev/mqsvg1/lv1 /tmp/lv1
root@db2a:/tmp# ls -l /tmp/lv1/
total 15
drwxr-xr-x 2 root root 1024 Aug 21 19:11 dir1
-rw-r--r-- 1 root root 10 Aug 21 19:10 file1
-rw-r--r-- 1 root root 10 Aug 21 19:10 file2
drwx------ 2 root root 12288 Aug 21 19:06 lost+found
现在新建一个lv2,然后使用dd if=/dev/mqsvg1/lv1 of=/dev/mqsvg1/lv2命令把lv1的内容复制到lv2上:
root@db2a:/tmp# lvcreate -L 600M -n lv2 mqsvg1
root@db2a:/tmp# dd if=/dev/mqsvg1/lv1 of=/dev/mqsvg1/lv2
1024000+0 records in
1024000+0 records out
524288000 bytes (524 MB) copied, 106.313 s, 4.9 MB/s
root@db2a:/tmp# mkdir /tmp/lv2
root@db2a:/tmp# mount /dev/mqsvg1/lv2 /tmp/lv2
root@db2a:/tmp# ls -l /tmp/lv2
total 15
drwxr-xr-x 2 root root 1024 Aug 21 19:11 dir1
-rw-r--r-- 1 root root 10 Aug 21 19:10 file1
-rw-r--r-- 1 root root 10 Aug 21 19:10 file2
drwx------ 2 root root 12288 Aug 21 19:06 lost+found
root@db2a:/tmp# df -hT lv*
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/mqsvg1-lv1 ext4 477M 2.3M 445M 1% /tmp/lv1
/dev/mapper/mqsvg1-lv2 ext4 477M 2.3M 445M 1% /tmp/lv2
注意一点:这里甚至都没有格式化lv2。除了备份分区外,还可以备份整个磁盘。
示例3:快速生成一个大文件,这里的bs表示字节数,默认为512 bytes,也就是一个磁盘block的大小,count表示数量,下面的两个例子都是生成500MB的大文件:
root@db2a:/tmp# dd if=/dev/zero of=bigfile count=1024000
1024000+0 records in
1024000+0 records out
524288000 bytes (524 MB) copied, 1.75448 s, 299 MB/s
root@db2a:/tmp# dd if=/dev/zero of=bigfile2 bs=1M count=500
500+0 records in
500+0 records out
524288000 bytes (524 MB) copied, 0.610042 s, 859 MB/s
root@db2a:/tmp# ls -lh bigfile*
-rw-r--r-- 1 root root 500M Aug 21 19:47 bigfile
-rw-r--r-- 1 root root 500M Aug 21 19:48 bigfile2
这里文件是实际写入到硬盘了,写的数据全是0,需要耗费很多时间,如果不想真正写入,可以使用seek跳过前n个block。这样子系统会认为有一个大文件,但文件不占空间
root@db2a:/tmp# dd if=/dev/zero of=bigfile3 bs=1M count=0 seek=500
0+0 records in
0+0 records out
0 bytes (0 B) copied, 0.000919414 s, 0.0 kB/s
root@db2a:/tmp# ls -lh bigfile3
-rw-r--r-- 1 root root 500M Aug 21 20:10 bigfile3
可以看到,瞬间完成。这种文件生成后,使用df和du查看文件系统和目录和使用时,均没有增加,但find的-size选项可以搜索到
示例4:文本大小写转换
使用conv=lcase转换为小写,使用conv=ucase转换为大写:
root@db2a:/tmp# cat file1.txt
Hello, World.
This is a TEXT with both CAPITALs and lowercases.
root@db2a:/tmp# dd if=file1.txt of=fileL.txt conv=lcase
root@db2a:/tmp# dd if=file1.txt of=fileU.txt conv=ucase
root@db2a:/tmp# cat fileL.txt
hello, world.
this is a text with both capitals and lowercases.
root@db2a:/tmp# cat fileU.txt
HELLO, WORLD.
THIS IS A TEXT WITH BOTH CAPITALS AND LOWERCASES.

- 标签:
-
其他
您可能感兴趣的新闻 换一批
热门文章
现在下载,可享30天免费试用