Copying files based on partial names in a file

Apricot

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.

darxmurf

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
  • pushd will move you in the specified directory
  • for+find line will get the ID from your text file, find files ending like _ID.xml and move them in the /home/iris/xmlfiles folder
  • the last find like will delete the non moved files but only in this folder and not sub ones
  • popd will put you back in your original directory

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Copying files based on input list, special characters in file names

From Dev

(OS X Shell) Copying files based on text file containing file names without extensions

From Dev

Copying certain files by partially matching their names to a list within a text file

From Dev

Bash - Check directory for files against list of partial file names

From Dev

Bash - Check directory for files against list of partial file names

From Dev

Select wav files from a folder whose partial names are in a text file

From Dev

Copying files based on a condition

From Dev

batch file to copy files based on their names

From Dev

Sort files based on similarity of file names

From Dev

Concatenating Files based on first characters of file names

From Dev

Copying file names into an array with C

From Dev

Copying file names into an array with C

From Dev

Move specific files to another folder based on partial pattern in .txt file

From Dev

Search for files based on a list of partial names and copy them to a destination folder using windows shell

From Dev

Copying file names to a text file in PowerShell

From Dev

copying files with particular names to another folder

From Dev

Copying files with the names having a number within a range

From Dev

Docker image not copying files names starting with

From Dev

Regex Grab files names based on a list of file extensions

From Dev

batch rename zip files based on contained file names

From Dev

How to move the files to new directory based on names in text file?

From Dev

Untar files to destination directories based on tar file names

From Dev

How to symbolic link the files to new directory based on names in text file?

From Dev

Moving large number of files into directories based on file names in linux

From Dev

moving files into different directories based on their names matching with another file

From Dev

Edit files with sed and save the result to different files whose names are based on the original file names

From Dev

Copying files with file locks in Java

From Dev

Copying modified files and their file structure

From Dev

Subset data based on partial match of column names

Related Related

  1. 1

    Copying files based on input list, special characters in file names

  2. 2

    (OS X Shell) Copying files based on text file containing file names without extensions

  3. 3

    Copying certain files by partially matching their names to a list within a text file

  4. 4

    Bash - Check directory for files against list of partial file names

  5. 5

    Bash - Check directory for files against list of partial file names

  6. 6

    Select wav files from a folder whose partial names are in a text file

  7. 7

    Copying files based on a condition

  8. 8

    batch file to copy files based on their names

  9. 9

    Sort files based on similarity of file names

  10. 10

    Concatenating Files based on first characters of file names

  11. 11

    Copying file names into an array with C

  12. 12

    Copying file names into an array with C

  13. 13

    Move specific files to another folder based on partial pattern in .txt file

  14. 14

    Search for files based on a list of partial names and copy them to a destination folder using windows shell

  15. 15

    Copying file names to a text file in PowerShell

  16. 16

    copying files with particular names to another folder

  17. 17

    Copying files with the names having a number within a range

  18. 18

    Docker image not copying files names starting with

  19. 19

    Regex Grab files names based on a list of file extensions

  20. 20

    batch rename zip files based on contained file names

  21. 21

    How to move the files to new directory based on names in text file?

  22. 22

    Untar files to destination directories based on tar file names

  23. 23

    How to symbolic link the files to new directory based on names in text file?

  24. 24

    Moving large number of files into directories based on file names in linux

  25. 25

    moving files into different directories based on their names matching with another file

  26. 26

    Edit files with sed and save the result to different files whose names are based on the original file names

  27. 27

    Copying files with file locks in Java

  28. 28

    Copying modified files and their file structure

  29. 29

    Subset data based on partial match of column names

HotTag

Archive