I shut up the following: the script tracks the size of a specific file (always one) and if its size is above a static threshold value, it creates an archive from this file, and the file deletes it. The problem is that when the file reaches the maximum value again, a new archive will be created that will overwrite the old one. Question: how to make a counter in the name of the archive being created (and check)? If there is an x.tgz file in the / 1 directory, then create x1.tgz, if there is x1.tgz, then create x2.tgz indefinitely.
- Use the date as the file name extension. - sergw
- possible through logrotate, but I want to be perverted - Sebastian Pereiro
- A date is a good idea, but it’s a cheat ^^ - Sebastian Pereiro
- Then write a script that calculates the maximum number in the name of an existing archive file and adds to it 1. - sergw
|
3 answers
name=somefile if [[ -e $name.tgz ]] ; then i=0 while [[ -e $name-$i.tgz ]] ; do let i++ done name=$name-$i fi touch $name.tgz
- With this cycle I am trying to achieve efficiency, but something is swearing at fi - Sebastian Pereiro
- It all worked. Thank! - Sebastian Pereiro
- I see one drawback of this option, in case of deletion of old files, it will generate the names of files with old numbers. - sergw
- I agree, but they will have a creation date, t.ch. you can determine. - Sebastian Pereiro
- Another answer is devoid of the indicated disadvantage, since calculates the number by the names of existing files. - sergw
|
Get an array of existing files, sort, take the last element, select a number from it, add one, write a new file under this name. Accordingly, if nothing from the disc was considered, then the file is the first.
|
design:
$ base=x $ echo $(($(ls ${base}*.tgz 2>/dev/null | sed -rn 's/.*[^0-9]([0-9]+)\.tgz/\1/p' | sort -n | tail -n 1)+1))
will return the next number for files that fall under the mask x*.tgz
(and one if there are no such files or only x.tgz
).
The “base” name is specified in the variable base
.
You can save this number to the next
variable for further use:
base=x next=$(($(ls ${base}*.tgz 2>/dev/null | sed -rn 's/.*[^0-9]([0-9]+)\.tgz/\1/p' | sort -n | tail -n 1)+1))
- for the complete happiness of the author, this script should be supplemented with
echo $next touch $base$next.tgz
- sergw
|