There is a directory including several XML files.
Each filename has the following format: yyyy-mm-dd-hh-mm-ss-AUFXXXXXXXXX.xml
Some XML filenames have the same number which is coming after AUF because they've been modified. I want to have a bash script which should recognize these kinds of files, keep the origin ones by checking the timestamps and move the modified files to another folder. I'm trying to write bash script to solve this issue. This is what I've got so far:
#!/bin/bash
declare -a filelist
shopt -s extglob
for file in $(ls -tr1 *AUF*); do
filelist=(${filelist[@]} "$file")
echo "${file}"
done
part of output:
2019-11-14-17-44-04-AUF19000276.xml
2019-11-15-09-12-01-AUF19000276.xml
2019-11-15-09-27-26-AUF19000276.xml
2019-11-15-09-28-51-AUF19000276.xml
2019-11-18-13-50-34-AUF19000296.xml
2019-11-20-16-45-14-AUF19000300.xml
2019-11-27-12-16-25-AUF19000292.xml
2019-11-27-12-19-50-AUF19000225.xml
2019-11-27-17-11-04-AUF19000300.xml
2019-11-28-09-40-44-AUF19000294.xml
2019-11-29-17-03-33-AUF19000305.xml
2019-11-29-17-04-43-AUF19000306.xml
2019-11-29-17-05-41-AUF19000306.xml
2019-12-02-12-02-20-AUF19000305.xml
2019-12-02-12-03-00-AUF19000305.xml
2019-12-03-09-22-06-AUF19000307.xml
2019-12-04-10-49-03-AUF19000308.xml
2019-12-05-09-23-54-AUF19000310.xml
2019-12-05-09-24-41-AUF19000310.xml
2019-12-09-13-12-31-AUF19000256.xml
2019-12-09-13-59-42-AUF19000256.xml
2019-12-09-15-29-25-AUF19000281.xml
2019-12-09-15-30-13-AUF19000281.xml
2019-12-09-15-34-07-AUF19000284.xml
2019-12-09-15-39-39-AUF18000346.xml
2019-12-09-15-40-21-AUF19000058.xml
2019-12-10-16-19-35-AUF19000312.xml
2019-12-11-11-58-55-AUF19000313.xml
For example: I want to keep the first created file 2019-11-14-17-44-04-AUF19000276.xml and move the other three files with the same number to another directory.
I currently don't know how to check through the array and include the above-mentioned conditions. Glad for any help!
I think the following (minus the echo
) will do the job ...
# iterate over the AUF* parts
for i in $( ls | sed -r 's/^.*(AUF[^.]+)\.xml/\1/' | sort -u )
# iterate over the sections, move all but the first (oldest) files
do for j in $( ls -1 *${i}*| tail -n +2 )
do echo mv $j newdir
done
done
Remove the echo
if the output looks sane =}