I have millions of xml files in a folder. The name of the files follow a specific pattern:
ABC_20190101011030931_6049414.xml
In this I am interested only in the last set of digits before xml 6049414
. I have a list of around 8000 such numbers in a text file. The details in the text file is as follows - a number in a line:
104638
222885
108880071
I am using the following code to move the files from the folder that matches the number given in the text file:
#folder where the xml files are stored
cd /home/iris/filesToExtract
SECONDS=0
#This line reads each number in the hdpvr.txt file and if a match is found moves that file to another folder called xmlfiles.
nn=($(cat /home/iris/hdpvr.txt));for x in "${nn[@]}";do ls *.xml| grep "$x"| xargs -I '{}' cp {} /home/iris/xmlfiles;done
#this line deletes all the other xml files from filesToExtract folder
find . -name "*.xml" -delete
echo $SECONDS
I am facing two issues. 1 Some of the files are not getting moved despite there is a match and 2. Even if the match is found in the middle part of the file name for example
from this ABC_20190101011030931_6049414.xml -> this 20190101011030931
if a match is found it still moves....how can I get the exact matches and move the files.
Would something like this make the job ?
pushd /home/iris/filesToExtract
for i in $(</home/iris/hdpvr.txt); do find . -mindepth 1 -maxdepth 1 -type f -name "*_$i.xml" -print0 | xargs -r -0 -i mv "{}" /home/iris/xmlfiles; done
find . -mindepth 1 -maxdepth 1 -type f -name "*.xml" -delete
popd
You can also do it the brutal way with mv but it will throw errors if a file is not found
pushd /home/iris/filesToExtract
for i in $(</home/iris/hdpvr.txt); do mv "*_$i.xml" /home/iris/xmlfiles; done
find . -mindepth 1 -maxdepth 1 -type f -name "*.xml" -delete
popd
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments