How to create multiple files/directories with randomized names

command linemktemptouch

I'm trying to create some files that has different names and with different extensions. Let say I want to create for example 3 files with following name and extension:

File001.000
File002.001
File003.002

Or with alphabetical extension:

File001.A
File002.B
File003.C

Also it would be better if I could create files with random names and extension.

Filexct.cbb
Filedht.ryt
Filefht.vbf

Best Answer

The simplest way I could find is:

touch $(paste -d '.' <(printf "%s\n" File{001..005}) \
                    <(printf "%s\n" {000..004}))

This will create

File001.000  File002.001  File003.002  File004.003  File005.004

To understand how this works, have a look at what each command prints:

$ printf "%s\n" File{001..005}
File001
File002
File003
File004
File005

$ printf "%s\n" {000..004}
000
001
002
003
004

$ paste -d '.' <(printf "%s\n" File{001..005}) \
>              <(printf "%s\n" {000..004})
File001.000
File002.001
File003.002
File004.003
File005.004

So, all together, they expand to

touch File001.000 File002.001 File003.002 File004.003 File005.004 

Creating 5 files with random names is much easier:

$ for i in {1..5}; do mktemp File$i.XXX; done
File1.4Jt
File2.dEo
File3.nhR
File4.nAC
File5.Fd8

To create 5 files with random 5 alphabetical character names and random extensions, you can use this:

 $ for i in {1..5}; do 
    mktemp $(head -c 100 /dev/urandom | tr -dc 'a-z' | fold -w 5 | head -n 1).XXX
   done
jhuxe.77b
cwvre.0BZ
rpxpp.ug1
htzkq.f9W
bpgor.Bak

Finally, to create 5 files with random names and no extensions, use

$ for i in {1..5}; do mktemp -p ./ XXXXX; done
./90tp0
./Hhn4U
./dlgr9
./iVcn4
./WsJIx
Related Question