Best explained by an example:

Source dir:

  • file-a-1.txt
  • file-b-1.txt

Target dir:

  • file-a-2.txt

Expected result:

  • file-a-2.txt
  • file-b-1.txt

Move all files from src dir to target dir only if file from source dir to target dir if target dir does not have a file that partially matches the filename.

  • What do you mean by “partially matches”? Do you mean that they should differ only in one sequence of digits? Only in one sequence of digits immediately before the last dot in the file name? Only in one sequence of alphanumeric characters between the next-to-last punctuation mark and the last punctuation mark? Something else? – Gilles 'SO- stop being evil' Jul 11 '16 at 23:00
  • Solved it. Not relevant exactly what part should match, thats only the regex part. – abaelter Jul 12 '16 at 14:41


  1. List files in target dir

  2. Replace to wildcard match with sed

  3. Pipe to rsync --exclude-from

find target_dir | sed -r 's/\.\/(.+?)-.*/\1*/' | rsync --verbose --ignore-existing --exclude-from - src_dir/* target_dir

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.