# Preserve mtime when using FTP?

**URL:** <https://rubytalk.org/t/preserve-mtime-when-using-ftp/32937>\
**Category:** ruby-talk\
**Created:** [14 November 2006 19:28 UTC](https://rubytalk.org/t/preserve-mtime-when-using-ftp/32937 "2006-11-14T19:28:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![greg](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@greg](https://rubytalk.org/u/greg)\
**Post date:** [14 November 2006 19:28 UTC](https://rubytalk.org/t/preserve-mtime-when-using-ftp/32937/1 "2006-11-14T19:28:32Z")

</div>

I am using Net::FTP to copy files, but the file stats are not  
preserved. In particular, I would like to preserve mtime (time of last  
modification).

There is an FTP mtime method that can get the mtime of the remote file.

Is there any way to preserve or set mtime, or is there a different  
protocol or library I should use?

Thanks,  
Greg

---

<div class="post-metadata">

**Author:** ![Paul\_Lutus](https://avatars.discourse-cdn.com/v4/letter/p/2bfe46/32.png) [@Paul\_Lutus](https://rubytalk.org/u/Paul_Lutus)\
**Post date:** [14 November 2006 19:55 UTC](https://rubytalk.org/t/preserve-mtime-when-using-ftp/32937/2 "2006-11-14T19:55:06Z")

</div>

greg wrote:

> I am using Net::FTP to copy files, but the file stats are not  
> preserved. In particular, I would like to preserve mtime (time of last  
> modification).
> 
> There is an FTP mtime method that can get the mtime of the remote file.

But FTP can't set the mtime on remote files, it can only retrieve it.

> Is there any way to preserve or set mtime, or is there a different  
> protocol or library I should use?

The FTP protocol doesn't do this, anywhere, on any platform, at all.  
Therefore Ruby FTP doesn't do it either.

Have you considered using an ssh or sftp transaction instead?

> **···**
>
> --  
> Paul Lutus  
> [http://www.arachnoid.com](http://www.arachnoid.com)

---

<div class="post-metadata">

**Author:** ![greg](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@greg](https://rubytalk.org/u/greg)\
**Post date:** [14 November 2006 21:06 UTC](https://rubytalk.org/t/preserve-mtime-when-using-ftp/32937/3 "2006-11-14T21:06:32Z")

</div>

Unless I am mistaken I don't think sftp will preserve the file  
metadata. I don't know how ssh would either. sftp and ftp use  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;File.open( local\_path, "wb" ) { |f| f.write contents }

After looking through the FileUtils source which can preserve metadata,  
I see it just uses File.utime to set the metadata, so that is what I  
will try.

---

<div class="post-metadata">

**Author:** ![Paul\_Lutus](https://avatars.discourse-cdn.com/v4/letter/p/2bfe46/32.png) [@Paul\_Lutus](https://rubytalk.org/u/Paul_Lutus)\
**Post date:** [15 November 2006 09:30 UTC](https://rubytalk.org/t/preserve-mtime-when-using-ftp/32937/4 "2006-11-15T09:30:07Z")

</div>

greg wrote:

> Unless I am mistaken I don't think sftp will preserve the file  
> metadata. I don't know how ssh would either. sftp and ftp use  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;File.open( local\_path, "wb" ) { |f| f.write contents }

Not the sftp I'm thinking of. In any case, you can use ssh to force a reset  
of the destination file state using data passed from the source. I think  
this is how "rsync" does it.

> **···**
>
> --  
> Paul Lutus  
> [http://www.arachnoid.com](http://www.arachnoid.com)
