3

Is there a script/tool that will allow me to create N files following a pattern like '[user-defined-string]-N', with N being incrementally increasing numbers ideally starting with a 0 until reaching 10 and the contents of the files being unique so they don't get flagged when searching for duplicate files?

Example:

user-defined-string-01
user-defined-string-01
user-defined-string-02
user-defined-string-03
user-defined-string-04
user-defined-string-05
user-defined-string-06
user-defined-string-07
user-defined-string-08
user-defined-string-09
user-defined-string-10

and so on for a specified number of iterations to create the desired number of files?

I've been creating them manually (as text files with the filename being copied and pasted as the contents), but that's time consuming. I use these files as placeholders that get deleted when I receive the associated file and an easy way to see what file I'm up to/waiting to receive. I need to be able to use a different user-defined-string each time since the names are not irrelevant.

I was thinking maybe pipe a short length of the contents of /dev/urandom into each file to achieve the uniqueness, but I'm still at a complete loss as to how to generate a desired number of files with the desired naming scheme. I have basically zero scripting experience and actually use Windows most of the time but figured since I also have Ubuntu installed I likely have a better chance of achieving this using Linux (and then possibly install WSL on Windows and see if that lets me use the script without having to boot into Ubuntu)

2 Answers2

2

One option would be to pipe data to the split utility

NAME
       split - split a file into pieces

SYNOPSIS split [OPTION]... [FILE [PREFIX]]

DESCRIPTION Output pieces of FILE to PREFIXaa, PREFIXab, ...; default size is 1000 lines, and default PREFIX is 'x'.

   With no FILE, or when FILE is -, read standard input.

Ex. read 5 (default 512 byte) blocks from /dev/urandom with dd and create 5 files with prefix user-defined-string- and two-digit numeric suffixes starting at 00 :

dd if=/dev/urandom count=5 | split -b 512 --numeric-suffixes - user-defined-string-
steeldriver
  • 142,475
1

In bash, you can use a for ...; do ...; done loop with braces expansion(e.g. {00..10} or {000..999} etc.) with head -c 256 to read 256 bytes(IMO more than that is an over-kill for your purpose and a waste of disk space and probably even less than that will do) from /dev/urandom and shell redirection > to write to a user-defined-string-+suffix(numeric between 01 and 10) file like so:

for i in {01..10}; do head -c 256 /dev/urandom > "user-defined-string-""$i"; done
Raffa
  • 34,963