[Solved] shell script to backup system [closed]


Here is a solution that requires no subshell or external program, does not parse ls output (which is not recommended), and should work with filenames containing spaces (or even newlines). You can customize your prefix and extension.

#!/bin/bash
dir="/path/to/files"
prefix="backup#"
ext=".tar.gz"
max=1
for file in "$dir/$prefix"*
do
  [[ $file =~ /$prefix([0-9]+)$ext$ ]] || continue
  n="${BASH_REMATCH[1]}"
  [[ $max -gt $n ]] || max=$((n+1))
done
printf -v newfilename "$prefix%04d$ext" "$max"

3

solved shell script to backup system [closed]