dd命令

雷滚滚發表於2024-04-27

dd命令

複製檔案,且進行資料格式轉換。


語法
dd - 轉換和複製一個檔案
dd if=輸入 of=輸出 bs=塊大小 count=總數
常用
if=file
of=file
bs=size
count=N




命令實踐
生成一個1G大小的檔案
[root@yuchao-tx-server ~]# dd if=/dev/zero of=/opt/1G.txt2 bs=100M count=10
記錄了10+0 的讀入
記錄了10+0 的寫出
1048576000位元組(1.0 GB)已複製,4.42348 秒,237 MB/秒

檢視該檔案大小
[root@yuchao-tx-server ~]# ll -h /opt/
總用量 2.0G
-rw-r--r-- 1 root root 1000M 3月 30 23:22 1G.txt
-rw-r--r-- 1 root root 1000M 3月 30 23:23 1G.txt2
drwxr-xr-x 3 root root 4.0K 3月 20 22:19 containerd

linux特殊裝置檔案

/dev/null
/dev/null代表linux的空裝置檔案,所有往這個檔案裡面寫入的內容都會丟失,俗稱“黑洞”。比較常見的用法是把不需要的輸出重定向到這個檔案。
ping yuchaoit.cn > /dev/null &



/dev/zero
零”裝置,可以無限的提供空字元(0x00,ASCII程式碼NUL)。常用來生成一個特定大小的檔案。
dd if=/dev/zero of=test.log bs=1M count=50


/dev/random和/dev/urandom
dev/random和/dev/urandom是Linux系統中提供的隨機偽裝置,這兩個裝置的任務,是提供永不為空的隨機位元組資料流。很多解密程式與安全應用程式(如SSH Keys,SSL Keys等)需要它們提供的隨機資料流。


危險玩法,比如實現類似於shred粉碎檔案的作用
[root@yuchao-tx-server ~]# dd bs=1M count=30 if=/dev/urandom of=./t1


也可以銷燬硬碟分割槽資料
dd if=/dev/urandom of=/dev/sda5

相關文章