Linux generating random file

Generate a random filename in unix shell

I would like to generate a random filename in unix shell (say tcshell). The filename should consist of random 32 hex letters, e.g.:

c7fdfc8f409c548a10a0a89a791417c5 

(to which I will add whatever is neccesary). The point is being able to do it only in shell without resorting to a program.

14 Answers 14

Assuming you are on a linux, the following should work:

cat /dev/urandom | tr -cd 'a-f0-9' | head -c 32 

This is only pseudo-random if your system runs low on entropy, but is (on linux) guaranteed to terminate. If you require genuinely random data, cat /dev/random instead of /dev/urandom . This change will make your code block until enough entropy is available to produce truly random output, so it might slow down your code. For most uses, the output of /dev/urandom is sufficiently random.

If you on OS X or another BSD, you need to modify it to the following:

cat /dev/urandom | env LC_CTYPE=C tr -cd 'a-f0-9' | head -c 32 

This solution was actually doing weird things for me, as it appended a white-backgrounded «%» sign after the actual random hash, but because my shell is generally behaving strange on some occasions, I didn’t want to make this look bad before it was accepted 🙂

I tried this on a Mac, which has a /dev/urandom. Executing the command in a bash shell causes an error — ‘tr: Illegal byte sequence’

I think the problem here is that BSD and Mac’s interpret the string as being multibyte instead of single byte. I haven’t got a machine to try this on, so report back here if this works: cat /dev/urandom | env LC_CTYPE=C tr -cd ‘a-f0-9’ | head -c 32

why do not use unix mktemp command:

$ TMPFILE=`mktemp tmp.XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX` && echo $TMPFILE tmp.MnxEsPDsNUjrzDIiPhnWZKmlAXAO8983 

some implementations have a —dry-run flag to prevent a file being created. That of course opens a possible race condition.

While it is not a big deal, this utility makes an extra IO check to test whether the XXXX file exists on disk (just a stat) even when —try-tun is specified. Just a minor consideration and maybe an insignificant trade off for a great convenience

One command, no pipe, no loop:

hexdump -n 16 -v -e '/1 "%02X"' -e '/16 "\n"' /dev/urandom 

If you don’t need the newline, for example when you’re using it in a variable:

hexdump -n 16 -v -e '/1 "%02X"' /dev/urandom 

Using «16» generates 32 hex digits.

uuidgen generates exactly this, except you have to remove hyphens. So I found this to be the most elegant (at least to me) way of achieving this. It should work on linux and OS X out of the box.

As you probably noticed from each of the answers, you generally have to «resort to a program».

However, without using any external executables, in Bash and ksh:

string=''; for i in ; do string+=$(printf "%x" $(($RANDOM%16)) ); done; echo $string 
string=''; for i in ; do string+=$(printf "%x" $(($RANDOM%16)) ); dummy=$RANDOM; done; echo $string 

Change the lower case x in the format string to an upper case X to make the alphabetic hex characters upper case.

Читайте также:  Файл подкачки linux manjaro

Here’s another way to do it in Bash but without an explicit loop:

printf -v string '%X' $(printf '%.2s ' $((RANDOM%16))' ') 

In the following, «first» and «second» printf refers to the order in which they’re executed rather than the order in which they appear in the line.

This technique uses brace expansion to produce a list of 32 random numbers mod 16 each followed by a space and one of the numbers in the range in braces followed by another space (e.g. 11 00 ). For each element of that list, the first printf strips off all but the first two characters using its format string ( %.2 ) leaving either single digits followed by a space each or two digits. The space in the format string ensures that there is then at least one space between each output number.

The command substitution containing the first printf is not quoted so that word splitting is performed and each number goes to the second printf as a separate argument. There, the numbers are converted to hex by the %X format string and they are appended to each other without spaces (since there aren’t any in the format string) and the result is stored in the variable named string .

When printf receives more arguments than its format string accounts for, the format is applied to each argument in turn until they are all consumed. If there are fewer arguments, the unmatched format string (portion) is ignored, but that doesn’t apply in this case.

I tested it in Bash 3.2, 4.4 and 5.0-alpha. But it doesn’t work in zsh (5.2) or ksh (93u+) because RANDOM only gets evaluated once in the brace expansion in those shells.

Note that because of using the mod operator on a value that ranges from 0 to 32767 the distribution of digits using the snippets could be skewed (not to mention the fact that the numbers are pseudo random in the first place). However, since we’re using mod 16 and 32768 is divisible by 16, that won’t be a problem here.

In any case, the correct way to do this is using mktemp as in Oleg Razgulyaev’s answer.

Источник

How to Create a Random Human-Readable Text File in Linux

announcement - icon

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.

Читайте также:  Astra linux объем дисков

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.

Источник

Оцените статью
Adblock
detector