在 bash 中生成虚拟文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9800405/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 21:49:17  来源:igfitidea点击:

Generate dummy files in bash

bashdummy-data

提问by jabal

I'd like to generate dummy files in bash. The content doesn't matter, if it was random it would be nice, but all the same byte is also acceptable.

我想在 bash 中生成虚拟文件。内容无关紧要,如果它是随机的就好了,但所有相同的字节也是可以接受的。

My first attempt was the following command:

我的第一次尝试是以下命令:

rm dummy.zip;
touch dummy.zip;
x=0;
while [ $x -lt 100000 ];
do echo a >> dummy.zip;
  x=`expr $x + 1`;
done;

The problem was its poor performance. I'm using GitBash on Windows, so it might be much faster under Linux but the script is obviously not optimal.

问题是它的性能不佳。我在 Windows 上使用 GitBash,所以它在 Linux 下可能要快得多,但脚本显然不是最佳的。

Could you suggest me a quicker and nice way to generate dummy (binary) files of given size?

你能给我建议一种更快更好的方法来生成给定大小的虚拟(二进制)文件吗?

回答by kev

You can try headcommand:

你可以试试head命令:

$ head -c 100000 /dev/urandom >dummy

回答by user unknown

You may use ddfor this purpose:

您可以为此使用dd

dd if=/dev/urandom bs=1024 count=5 of=dummy
  • if:= in file
  • of:= out file
  • bs:= block size
  • 如果:= 在文件中
  • of:= 输出文件
  • bs:= 块大小

Note, that

注意

 x=`expr $x + 1`;

isn't the most efficient way to calculation in bash. Do arithmetic integer calculation in double round parenthesis:

不是 bash 中最有效的计算方式。在双圆括号中进行算术整数计算:

 x=((x+1)) 

But for an incremented counter in a loop, there was the for-loop invented:

但是对于循环中的递增计数器,发明了 for 循环:

x=0;
while [ $x -lt 100000 ];
do echo a >> dummy.zip;
  x=`expr $x + 1`;
done;

in contrast to:

相比之下:

for  ((x=0; x<100000; ++x))
do
    echo a 
done >> dummy.zip 

Here are 3 things to note:

这里有3点需要注意:

  • unlike the [ -case, you don't need the spacing inside the parens.
  • you may use prefix (or postfix) increment here: ++x
  • the redirection to the file is pulled out of the loop. Instead of 1000000 opening- and closing steps, the file is only opened once.
  • 与 [ -case 不同,您不需要括号内的间距。
  • 您可以在此处使用前缀(或后缀)增量:++x
  • 对文件的重定向被拉出循环。文件只打开一次,而不是 1000000 个打开和关闭步骤。

But there is still a more simple form of the for-loop:

但是还有一种更简单的 for 循环形式:

for x in {0..100000}
do
    echo a 
done >> dummy.zip 

回答by glenn Hymanman

This will generate a text file 100,000 bytes large:

这将生成一个 100,000 字节大的文本文件:

yes 123456789 | head -10000 > dummy.file

回答by vbarbarosh

$ openssl rand -out random.tmp 1000000

回答by mivk

If your file system is ext4, btrfs, xfs or ocfs2, and if you don't care about the content you can use fallocate. It's the fastest method if you need big files.

如果您的文件系统是 ext4、btrfs、xfs 或 ocfs2,并且您不关心内容,则可以使用fallocate. 如果您需要大文件,这是最快的方法。

fallocate -l 100KB dummy_100KB_file

See "Quickly create a large file on a Linux system?" for more details.

详情请参阅“在 Linux 系统上快速创建大文件?”。

回答by Stefan Certic

Easy way:

简单的方法:

make file test and put one line "test"

制作文件测试并放置一行“测试”

Then execute:

然后执行:

 cat test >> test

ctrl+c after a minute will result in plenty of gigabytes :)

一分钟后 ctrl+c 将导致大量千兆字节:)

回答by Daniel Stańczyk

Possibly

可能

dd if=/dev/zero of=/dummy10MBfile bs=1M count=10

回答by Tenzing

echo "To print the word in sequence from the file" c=1 for w in cat filedo echo "$c . $w" c = expr $c +1done

echo "从文件中按顺序打印单词" c=1 for w in cat filedo echo "$c . $w" c = expr $c +1done