How to create a random .txt(Human readable text like ascii) file in linux

We can do it by following command

base64 /dev/urandom | head -c 10000000 > file.txt

It creates a file with name file.txt size of 10 MB.


get the output of:

tr -dc A-Za-z0-9 </dev/urandom 

and pipe it to a file.

You can use head command with -c or -n to limit the file size

example to generate a 1kB file a.txt:

tr -dc A-Za-z0-9 </dev/urandom | head -c 1024 > a.txt

base64 seems to only output alphanumeric characters plus / and +.

I like this to get more "punctuation" characters, like

'[:punct:]'
  Punctuation characters; in the 'C' locale and ASCII character
  encoding, this is ! " # $ % & ' ( ) * + , - . / : ; < = > ? @ [ \
  ] ^ _ ` { | } ~

So use this:

'[:graph:]'
     Graphical characters: '[:alnum:]' and '[:punct:]'

and use tr to remove single quotes ' backticks ` and backslashes \

tr -dc '[:graph:]' < /dev/urandom | tr -d \''\\'\` | head -c [size]

the -c size option to head can have a multiplier suffix: b 512, kB 1000, K 1024, MB 1000*1000, M 1024*1024, GB 1000*1000*1000, G 1024*1024*1024, and so on for T, P, E, Z, Y.

Tags:

Linux