dd:多个输入文件

Mar*_*ter 15 dd files cat data

我需要连接两个文件中的块:

如果我需要连接整个文件,我可以简单地做

cat file1 file2 > output
Run Code Online (Sandbox Code Playgroud)

但是我需要从第一个文件中跳过前 1MB,而我只想要从第二个文件中跳过 10 MB。听起来像是一份工作dd

dd if=file1 bs=1M count=99 skip=1 of=temp1
dd if=file2 bs=1M count=10 of=temp2
cat temp1 temp2 > final_output
Run Code Online (Sandbox Code Playgroud)

有没有可能一步做到这一点?即,无需保存中间结果?我可以使用多个输入文件dd吗?

meu*_*euh 23

dd 也可以写入标准输出。

( dd if=file1 bs=1M count=99 skip=1
  dd if=file2 bs=1M count=10  ) > final_output
Run Code Online (Sandbox Code Playgroud)

  • 在像`bash` 和`mksh` 这样的shell 中,没有优化子shell 中最后一个命令的fork,你可以通过用命令组替换子shell 来稍微提高效率。对于其他 shell,这应该无关紧要,并且 subshel​​l 方法甚至可能稍微更有效,因为 shell 不需要保存和恢复 stdout。 (3认同)

Ste*_*itt 11

我不认为您可以在一次dd调用中轻松读取多个文件,但您可以通过以下几个步骤附加来构建输出文件:

dd if=file1 bs=1M count=99 skip=1 of=final_output
dd if=file2 bs=1M count=10 of=final_output oflag=append conv=notrunc
Run Code Online (Sandbox Code Playgroud)

您需要同时指定conv=notruncoflag=append。第一个避免截断输出,第二个从现有文件的末尾开始写入。


Sté*_*las 8

请记住,这ddread(),write()lseek()系统调用的原始接口。您只能可靠地使用它从常规文件、块设备和某些字符设备(如/dev/urandom)中提取数据块,即只要未到达文件末尾read(buf, size)就保证返回size的文件。

对于管道、套接字和大多数字符设备(如 ttys),除非您read()使用大小为 1 的 s 或使用 GNUdd扩展名,否则无法保证iflag=fullblock

所以要么:

{
  gdd < file1 bs=1M iflag=fullblock count=99 skip=1
  gdd < file2 bs=1M iflag=fullblock count=10
} > final_output
Run Code Online (Sandbox Code Playgroud)

或者:

M=1048576
{
  dd < file1 bs=1 count="$((99*M))" skip="$M"
  dd < file2 bs=1 count="$((10*M))"
} > final_output
Run Code Online (Sandbox Code Playgroud)

或者使用内置支持搜索运算符的外壳,例如ksh93

M=1048576
{
  command /opt/ast/bin/head -c "$((99*M))" < file1 <#((M))
  command /opt/ast/bin/head -c "$((10*M))" < file2
}
Run Code Online (Sandbox Code Playgroud)

或者zsh(假设您head支持-c此处的选项):

zmodload zsh/system &&
{
  sysseek 1048576 && head -c 99M &&
  head -c 10M < file2
} < file1 > final_output
Run Code Online (Sandbox Code Playgroud)