0

I'm trying to write a script that moves files and (sub)directories from within one directory to another. That move operation should include not only regular files and directories but also any dot-files. So far I've come up with this:

mv -f "$SOURCE_DIR"/.?? "$TARGET_DIR"
mv -f "$SOURCE_DIR"/.??* "$TARGET_DIR"
mv -f "$SOURCE_DIR"/* "$TARGET_DIR"

But these commands fail if there are no files matching the patterns.

I need this move operation to succeed in any case ..

  • regardless of the file name
  • regardless if there are no files at all
  • regardless of the type: file, link, directory, etc.

But I need the operation to fail if something goes wrong completely (e.g. insufficient privileges at the target directory, etc.) so just ignoring any errors is not a good option.

How to I solve this problem? I guess there is a very simple solution to that which I overlooked.

Thank you for your help!

1

You are probably making this harder than you need to.

mv $srcdir $destdir
3
  • You're right. But only in this very specific case: I can abandon the source directory as well and move it to the target as is. I came up with the existing solution in the first place as I though I could not remove the source directory ... So I guess your answer perfectly solves my problem (Thank you!!!) but I guess it does not answer the question ... ;-) – Regis May Jan 14 at 16:07
  • So what should I do? Accept it as it solves my problem? It DOES solve my problem ... (Thank you!!!) – Regis May Jan 14 at 16:07
  • if that answers your questions please give them the acceptance – djdomi Jan 14 at 16:54

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.