Copy a file preserving modes, timestamps, owners


I was wondering how I could go about copying a file in Elixir while preserving as much as possible ie. Timestamps etc.

You need to copy file and then set correct attributes. I do not believe that there exists “magical” function that would do such for you.

Does File.cp/3 work for you?

Hi hauleth,

Ohh … didn’t realize that.
Shall check that out as well.

Hi Nobbz,

Nopes File.cp/3 doesn’t preserve the timestamp.
It just preserves the mode.

Which timestamp?

Even cp doesn’t preserve them…

Nopes …
I am using File.cp/3 only

Does it work for you?

As I said, I’m not aware of any copy mechanism to preserve timestamps, even from outside of elixir, except for copying through a tarball…


Okay, just remembered that there is --preserve option to cp, so you’ll need to shell out, System.cmd/3

1 Like

So I was just looking through the docs and I see there is a File.write_stat/3 func.
Maybe I can try that after copying the file with File.cp/3.

So using File.write_stat/3 seems to work perfectly.
Thnx so much for the help.