I want to copy latest file based on filename in (filename_config) & in each directory based on config file (dir_config) to backup directory.
In one directory could have > 1 filename to be copy.
dir_config:
/XXX/DIR1
/XXX/DIR2
/XXX/DIRB
/XXX/DIRY7
filename_config:
ABCD
123AB
E142
However my bash script it only copy 1 filename to the backup directory even though in the dir_config having multiple filename to be copy.
Example Output:
[Wed Jul 10 13:55:09 UTC 2024] Copy latest file from /srv/XXX/DIR1/12-ABCD_1.0_20231103082747609.zip to /srv/backup_data/XXX/DIR1
[Wed Jul 10 13:55:09 UTC 2024] Copy latest file from /srv/XXX/DIR2/E142_1.0_20231103082747609.tar to /srv/backup_data/XXX/DIR2
[Wed Jul 10 13:55:09 UTC 2024] Copy latest file from /srv/XXX/DIRB/17-ABCD_1.0_20231103082747609.tar to /srv/backup_data/XXX/DIRB
[Wed Jul 10 13:55:09 UTC 2024] Copy latest file from /srv/XXX/DIRY7/XY-123AB_1.0_20231103082747609.csv to /srv/backup_data/XXX/DIRY7
This my script:
for i in $(<$dir_config)
do
cd $Source_Dir
for y in $(<$filename_config)
do
latest_file=$(LC_ALL=C ls $i *$y* -cr | tail -1)
done
cp "$Source_Dir$i/$latest_file" $Backup_Dir$i
WriteLog "Copy latest file from $Source_Dir$i/$latest_file to $Backup_Dir$i "
done
I’m expecting as below:
Output:
[Wed Jul 10 13:55:09 UTC 2024] Copy latest file from /srv/XXX/DIR1/12-ABCD_1.0_20231103082747609.zip to /srv/backup_data/XXX/DIR1
[Wed Jul 10 13:55:09 UTC 2024] Copy latest file from /srv/XXX/DIR1/E142_1.0_20231103082747601.zip to /srv/backup_data/XXX/DIR1
[Wed Jul 10 13:55:09 UTC 2024] Copy latest file from /srv/XXX/DIR1/ZA-123AB_1.0_20231103082747601.zip to /srv/backup_data/XXX/DIR1
[Wed Jul 10 13:55:09 UTC 2024] Copy latest file from /srv/XXX/DIR2/E142_1.0_20231103082747607.tar to /srv/backup_data/XXX/DIR2
[Wed Jul 10 13:55:09 UTC 2024] Copy latest file from /srv/XXX/DIR2/12-ABCD_1.0_20231103082747602.tar to /srv/backup_data/XXX/DIR2
[Wed Jul 10 13:55:09 UTC 2024] Copy latest file from /srv/XXX/DIRB/17-ABCD_1.0_20231103082747604.tar to /srv/backup_data/XXX/DIRB
[Wed Jul 10 13:55:09 UTC 2024] Copy latest file from /srv/XXX/DIRY7/XY-123AB_1.0_20231103082747605.csv to /srv/backup_data/XXX/DIRY7