如何用标准工具高效填充文件为重复常量值(含单字节与任意字符串)
If you need to generate a file filled with a repeated constant (either a single byte or an arbitrary string) efficiently—without writing compiled code and avoiding the painfully slow looped echo approach—here are the best standard tool-based solutions:
Single-Byte Constants
Case 1: Zero-byte (0x00) fill
You already noted the optimized dd command for this scenario. Adjust bs (block size) and count to hit your desired file size (total size = bs * count):
dd if=/dev/zero of=/path/to/output_file bs=1M count=100 # Creates a 100MB file of zeros
Larger bs values cut down on I/O operations, so pick a size aligned with your system's capabilities (e.g., 1M, 4M, or your storage device's native sector size).
Case 2: Non-zero single-byte fill
For a repeated byte like 0x41 (='A') or 0xFF (255), use printf to generate the byte once, then pipe it through dd for efficient scaling:
# Fill a 50MB file with the byte 'A' (0x41) printf '\x41' | dd of=/path/to/output_file bs=1M count=50 conv=notrunc
Add conv=notrunc if you're appending to an existing file; omit it to overwrite entirely.
Alternatively, use yes (we just strip the default newline it adds):
yes 'A' | tr -d '\n' | head -c 50M > /path/to/output_file
This works well for human-readable single characters, but dd is generally more performant for large files.
Arbitrary String Constants
When repeating multi-character strings (e.g., "HelloStackOverflow"), skip shell loops or massive brace expansions (they can crash your shell for huge repeat counts). Try these methods instead:
Method 1: yes + head (simple & efficient)
Use yes to generate the string repeatedly, strip newlines, then truncate to your total desired byte count:
STR="HelloStackOverflow" TOTAL_REPEATS=1000000 TOTAL_BYTES=$(( ${#STR} * TOTAL_REPEATS )) # Calculate total size yes "$STR" | tr -d '\n' | head -c "$TOTAL_BYTES" > /path/to/output_file
Method 2: dd with a template file (best for extreme file sizes)
For massive files, create a small template with your string once, then use dd to copy it repeatedly—this minimizes process overhead:
STR="HelloStackOverflow" # Create a template file with one instance of the string echo -n "$STR" > /tmp/template.txt TEMPLATE_SIZE=$(stat -c%s /tmp/template.txt) REPEATS=1000000 # Number of times to copy the template # Generate the final file by repeating the template dd if=/tmp/template.txt of=/path/to/output_file bs="$TEMPLATE_SIZE" count="$REPEATS"
If you need a specific total file size (not exact repeats), adjust count and add a final dd command to truncate or pad as needed.
Why Avoid Looped echo >> file?
Every echo call opens the file, writes a tiny chunk of data, then closes the file. This creates thousands or millions of unnecessary I/O operations, making the process orders of magnitude slower than the methods above—always skip this approach for large files.
内容的提问来源于stack exchange,提问作者einpoklum

