I have a bash script that classifies and retrieves files from a remote server. I have trouble with the classification step, which is based on the file names.
I am correctly able to identify the different families of files that are defined by the start of the filenames. Filenames can be:
ala-olo_ulu-1602915797.txt
ala-olo_ulu-1602915799.txt
ili-olo-1602915897.txt
ili-olo-1602915997.txt
ili-olo-pip-1602925797.txt
ili-olo-pip-1602935797.txt
In this example, I have 3 families:
ala-olo_ulu
ili-olo
ili-olo-pip
(pure examples :))
Each family is treated in one iteration of a loop.
In such an iteration, I have the family name available in an variable BASE_NAME
(for instance ili-olo
).
My trouble is the taring step, before rsync'ing the files to local. I am managing it with the following ssh command.
ssh root@"${RMT_IP}" '
for FILE in "'${BASE_NAME}'*'${FILE_EXTENSION}'"; do
tar -rf "'${BASE_NAME}'.tar" ${FILE} --remove-files
done' < /dev/null
With this script, unfortunately, if ili-olo
is managed before ili-olo-pip
, then, the archive will contain both families (they both share the same start).
And when ili-olo-pip
will be then managed, they won't be any file anymore, and the tar command ends in error. (which is how I could spot the trouble).
I guess, I should rather use regex to specify that the variable part of the file name is the digit part.
Please, how can I change the for
loop definition so that the families starting with the same string do not get into the same tar?
for FILE in "'${BASE_NAME}'*'${FILE_EXTENSION}'"; do
?
The digit part always has the same number of digits (it is a timestamp, with second precision), for instance 1602915797
I thank you for your help. Have a good day, Bests, Pierre