V
V
ViGilant2014-04-13 10:57:01
linux
ViGilant, 2014-04-13 10:57:01

How to save files in bash to a directory with a serial number?

Can you please tell me how to correctly output files to a directory in a sh script, giving them names like file000001, file000002, file000003, etc.?
Adjusted for the fact that the script can start its work at any moment and not know which file is currently the last ..005th or ..505th.
* the script takes snapshots from the rtsp stream using ffmpeg, but this does not play a special role

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Y
YoungSkipper, 2014-04-13
@YoungSkipper

find . -name "file*.txt" | sort -r | head -n 1 | sed -e "s/[^0-9]//g" | read data; data=$((data + 1)); printf "file%05d.txt" "$data"

We found all the necessary files, sorted them in descending order, took the first one, pulled out the number from it, increased the number, and output it with leading zeros.
From the unpleasant - the number of leading zeros, and the file prefix are hardcoded. You can, and get rid of it, if you want.

V
Valentine, 2014-04-13
@vvpoloskin

I will offer 3 options:
1) Store the list of already received files in some text file
2) Store the value of the last file in an environment variable
3) Get the last file by creation (modified?) date and increase the name

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question