Linux系统之dd命令详解
一、dd命令介绍
1.dd介绍
Linux dd 命令用于读取、转换并输出数据。
2.dd 可从标准输入或文件中读取数据,根据指定的格式来转换数据,再输出到文件、设备或标准输出。
2.dd的相关解释
if 代表输入文件。如果不指定 if,默认就会从 stdin 中读取输入。of 代表输出文件。如果不指定 of,默认就会将 stdout 作为默认输出。bs 代表字节为单位的块大小。count 代表被复制的块数。/dev/zero 是一个字符设备,会不断返回 0 值字节(\0)。
二、创建指定大小的文件
[root@192 ~]# dd if=/dev/zero of=/tmp/1G.txt bs=1M count=10241024+0 records in1024+0 records out1073741824 bytes (1.1 GB) copied, 31.19 s, 34.4 MB/s[root@192 ~]# ll -h /tmp/total 1.0G-rw-r--r-- 1 root root 1.0G May 31 21:51 1G.txtdrwx------ 3 root root 17 May 23 08:29 systemd-private-83fcd0fe514147ac8635da5768da9361-bolt.service-taaMXPdrwx------ 3 root root 17 May 23 08:29 systemd-private-83fcd0fe514147ac8635da5768da9361-colord.service-dOXoKcdrwx------ 3 root root 17 May 23 08:29 systemd-private-83fcd0fe514147ac8635da5768da9361-cups.service-Dchwxxdrwx------ 3 root root 17 May 23 08:29 systemd-private-83fcd0fe514147ac8635da5768da9361-mariadb.service-DEfrgodrwx------ 3 root root 17 May 23 08:29 systemd-private-83fcd0fe514147ac8635da5768da9361-rtkit-daemon.service-1HtQQqdrwx------ 2 root root 6 May 26 14:39 tmp.1KlZmVfyQOdrwx------ 2 root root 6 May 23 00:18 tracker-extract-files.0drwx------. 2 root root 6 May 19 15:50 vmware-root_8524-970702477drwx------ 2 root root 6 May 20 02:32 vmware-root_8529-1990534344drwx------ 2 root root 6 May 23 08:29 vmware-root_8536-995146008
三、给磁盘的分区做备份
1.磁盘分区备份
dd if=/dev/sda1 of=/root/sda1.bak
2.磁盘分区还原
dd if=/root/sda1.bak of=/dev/sda1
四、清空磁盘
[root@192 ~]# dd if=/dev/zero of=/dev/sdb dd: writing to ‘/dev/sdb’: No space left on device41943041+0 records in41943040+0 records out21474836480 bytes (21 GB) copied, 172.229 s, 125 MB/s[root@192 ~]# fdisk -lDisk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk label type: dosDisk identifier: 0x0009c6fd Device Boot Start End Blocks Id System/dev/sda1 * 2048 2099199 1048576 83 Linux/dev/sda2 2099200 33556479 15728640 83 Linux/dev/sda3 33556480 35653631 1048576 83 LinuxDisk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytes
五、给磁盘备份
1.备份磁盘
[root@192 ~]# dd if=/dev/sda of=/dev/sdb41943040+0 records in41943040+0 records out21474836480 bytes (21 GB) copied, 249.87 s, 85.9 MB/s[root@192 ~]# fdisk -lDisk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk label type: dosDisk identifier: 0x0009c6fd Device Boot Start End Blocks Id System/dev/sda1 * 2048 2099199 1048576 83 Linux/dev/sda2 2099200 33556479 15728640 83 Linux/dev/sda3 33556480 35653631 1048576 83 LinuxDisk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk label type: dosDisk identifier: 0x0009c6fd Device Boot Start End Blocks Id System/dev/sdb1 * 2048 2099199 1048576 83 Linux/dev/sdb2 2099200 33556479 15728640 83 Linux/dev/sdb3 33556480 35653631 1048576 83 Linux
2.还原磁盘
dd if=/dev/sdb of=/dev/sda
六、备份分区表
1.备份分区表
dd if=/dev/sda of=/root/mbr.bak bs=512 count=1
2.还原分区表
d if=/root/mbr.bak of=/dev/sda
七、为虚拟机创建一个空磁盘文件
1.制作空磁盘文件
[root@192 ~]# dd if=/dev/zero of=aabb.img bs=1M count=256256+0 records in256+0 records out268435456 bytes (268 MB) copied, 0.132996 s, 2.0 GB/s[root@192 ~]# ll -htotal 257M-rw-r--r-- 1 root root 256M Jun 1 06:54 aabb.img-rw-------. 1 root root 1.8K Jan 31 08:15 anaconda-ks.cfgdrwxr-xr-x. 2 root root 6 Feb 1 12:06 Desktop-rw-r--r-- 1 root root 0 Jun 1 06:53 disk01.imgdrwxr-xr-x. 2 root root 6 Feb 1 12:06 Documentsdrwxr-xr-x. 2 root root 6 Feb 1 12:06 Downloads-rw-r--r--. 1 root root 1.8K Feb 1 11:50 initial-setup-ks.cfgdrwxr-xr-x. 2 root root 6 Feb 1 12:06 Musicdrwxr-xr-x. 2 root root 6 Feb 1 12:06 Picturesdrwxr-xr-x. 2 root root 6 Feb 1 12:06 Publicdrwxr-xr-x. 2 root root 6 Feb 1 12:06 Templatesdrwxr-xr-x. 2 root root 6 Feb 1 12:06 Videos
2.制作qcow2格式磁盘文件
qemu-img create -f qcow2 -o preallocation=metadata server1.qcow2 1G
八、在Linux下制作启动盘
dd if=boot.img of=/dev/fd0 bs=1440k
九、转换文件所有字母为大写
[root@192 data]# dd if=test1 of=test2 conv=ucasedd: failed to open ‘test1’: No such file or directory[root@192 data]# dd if=test1.txt of=test2.txt conv=ucase0+1 records in0+1 records out102 bytes (102 B) copied, 0.000140887 s, 724 kB/s[root@192 data]# cat test2.txt HELLO LINUX! LINUX IS A FREE UNIX-TYPE OPTERATING SYSTEM. THIS IS A LINUX TESTFILE! LINUX TEST
来源地址:https://blog.csdn.net/jks212454/article/details/125074067