2

I need a way to move a linux directory and all of it's contents only if it doesn't currently exist in the target location. If it does currently exist (including all sub-folders and files) then the source folder can just can just be removed recursively.

I currently use the following framework but wish to expand it to meet the above criteria.

mv /source/* /target

Thanks

user1658170
  • 814
  • 2
  • 14
  • 24

2 Answers2

1

rsync -av --remove-source-files source/ destination/ && rm -rf source/

Replace source/ and destination/ accordingly.

Source

Community
  • 1
  • 1
Daniel Arnett
  • 493
  • 2
  • 8
  • 18
0

Gnu mv has the -n or --no-clobber option. Unfortunately, it seems to return with an successful exit status even if the mv was a no-op due to the --no-clobber option, however it seems that in your use case, you can simply do the --no-clobber move and then clear the source if the move succeeds regardless of whether or not it did anything.

Petr Skocik
  • 58,047
  • 6
  • 95
  • 142