jeudi 1 décembre 2016

Create a file of a specific size with random printable strings in bash

I want to create a file of a specific size containing only printable strings in bash.

My first thought was to use /dev/urandom:

dd if=/dev/urandom of=/tmp/file bs=1M count=100
  100+0 records in
  100+0 records out
  104857600 bytes (105 MB, 100 MiB) copied, 10,3641 s, 10,1 MB/s

file /tmp/file && du -h /tmp/file
  /tmp/file: data
  101M  /tmp/file

This leaves me with a file, of my desired size, but not only containing printable strings.

Now, I can use strings to create a file only containing printable strings.

cat /tmp/file | strings > /tmp/file.txt
file /tmp/file.txt && du -h /tmp/file.txt 
  /tmp/file.txt: ASCII text
  7,0M  /tmp/file.txt

This leaves me with a file containing only printable strings, but with the wrong file size.

TL;DR

How can I create a file of a specific size, containing only printable strings, in bash?




Aucun commentaire:

Enregistrer un commentaire