I was wondering if it was possible have a command that creates one file, but every time the command is executed, it wouldn't overwrite the file created in the previous execution.
For example: touch test1.txt
would create 1 file called test1.txt
. But the next time I execute it, I would like the new file to be called test2.txt
, or something like that. So without overwriting the already existing file. In a way it could be executed multiple times without problems.
Don't misunderstand me, I'm not trying to create multiple files with one command.
Thanks in advance!
The easiest solution would be to add a timestamp to the filename and not use a single digit.
The easiest method to create an empty file would be
touch test$(date +%Y%m%d-%H%M%S)
and that would result in a file namedtest20110802-170410
. A 2nd timetest*
will get a newer timestamp so it will result in 2 files.I doubt a general command exist to do that, but you can think of something like this:
The input parameter to the function is split in prefix, number, suffix, then until the file exists, the number is incremented. Found a free slot, the file is created with
touch
.The split mechanism should be adapted to your needs, and various error check should be added.
If sequence numbers are not required, then the easiest way is to use
mktemp
command.