0


Linux系统之dd命令详解

Linux系统之dd命令详解

一、dd命令介绍

1.dd介绍

1.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 in
1024+0 records out
1073741824bytes(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 3121:511G.txt
drwx------3 root root   17 May 2308:29 systemd-private-83fcd0fe514147ac8635da5768da9361-bolt.service-taaMXP
drwx------3 root root   17 May 2308:29 systemd-private-83fcd0fe514147ac8635da5768da9361-colord.service-dOXoKc
drwx------3 root root   17 May 2308:29 systemd-private-83fcd0fe514147ac8635da5768da9361-cups.service-Dchwxx
drwx------3 root root   17 May 2308:29 systemd-private-83fcd0fe514147ac8635da5768da9361-mariadb.service-DEfrgo
drwx------3 root root   17 May 2308:29 systemd-private-83fcd0fe514147ac8635da5768da9361-rtkit-daemon.service-1HtQQq
drwx------2 root root    6 May 2614:39 tmp.1KlZmVfyQO
drwx------2 root root    6 May 2300:18 tracker-extract-files.0
drwx------.2 root root    6 May 1915:50 vmware-root_8524-970702477
drwx------2 root root    6 May 2002:32 vmware-root_8529-1990534344
drwx------2 root root    6 May 2308: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 device
41943041+0 records in
41943040+0 records out
21474836480bytes(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   *20482099199104857683  Linux
/dev/sda2         2099200335564791572864083  Linux
/dev/sda3        3355648035653631104857683  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
21474836480bytes(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   *20482099199104857683  Linux
/dev/sda2         2099200335564791572864083  Linux
/dev/sda3        3355648035653631104857683  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   *20482099199104857683  Linux
/dev/sdb2         2099200335564791572864083  Linux
/dev/sdb3        3355648035653631104857683  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 in
256+0 records out
268435456bytes(268 MB) copied,0.132996 s,2.0 GB/s
[root@192~]# ll -h
total 257M
-rw-r--r--1 root root 256M Jun  106:54 aabb.img
-rw-------.1 root root 1.8K Jan 3108:15 anaconda-ks.cfg
drwxr-xr-x.2 root root    6 Feb  112:06 Desktop
-rw-r--r--1 root root    0 Jun  106:53 disk01.img
drwxr-xr-x.2 root root    6 Feb  112:06 Documents
drwxr-xr-x.2 root root    6 Feb  112:06 Downloads
-rw-r--r--.1 root root 1.8K Feb  111:50 initial-setup-ks.cfg
drwxr-xr-x.2 root root    6 Feb  112:06 Music
drwxr-xr-x.2 root root    6 Feb  112:06 Pictures
drwxr-xr-x.2 root root    6 Feb  112:06 Public
drwxr-xr-x.2 root root    6 Feb  112:06 Templates
drwxr-xr-x.2 root root    6 Feb  112: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=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
102bytes(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 
标签: linux 运维 服务器

本文转载自: https://blog.csdn.net/jks212454/article/details/125074067
版权归原作者 江湖有缘 所有, 如有侵权,请联系我们删除。

“Linux系统之dd命令详解”的评论:

还没有评论