- Create many files with random content
- 6 Answers 6
- How to Create a Random Human-Readable Text File in Linux
- 1. Overview
- 2. Using the base64 Command
- 3. Using the tr Command
- 4. Using the strings Command
- 5. Using a Dictionary
- 6. Conclusion
- generate a random file using shell script
- 6 Answers 6
- Создание рандомного файла
- 2 ответа 2
Create many files with random content
What did you mean by “exclusive”? It doesn’t make sense in context, so you probably used the wrong word.
6 Answers 6
Since you don’t have any other requirements, something like this should work:
#! /bin/bash for n in ; do dd if=/dev/urandom of=file$( printf %03d "$n" ).bin bs=1 count=$(( RANDOM + 1024 )) done
(this needs bash at least for ).
This needs bash for numerous reasons, including $((…)) and $RANDOM . Even $(…) might not exist in every shell.
@G-Man, in any case, none of those features are specific to bash nor did they originate in bash ( <1..1000>comes from zsh , for n in. ; done and variable expansion comes from the Bourne shell, $(. ) , $((. )) and $RANDOM come ksh). The features that are not POSIX are <1..1000>, $RANDOM and /dev/urandom .1..1000>
If you wanted 1..1000 to be constant-width you need «%04d» in which case bash or zsh can do with no printf
Additional feature: I need a way to spread these files over lots of randomly named nested subdirectories.
A variation with seq , xargs , dd and shuf :
seq -w 1 10 | xargs -n1 -I% sh -c 'dd if=/dev/urandom of=file.% bs=$(shuf -i1-10 -n1) count=1024'
Explanation as requested per comments:
seq -w 1 10 prints a sequence of numbers from 01 to 10
xargs -n1 -I% executes the command sh -c ‘dd . % . ‘ for each sequence number replacing the % with it
dd if=/dev/urandom of=file.% bs=$(shuf . ) count=1024 creates the files feeded from /dev/urandom with 1024 blocks with a blocksize of
shuf -i1-10 -n1 a random value from 1 to 10
How to Create a Random Human-Readable Text File in Linux
The Kubernetes ecosystem is huge and quite complex, so it’s easy to forget about costs when trying out all of the exciting tools.
To avoid overspending on your Kubernetes cluster, definitely have a look at the free K8s cost monitoring tool from the automation platform CAST AI. You can view your costs in real time, allocate them, calculate burn rates for projects, spot anomalies or spikes, and get insightful reports you can share with your team.
Connect your cluster and start monitoring your K8s costs right away:
1. Overview
Sometimes, we may need to generate a human-readable text file whose content is random. For example, we might be developing a program which gets a text file as an input. If we don’t want to test this program with the same input file, we have to find a way of generating text files with random content.
In this tutorial, we’ll discuss how to generate human-readable text files with random content.
2. Using the base64 Command
Base64 is a popular binary-to-text and text-to-binary encoding/decoding scheme for transferring binary data over channels that only support text transfer. For example, it’s used for transferring binary data such as an image on the World Wide Web.
We can use the base64 command to generate a human-readable text file from binary data.
base64 is used for both Base64 encoding and decoding. If we use it without any options, it encodes the input. Its -d option is for decoding the encoded data.
We’ll get the random binary data from the /dev/urandom device file. This file is a pseudorandom number generator.
Now, let’s try to generate a human-readable file with random content using base64:
$ base64 /dev/urandom | head –c 10 pIS3VBfRhf
In the base64 /dev/urandom part of the above command, base64 encoded the binary data generated by /dev/urandom. However, since /dev/urandom device file is non-blocking, it generates random data continuously. So, we got the first 10 characters of the encoded data using the head -c 10 part of the command. In this example, the generated random data was pIS3VBfRhf which consists of 10 characters.
We can generate the human-readable file with random content simply by directing the output of the above command to a file:
$ base64 /dev/urandom | head –c 10 > random_file.txt $ cat random_file.txt /8ZtS5IfK+
Here, we directed the encoded output to the file random_file.txt. Then, we checked its content using the cat command. As it’s apparent, the output consists of 10 characters.
Base64 encoding uses an alphabet of 64 characters. These characters are the alphanumeric characters and the + and / characters.
3. Using the tr Command
We can also use the tr command for converting binary data to human-readable data and hence for creating text files with random content. This command can be used, in general, for performing text transformations such as case conversions, text replacement, or deleting characters.
We use the -d option of tr for removing specific characters in the input set. The -c option complements the input set. The [:graph:] specifies a set consisting of all printable characters, excluding space. Therefore, the tr -dc [:graph:] < /dev/urandompart of the command specified to remove all characters generated by /dev/urandom except the printable characters. That’s why we observed only printable characters in the output.
The second part of the command after the pipe, head -c 10, just limited the output to 10 characters as before.
We can change the set of the output characters. For example, we can use [:alnum:] for getting just letters and digits in the output:
The set generated by [:graph:] is, of course, a broader set than the one generated by using [:alnum:].
Finally, we must direct the output to a file to generate a text file with random content:
$ tr –dc [:graph:] < /dev/urandom | head –c 10 >random_file.txt $ cat random_file.txt TKt4r$#Z7r
We directed the output to the file random_file.txt. The file consisted of 10 randomly generated characters.
4. Using the strings Command
The strings command is another option for obtaining a text file with random content. We generally use strings for extracting the human-readable text in binary files. So, we can use this idea to transform the output of /dev/urandom into a human-readable text:
$ strings -s "" /dev/urandom | head -c 10 BB2\!C5./8
Here, strings tried to find character sequences of at least 4 characters long. We can change the default character limit of 4 using its -n option.
Normally, strings prints each found character sequence on a separate line as the newline is the default output separator. But, it’s possible to customize the output separator using the -s option. So, the -s “” part of the command specified to use an empty string as the delimiter. In other words, we concatenated the found character sequences.
We used the head -c 10 part of the command to limit the output to 10 characters as before.
We can direct the output to a file as before to have a human-readable file with random content:
$ strings -s "" /dev/urandom | head -c 10 > random_file.txt $ cat random_file.txt 7)XM/F%F;^
5. Using a Dictionary
The methods we discussed till now used /dev/urandom as the random binary data generator. We converted the binary data to a human-readable format using base64, tr, or strings. So, the generated content was completely random, having no meaning in a language.
If we need to generate random content with meaningful words, then we can use a dictionary. A dictionary in English generally comes installed in Linux distros. The dictionary file is /usr/share/dict/words. So, we can use this dictionary to generate a file with randomly chosen words from this dictionary:
$ shuf -n 5 /usr/share/dict/words | tr '\n' ' ' > random_file.txt $ cat random_file.txt discriminated intermeasuring viceversally nightwear oversocial
Here, we used the shuf command to shuffle the dictionary and then select 5 words from the shuffled dictionary. We achieved it using the shuf –n 5 /usr/share/dict/words part of the command.
shuf prints each selected word on a separate line by default, so we used tr for replacing newlines with blanks. The tr ‘\n’ ‘ ’ part of the command was for this purpose.
Finally, we directed the output to the file random_file.txt. As it’s apparent from the output, there are 5 randomly chosen words from the dictionary.
6. Conclusion
In this article, we discussed four different methods for generating a human-readable text file with random content.
First, we used base64 for obtaining text data using Base64 encoding.
Second, we filtered out the binary data using tr. We achieved this by deleting the non-printable characters in the binary data.
Third, we used strings. We extracted the human-readable text in the binary data and concatenated the extracted character sequences.
base64, tr, and strings commands used /dev/urandom device file as the input for obtaining the random binary data.
Finally, we used the dictionary file /usr/share/dict/words to generate a file with meaningful words. We used shuf for randomly selecting words from the dictionary.
generate a random file using shell script
How can i generate a random file filled with random number or character in shell script? I also want to specify size of the file.
6 Answers 6
Use dd command to read data from /dev/random.
dd if=/dev/random of=random.dat bs=1000000 count=5000
That would read 5000 1MB blocks of random data, that is a whole 5 gigabytes of random data!
Experiment with blocksize argument to get the optimal performance.
After a second read of the question, i think he also wanted to save only characters (guessing alphabetic ones) and numbers to the file.
That dd command is unlikely to complete as there will not be 5 gigabytes of entropy available. Use /dev/urandom if you need this much «randomness».
head -c 10 /dev/random > rand.txt
change 10 to whatever. Read «man random» for differences between /dev/random and /dev/urandom.
Or, for only base64 characters
head -c 10 /dev/random | base64 | head -c 10 > rand.txt
The base64 might include some characters you’re not interested in, but didn’t have time to come up with a better single-liner character converter. (also we’re taking too many bytes from /dev/random. sorry, entropy pool!)
oops, missed the characters and numbers part, i’m guessing you mean alphanumeric characters. need to revise.
#!/bin/bash # Created by Ben Okopnik on Wed Jul 16 18:04:33 EDT 2008 ######## User settings ############ MAXDIRS=5 MAXDEPTH=2 MAXFILES=10 MAXSIZE=1000 ######## End of user settings ############ # How deep in the file system are we now? TOP=`pwd|tr -cd '/'|wc -c` populate() < cd $1 curdir=$PWD files=$(($RANDOM*$MAXFILES/32767)) for n in `seq $files` do f=`mktemp XXXXXX` size=$(($RANDOM*$MAXSIZE/32767)) head -c $size /dev/urandom >$f done depth=`pwd|tr -cd '/'|wc -c` if [ $(($depth-$TOP)) -ge $MAXDEPTH ] then return fi unset dirlist dirs=$(($RANDOM*$MAXDIRS/32767)) for n in `seq $dirs` do d=`mktemp -d XXXXXX` dirlist="$dirlist$$PWD/$d" done for dir in $dirlist do populate "$dir" done > populate $PWD
Создание рандомного файла
Собственно, нужно создать большой по размеру(~100mb) файл, с рандомными символами. Желательно сделать это средствами системы и максимально просто. Есть идеи?
2 ответа 2
Можно записать ~10 8 случайных байт из /dev/urandom
head -c 100000000 /dev/urandom > file
dd if=/dev/urandom of=file bs=100M count=1 iflag=fullblock
Можно записать только печатные символы как-то так:
Можно и по совету использовать base64:
Вроде при таком подходе это будет столь же безопасно (никакой конец обрезать не надо), но даст меньший набор печатных символов.
можно использовать base64 для более «экономичного» получения печатных ascii-символов из набора байт. только последние несколько байт вывода программы лучше отрезать — они весьма «неслучайны».
Если задача именно создать «заглушку» то есть уже готовое решение в util-linux
~$ fallocate --help Usage: fallocate [options] Preallocate space to, or deallocate space from a file. Options: -c, --collapse-range remove a range from the file -d, --dig-holes detect zeroes and replace with holes -l, --length length for range operations, in bytes -n, --keep-size maintain the apparent size of the file -o, --offset offset for range operations, in bytes -p, --punch-hole replace a range with a hole (implies -n) -z, --zero-range zero and ensure allocation of a range -v, --verbose verbose mode -h, --help display this help and exit -V, --version output version information and exit For more details see fallocate(1).