3

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
αғsнιη
  • 40,939
  • 15
  • 71
  • 114

3 Answers3

4

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
terdon
  • 234,489
  • 66
  • 447
  • 667
2

If you want to guarantee that they are different, look into mktemp:

$ mktemp 
/tmp/tmp.r8FumWPhn5
$ mktemp XXXXXXXX.tmp
Pu4Ii6Sf.tmp
l0b0
  • 50,672
  • 41
  • 197
  • 360
  • I'd like to have also random extension with random names. If I use `mktemp xxx.xxx` I get error: `mktemp: too few X's in template ‘XXX.XXX’` – αғsнιη Nov 21 '14 at 16:23
  • I believe you need at least eight `X`es to make some implementations of `mktemp` happy. – l0b0 Nov 21 '14 at 16:25
2

I found an easy way to creating files and also directory with random names and extensions:

To create random file with random ext use this command:

$ mktemp XXX.$(mktemp -u XXX)
-u, --dry-run
    do not create anything; merely print a name (unsafe)

And for creating multiple files use above command inside a loop:

$ for create in $(seq 5); do
      mktemp XXX.$(mktemp -u XXX);
  done

nDq.NoB
mT2.Ns0
DEN.aga
K7b.HCf
pNC.q9N

And finally if you want to create multiple directory, use mktemp with its -d option.

mktemp -d XXX.$(mktemp -u XXX)
αғsнιη
  • 40,939
  • 15
  • 71
  • 114