Linux系统之dd命令详解

一、dd命令介绍
1.dd介绍
1.Linux dd 命令用于读取、转换并输出数据。
2.dd 可从标准输入或文件中读取数据,根据指定的格式来转换数据,再输出到文件、设备或标准输出。

2.dd的相关解释
if 代表输入文件。如果不指定 if,默认就会从 stdin 中读取输入。
of 代表输出文件。如果不指定 of,默认就会将 stdout 作为默认输出。
bs 代表字节为单位的块大小。
count 代表被复制的块数。
/dev/zero 是一个字符设备,会不断返回 0 值字节(\0)。
1
二、创建指定大小的文件
[root@192 ~]# dd if=/dev/zero of=/tmp/1G.txt bs=1M count=1024
1024+0 records in
1024+0 records out
1073741824 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.txt
drwx------  3 root root   17 May 23 08:29 systemd-private-83fcd0fe514147ac8635da5768da9361-bolt.service-taaMXP
drwx------  3 root root   17 May 23 08:29 systemd-private-83fcd0fe514147ac8635da5768da9361-colord.service-dOXoKc
drwx------  3 root root   17 May 23 08:29 systemd-private-83fcd0fe514147ac8635da5768da9361-cups.service-Dchwxx
drwx------  3 root root   17 May 23 08:29 systemd-private-83fcd0fe514147ac8635da5768da9361-mariadb.service-DEfrgo
drwx------  3 root root   17 May 23 08:29 systemd-private-83fcd0fe514147ac8635da5768da9361-rtkit-daemon.service-1HtQQq
drwx------  2 root root    6 May 26 14:39 tmp.1KlZmVfyQO
drwx------  2 root root    6 May 23 00:18 tracker-extract-files.0
drwx------. 2 root root    6 May 19 15:50 vmware-root_8524-970702477
drwx------  2 root root    6 May 20 02:32 vmware-root_8529-1990534344
drwx------  2 root root    6 May 23 08:29 vmware-root_8536-995146008


三、给磁盘的分区做备份
1.磁盘分区备份
dd if=/dev/sda1 of=/root/sda1.bak
1
2.磁盘分区还原
dd if=/root/sda1.bak of=/dev/sda1 
1
四、清空磁盘
[root@192 ~]#  dd if=/dev/zero of=/dev/sdb 
dd: writing to ‘/dev/sdb’: No space left on device
41943041+0 records in
41943040+0 records out
21474836480 bytes (21 GB) copied, 172.229 s, 125 MB/s
[root@192 ~]# fdisk -l

Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk 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  Linux

Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


五、给磁盘备份
1.备份磁盘
[root@192 ~]# dd if=/dev/sda  of=/dev/sdb
41943040+0 records in
41943040+0 records out
21474836480 bytes (21 GB) copied, 249.87 s, 85.9 MB/s
[root@192 ~]# fdisk -l

Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk 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  Linux

Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk 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
1

2.还原磁盘
dd if=/dev/sdb  of=/dev/sda
1
六、备份分区表
1.备份分区表
dd  if=/dev/sda of=/root/mbr.bak bs=512 count=1
1
2.还原分区表
d  if=/root/mbr.bak of=/dev/sda
1
七、为虚拟机创建一个空磁盘文件
1.制作空磁盘文件
[root@192 ~]# dd if=/dev/zero of=aabb.img  bs=1M count=256
256+0 records in
256+0 records out
268435456 bytes (268 MB) copied, 0.132996 s, 2.0 GB/s
[root@192 ~]# ll -h
total 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.cfg
drwxr-xr-x. 2 root root    6 Feb  1 12:06 Desktop
-rw-r--r--  1 root root    0 Jun  1 06:53 disk01.img
drwxr-xr-x. 2 root root    6 Feb  1 12:06 Documents
drwxr-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.cfg
drwxr-xr-x. 2 root root    6 Feb  1 12:06 Music
drwxr-xr-x. 2 root root    6 Feb  1 12:06 Pictures
drwxr-xr-x. 2 root root    6 Feb  1 12:06 Public
drwxr-xr-x. 2 root root    6 Feb  1 12:06 Templates
drwxr-xr-x. 2 root root    6 Feb  1 12:06 Videos

1

2.制作qcow2格式磁盘文件
qemu-img create -f qcow2 -o preallocation=metadata server1.qcow2 1G
1
八、在Linux下制作启动盘
dd if=boot.img of=/dev/fd0 bs=1440k 
1
九、转换文件所有字母为大写
[root@192 data]# dd if=test1 of=test2 conv=ucase
dd: failed to open ‘test1’: No such file or directory
[root@192 data]# dd if=test1.txt of=test2.txt conv=ucase
0+1 records in
0+1 records out
102 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 

猜你喜欢

转载自blog.csdn.net/hnlgzb/article/details/129047382
今日推荐