2015/08/17

Linux dd to create an specific size empty file 建立特定大小的空檔案

最近因為工作的關係,需要在Linux 下用 dd 建立特定大小的空檔案(specific size empty file)。

終於讓我試出用 dd 建立特定大小空檔案的方法。

關於 dd


dd 原本的用途並不是建立特定大小空檔案,而是以指定的方法來備份整顆 partition 或 整顆 disk,而且不只 partition 上面的 sector 表的資料,連 superblock、boot sector、meta data 等資料,也會一併複製備份成一個檔案,實在非常好用。

由於本文的目的是要說明「建立特定大小的空檔案」,至於其他的指令操作,請參考鳥哥的 Linux 私房菜-《第九章、檔案與檔案系統的壓縮與打包-dd》。

使用 dd 建立空檔案 to create an specific size empty file


如果你想在 Linux 內建立一個 65280 Byte 大小的空檔案,可以用以下的指令:
jose@BOX:/mnt# dd if=/dev/sda2 of=file.1 bs=1 count=65280
65280+0 records in
65280+0 records out
65280 bytes (65 kB) copied, 0.372325 s, 175 kB/s
jose@BOX:/mnt# ls -al file.1
-rw-r--r-- 1 root root 65280 Jan 1 01:38 file.1


  • if:就是 input file,在案例中指定使用 /dev/sda2 這個 partition
  • of:就是 output file,在案例中指定file.1這個檔案,當然也可以指定成裝置
  • bs:規劃的一個 block size的大小,在案例中指定成1 byte,未指定時則預設 512 bytes(一個 sector 的大小)。
  • count:有多少個 bs ,在這個案例中指定要 65280 個

沒有留言:

張貼留言