He everybody,

I couldn't find an answer to my question, so here goes:

I want to make a "job" that replaces / distributes 10000+ PDF-files from 1 directory to a unique folder where "the same" PDF (only PDF name is the same) is already.

Example: directories with 10000+ PDF-files = /my_dir/backup there are 10000+ random PDF's, like 143079.pdf, 493857.pdf and 888675.pdf

The "job" has to search for the folder where 143079.pdf is already in, for example: its located in /os_dir/pdf/EDFR_29384_euendho and replace it. So the "job" will see /my_dir/backup/143079.pdf and start searching in the folder on /os_dir/pdf/ and will find it in /os_dir/pdf/EDFR_29384_euendho/. He has to replace 143079.pdf in /os_dir/pdf/EDFR_29384_euendho/ with the 143079.pdf from /my_dir/backup/. He has to do this 10000+ times with all random PDF's and all random folders.

What is the best way to do this?


find + bash solution:

find . -type f -name "*.pdf" -exec bash -c \
'p="/my_dir/backup/"; bn=${1##*/}; 
 [[ "$1" != $p* ]] && [[ -f "${p}$bn" ]] && cp "${p}$bn" "$1"' _ {} \;

  • p="/my_dir/backup/" - path to the initial (crucial) directory
  • bn=${1##*/} - file's basename
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.