Is there a cross-platform package to move files/folders with Go?

Is there a package that takes care of the entire process for moving files/directories?

For example, if the source and destination are on the same partition use os.Rename, if they are on different partitions copy the source to the destination and if there were no errors delete the source.

Is there a package that does all that for me? Ideally, it should support Linux and Windows.

Hello there, I had some code which I use in my project as external module. You can check it here. Dunno if it’s exactly what you are looking for, and sometimes it’s still work in progress.

1 Like

You got pretty close, it is very useful to move between different partitions (from disk C to D for example), when I want to move in the same partition (D:\someFolder to D:\anotherFolder for example) your package still makes a complete copy when you should just use an os.Rename().

Knowing if the source and destination are in the same partition seems not as simple as it seems.

1 Like

Hm, interesting idea for the future updates. Thx for the comment

1 Like

I think the most straightforward solution for now can be just a try to rename src to dst. If there is an error, then proceed with copy. E.g.,

if opt.move {
	if err := os.Rename(src, dst); err == nil {
		return nil
	}
}

As a review I would say that all that remains is to create any dst directory for each file(s) to be moved to avoid the error that os.Rename returns when the destination directory does not exist.

I’ve added some updates and marked them as v1.2.0-a4898caa. I tested move operations and it uses os.Rename where possible and creates dest subfolders if required.

1 Like