user1658170
user1658170

Reputation: 858

move linux directory unless it already exists

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

Upvotes: 2

Views: 171

Answers (2)

Daniel Arnett
Daniel Arnett

Reputation: 513

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

Replace source/ and destination/ accordingly.

Source

Upvotes: 1

Petr Skocik
Petr Skocik

Reputation: 60068

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.

Upvotes: 0

Related Questions