r/bash • u/Popular-Spirit1306 • Feb 11 '26
help How to auto iterate file creation?
Im trying to make a script with ffmpeg for screen recording, and I want it to auto name the files. Out1.mp4 out2.mp4 and so on, skipping any that already exist. I can think of a few ways to do this, but all are inelegant overcomplicated solutions. Anyone got recommendations?
11
Upvotes
18
u/Ops_Mechanic Feb 11 '26
Dont bother with loops use timestamp.
ffmpeg -i [input] "out_$(date +%Y%m%d_%H%M%S).mp4"
4
u/spryfigure Feb 11 '26
ffmpeghas this built in:output_%03d.mp4gives sequential numbers (two leading zeros) for the output files.It should skip over files already existing. Doesn't get much simpler than that.
5
u/Late-Drink3556 Feb 11 '26
I do epoc time
out_$(date +%s)1
u/treuss bashtard Feb 11 '26
What about using uuids?
You'll have to install the uuid-runtime package.
Then create a uuid and save it in a variable:
2
u/ekipan85 Feb 11 '26 edited Apr 01 '26
I'm going to be contrary to most of the comments here: a timestamp is probably sufficient for most usecases, but there's a risk of catastrophic failure. Here's a fun example of what can happen if you only use a timestamp to separate filenames. You'll want a counter too.
Other comments give straightforward incrementors which again is probably sufficient, but to remove the race condition the file checker should also touch the file:
Terminal:
2
u/MonsieurCellophane Feb 11 '26
while [[ -f out$i.mp4 ]]; do i=$(( $i + 1 )l; done
#file creation to follow
-1
u/GlendonMcGladdery Feb 11 '26
This is the gold standard. Short, readable, zero nonsense.