# How to adjust file modification times?

**URL:** https://discourse.julialang.org/t/how-to-adjust-file-modification-times/52337
**Category:** General Usage
**Tags:** time, filesystem
**Created:** [December 24, 2020, 7:38pm UTC](https://discourse.julialang.org/t/how-to-adjust-file-modification-times/52337 "2020-12-24T19:38:42Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Joris\_Pinkse](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joris_pinkse/32/216398_2.png) [@Joris\_Pinkse](https://discourse.julialang.org/u/Joris_Pinkse)
#### Post date: [December 24, 2020, 7:38pm UTC](https://discourse.julialang.org/t/how-to-adjust-file-modification-times/52337/1 "2020-12-24T19:38:42Z")

</div>

How do I change the modification time of a file to a prespecified time other than by going something like run(`touch ....`) ? I know there is a touch command in Julia, but it only sets it to the current time.

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [December 24, 2020, 8:21pm UTC](https://discourse.julialang.org/t/how-to-adjust-file-modification-times/52337/2 "2020-12-24T20:21:56Z")

</div>

Seems like we could use a Julia API for [`uv_fs_utime`](http://docs.libuv.org/en/v1.x/fs.html), which is probably the function you want to call.

(You could call it yourself with `ccall` of course, but requires a bit of understanding of how Julia interacts with libuv.)

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [December 24, 2020, 8:44pm UTC](https://discourse.julialang.org/t/how-to-adjust-file-modification-times/52337/3 "2020-12-24T20:44:03Z")

</div>

This should work:

```julia
import Dates
import Base: _sizeof_uv_fs, uv_error

function setmtime(path::AbstractString, mtime::Real, atime::Real=mtime; follow_symlinks::Bool=true)
    req = Libc.malloc(_sizeof_uv_fs)
    try
        if follow_symlinks
            ret = ccall(:uv_fs_utime, Cint,
                (Ptr{Cvoid}, Ptr{Cvoid}, Cstring, Cdouble, Cdouble, Ptr{Cvoid}),
                C_NULL, req, path, atime, mtime, C_NULL)
        else
            ret = ccall(:uv_fs_lutime, Cint,
                (Ptr{Cvoid}, Ptr{Cvoid}, Cstring, Cdouble, Cdouble, Ptr{Cvoid}),
                C_NULL, req, path, atime, mtime, C_NULL)
        end
        ccall(:uv_fs_req_cleanup, Cvoid, (Ptr{Cvoid},), req)
        ret < 0 && uv_error("utime($(repr(path)))", ret)
    finally
        Libc.free(req)
    end
end

setmtime(path::AbstractString, mtime::Dates.AbstractDateTime, atime::Dates.AbstractDateTime=mtime; follow_symlinks::Bool=true) =
    setmtime(path, Dates.datetime2unix(mtime), Dates.datetime2unix(atime); follow_symlinks=follow_symlinks)

```

Might make a good PR if someone wanted to put together some documentation and a test.

---

<div class="post-metadata">

### Author: ![Joris\_Pinkse](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joris_pinkse/32/216398_2.png) [@Joris\_Pinkse](https://discourse.julialang.org/u/Joris_Pinkse)
#### Post date: [December 24, 2020, 9:00pm UTC](https://discourse.julialang.org/t/how-to-adjust-file-modification-times/52337/4 "2020-12-24T21:00:58Z")

</div>

Awesome, thank you. I’ll be happy to give the PR a go after Christmas.
