Get change date of a folder in Go












2














I want to get the date when a folder was renamed, from terminal it can be done using the stat command, e.g.:



> stat -x folders/folder1
File: "folders/folder1"
Size: 64 FileType: Directory
Mode: (0755/drwxr-xr-x) Uid: (2006390509/username) Gid:
(296108113/EMEADomain Users)
Device: 1,4 Inode: 2599274 Links: 2
Access: Mon Nov 12 17:59:57 2018
Modify: Mon Nov 12 14:12:20 2018
Change: Mon Nov 12 17:28:01 2018


The change date is the last date the folder's metadata was changed, which includes renaming.



Is there any way to get it with Go without using os.exec and parsing the output? os.Stat seems to provide only the last modification date which doesn't change when the folder is renamed.










share|improve this question






















  • "Is there any way to get it with Go without using os.exec and parsing the output?" I don't see any reason why not; you know the command to get the output and you know what format it's in. Are you having some trouble doing that? Can you show the code you're having trouble with?
    – Adrian
    Nov 12 '18 at 17:11










  • @Adrian it works, but I wonder if there is a better and cleaner way to get this information.
    – lawful_evil
    Nov 12 '18 at 17:13
















2














I want to get the date when a folder was renamed, from terminal it can be done using the stat command, e.g.:



> stat -x folders/folder1
File: "folders/folder1"
Size: 64 FileType: Directory
Mode: (0755/drwxr-xr-x) Uid: (2006390509/username) Gid:
(296108113/EMEADomain Users)
Device: 1,4 Inode: 2599274 Links: 2
Access: Mon Nov 12 17:59:57 2018
Modify: Mon Nov 12 14:12:20 2018
Change: Mon Nov 12 17:28:01 2018


The change date is the last date the folder's metadata was changed, which includes renaming.



Is there any way to get it with Go without using os.exec and parsing the output? os.Stat seems to provide only the last modification date which doesn't change when the folder is renamed.










share|improve this question






















  • "Is there any way to get it with Go without using os.exec and parsing the output?" I don't see any reason why not; you know the command to get the output and you know what format it's in. Are you having some trouble doing that? Can you show the code you're having trouble with?
    – Adrian
    Nov 12 '18 at 17:11










  • @Adrian it works, but I wonder if there is a better and cleaner way to get this information.
    – lawful_evil
    Nov 12 '18 at 17:13














2












2








2







I want to get the date when a folder was renamed, from terminal it can be done using the stat command, e.g.:



> stat -x folders/folder1
File: "folders/folder1"
Size: 64 FileType: Directory
Mode: (0755/drwxr-xr-x) Uid: (2006390509/username) Gid:
(296108113/EMEADomain Users)
Device: 1,4 Inode: 2599274 Links: 2
Access: Mon Nov 12 17:59:57 2018
Modify: Mon Nov 12 14:12:20 2018
Change: Mon Nov 12 17:28:01 2018


The change date is the last date the folder's metadata was changed, which includes renaming.



Is there any way to get it with Go without using os.exec and parsing the output? os.Stat seems to provide only the last modification date which doesn't change when the folder is renamed.










share|improve this question













I want to get the date when a folder was renamed, from terminal it can be done using the stat command, e.g.:



> stat -x folders/folder1
File: "folders/folder1"
Size: 64 FileType: Directory
Mode: (0755/drwxr-xr-x) Uid: (2006390509/username) Gid:
(296108113/EMEADomain Users)
Device: 1,4 Inode: 2599274 Links: 2
Access: Mon Nov 12 17:59:57 2018
Modify: Mon Nov 12 14:12:20 2018
Change: Mon Nov 12 17:28:01 2018


The change date is the last date the folder's metadata was changed, which includes renaming.



Is there any way to get it with Go without using os.exec and parsing the output? os.Stat seems to provide only the last modification date which doesn't change when the folder is renamed.







go






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 12 '18 at 17:07









lawful_evil

394415




394415












  • "Is there any way to get it with Go without using os.exec and parsing the output?" I don't see any reason why not; you know the command to get the output and you know what format it's in. Are you having some trouble doing that? Can you show the code you're having trouble with?
    – Adrian
    Nov 12 '18 at 17:11










  • @Adrian it works, but I wonder if there is a better and cleaner way to get this information.
    – lawful_evil
    Nov 12 '18 at 17:13


















  • "Is there any way to get it with Go without using os.exec and parsing the output?" I don't see any reason why not; you know the command to get the output and you know what format it's in. Are you having some trouble doing that? Can you show the code you're having trouble with?
    – Adrian
    Nov 12 '18 at 17:11










  • @Adrian it works, but I wonder if there is a better and cleaner way to get this information.
    – lawful_evil
    Nov 12 '18 at 17:13
















"Is there any way to get it with Go without using os.exec and parsing the output?" I don't see any reason why not; you know the command to get the output and you know what format it's in. Are you having some trouble doing that? Can you show the code you're having trouble with?
– Adrian
Nov 12 '18 at 17:11




"Is there any way to get it with Go without using os.exec and parsing the output?" I don't see any reason why not; you know the command to get the output and you know what format it's in. Are you having some trouble doing that? Can you show the code you're having trouble with?
– Adrian
Nov 12 '18 at 17:11












@Adrian it works, but I wonder if there is a better and cleaner way to get this information.
– lawful_evil
Nov 12 '18 at 17:13




@Adrian it works, but I wonder if there is a better and cleaner way to get this information.
– lawful_evil
Nov 12 '18 at 17:13












2 Answers
2






active

oldest

votes


















3














Change time is not accessible in os.FileInfo but can be get via os.FileInfo.Sys() which stores that data.



You can get it by



package main

import (
"fmt"
"log"
"os"
"syscall"
"time"
)

func main() {
f, err := os.Stat("your/dir")
if err != nil {
log.Fatalf("err reading: %v", err)
}

//access change time saved in os.FileInfo.Sys()
changeTime := f.Sys().(*syscall.Stat_t).Ctim

fmt.Print(time.Unix(changeTime.Unix()).String())
}


Ofcourse you need to check if f.Sys() it's proper type, but yeah syscall.Stat_t.Ctim is probably what you wanted.






share|improve this answer





















  • I think, in macOS one has to replace Ctim with Ctimespec
    – lawful_evil
    Nov 13 '18 at 11:00










  • If that's true then in f.Sys() it need to be other typ because syscall.Stat_t don't have Ctimspec. If Ctim is not accessible you need to call reflection on f.Sys() to get it type.
    – ttomalak
    Nov 13 '18 at 13:21










  • In mac syscall.Stat_t doesn't have Ctim, but has Ctimspec, it seems to be the same.
    – lawful_evil
    Nov 13 '18 at 14:43










  • Indeed golang.org/src/syscall/ztypes_darwin_amd64.go line 76 syscall.Stat_t for darwin os have Ctimespec. It's wired why they do that beetwen os'es
    – ttomalak
    Nov 13 '18 at 15:52





















1














If you're happy with calling os.Exec maybe you don't mind cross-platform issues. There was some discussion about this on a github issue a while back.



This code works for me to get the changed time anyway. Not sure how/if it'll work on Windows:



file, err := os.Open("test")
if err != nil {
panic(err)
}

stat, err := file.Stat()
sys := stat.Sys().(*syscall.Stat_t)
changedTime := time.Unix(sys.Ctim.Unix())

fmt.Println(stat.ModTime())
fmt.Println(changedTime)


When I run



mv test test1 && mv test1 test && go run main.go


It gives me:



2018-11-12 17:31:38.659095951 +0000 GMT
2018-11-12 17:57:43.042208583 +0000 GMT


Which seems to correctly reflect the time I changed the dirname, and not the creation time (as in the first date)






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53266940%2fget-change-date-of-a-folder-in-go%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    Change time is not accessible in os.FileInfo but can be get via os.FileInfo.Sys() which stores that data.



    You can get it by



    package main

    import (
    "fmt"
    "log"
    "os"
    "syscall"
    "time"
    )

    func main() {
    f, err := os.Stat("your/dir")
    if err != nil {
    log.Fatalf("err reading: %v", err)
    }

    //access change time saved in os.FileInfo.Sys()
    changeTime := f.Sys().(*syscall.Stat_t).Ctim

    fmt.Print(time.Unix(changeTime.Unix()).String())
    }


    Ofcourse you need to check if f.Sys() it's proper type, but yeah syscall.Stat_t.Ctim is probably what you wanted.






    share|improve this answer





















    • I think, in macOS one has to replace Ctim with Ctimespec
      – lawful_evil
      Nov 13 '18 at 11:00










    • If that's true then in f.Sys() it need to be other typ because syscall.Stat_t don't have Ctimspec. If Ctim is not accessible you need to call reflection on f.Sys() to get it type.
      – ttomalak
      Nov 13 '18 at 13:21










    • In mac syscall.Stat_t doesn't have Ctim, but has Ctimspec, it seems to be the same.
      – lawful_evil
      Nov 13 '18 at 14:43










    • Indeed golang.org/src/syscall/ztypes_darwin_amd64.go line 76 syscall.Stat_t for darwin os have Ctimespec. It's wired why they do that beetwen os'es
      – ttomalak
      Nov 13 '18 at 15:52


















    3














    Change time is not accessible in os.FileInfo but can be get via os.FileInfo.Sys() which stores that data.



    You can get it by



    package main

    import (
    "fmt"
    "log"
    "os"
    "syscall"
    "time"
    )

    func main() {
    f, err := os.Stat("your/dir")
    if err != nil {
    log.Fatalf("err reading: %v", err)
    }

    //access change time saved in os.FileInfo.Sys()
    changeTime := f.Sys().(*syscall.Stat_t).Ctim

    fmt.Print(time.Unix(changeTime.Unix()).String())
    }


    Ofcourse you need to check if f.Sys() it's proper type, but yeah syscall.Stat_t.Ctim is probably what you wanted.






    share|improve this answer





















    • I think, in macOS one has to replace Ctim with Ctimespec
      – lawful_evil
      Nov 13 '18 at 11:00










    • If that's true then in f.Sys() it need to be other typ because syscall.Stat_t don't have Ctimspec. If Ctim is not accessible you need to call reflection on f.Sys() to get it type.
      – ttomalak
      Nov 13 '18 at 13:21










    • In mac syscall.Stat_t doesn't have Ctim, but has Ctimspec, it seems to be the same.
      – lawful_evil
      Nov 13 '18 at 14:43










    • Indeed golang.org/src/syscall/ztypes_darwin_amd64.go line 76 syscall.Stat_t for darwin os have Ctimespec. It's wired why they do that beetwen os'es
      – ttomalak
      Nov 13 '18 at 15:52
















    3












    3








    3






    Change time is not accessible in os.FileInfo but can be get via os.FileInfo.Sys() which stores that data.



    You can get it by



    package main

    import (
    "fmt"
    "log"
    "os"
    "syscall"
    "time"
    )

    func main() {
    f, err := os.Stat("your/dir")
    if err != nil {
    log.Fatalf("err reading: %v", err)
    }

    //access change time saved in os.FileInfo.Sys()
    changeTime := f.Sys().(*syscall.Stat_t).Ctim

    fmt.Print(time.Unix(changeTime.Unix()).String())
    }


    Ofcourse you need to check if f.Sys() it's proper type, but yeah syscall.Stat_t.Ctim is probably what you wanted.






    share|improve this answer












    Change time is not accessible in os.FileInfo but can be get via os.FileInfo.Sys() which stores that data.



    You can get it by



    package main

    import (
    "fmt"
    "log"
    "os"
    "syscall"
    "time"
    )

    func main() {
    f, err := os.Stat("your/dir")
    if err != nil {
    log.Fatalf("err reading: %v", err)
    }

    //access change time saved in os.FileInfo.Sys()
    changeTime := f.Sys().(*syscall.Stat_t).Ctim

    fmt.Print(time.Unix(changeTime.Unix()).String())
    }


    Ofcourse you need to check if f.Sys() it's proper type, but yeah syscall.Stat_t.Ctim is probably what you wanted.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 12 '18 at 17:40









    ttomalak

    676110




    676110












    • I think, in macOS one has to replace Ctim with Ctimespec
      – lawful_evil
      Nov 13 '18 at 11:00










    • If that's true then in f.Sys() it need to be other typ because syscall.Stat_t don't have Ctimspec. If Ctim is not accessible you need to call reflection on f.Sys() to get it type.
      – ttomalak
      Nov 13 '18 at 13:21










    • In mac syscall.Stat_t doesn't have Ctim, but has Ctimspec, it seems to be the same.
      – lawful_evil
      Nov 13 '18 at 14:43










    • Indeed golang.org/src/syscall/ztypes_darwin_amd64.go line 76 syscall.Stat_t for darwin os have Ctimespec. It's wired why they do that beetwen os'es
      – ttomalak
      Nov 13 '18 at 15:52




















    • I think, in macOS one has to replace Ctim with Ctimespec
      – lawful_evil
      Nov 13 '18 at 11:00










    • If that's true then in f.Sys() it need to be other typ because syscall.Stat_t don't have Ctimspec. If Ctim is not accessible you need to call reflection on f.Sys() to get it type.
      – ttomalak
      Nov 13 '18 at 13:21










    • In mac syscall.Stat_t doesn't have Ctim, but has Ctimspec, it seems to be the same.
      – lawful_evil
      Nov 13 '18 at 14:43










    • Indeed golang.org/src/syscall/ztypes_darwin_amd64.go line 76 syscall.Stat_t for darwin os have Ctimespec. It's wired why they do that beetwen os'es
      – ttomalak
      Nov 13 '18 at 15:52


















    I think, in macOS one has to replace Ctim with Ctimespec
    – lawful_evil
    Nov 13 '18 at 11:00




    I think, in macOS one has to replace Ctim with Ctimespec
    – lawful_evil
    Nov 13 '18 at 11:00












    If that's true then in f.Sys() it need to be other typ because syscall.Stat_t don't have Ctimspec. If Ctim is not accessible you need to call reflection on f.Sys() to get it type.
    – ttomalak
    Nov 13 '18 at 13:21




    If that's true then in f.Sys() it need to be other typ because syscall.Stat_t don't have Ctimspec. If Ctim is not accessible you need to call reflection on f.Sys() to get it type.
    – ttomalak
    Nov 13 '18 at 13:21












    In mac syscall.Stat_t doesn't have Ctim, but has Ctimspec, it seems to be the same.
    – lawful_evil
    Nov 13 '18 at 14:43




    In mac syscall.Stat_t doesn't have Ctim, but has Ctimspec, it seems to be the same.
    – lawful_evil
    Nov 13 '18 at 14:43












    Indeed golang.org/src/syscall/ztypes_darwin_amd64.go line 76 syscall.Stat_t for darwin os have Ctimespec. It's wired why they do that beetwen os'es
    – ttomalak
    Nov 13 '18 at 15:52






    Indeed golang.org/src/syscall/ztypes_darwin_amd64.go line 76 syscall.Stat_t for darwin os have Ctimespec. It's wired why they do that beetwen os'es
    – ttomalak
    Nov 13 '18 at 15:52















    1














    If you're happy with calling os.Exec maybe you don't mind cross-platform issues. There was some discussion about this on a github issue a while back.



    This code works for me to get the changed time anyway. Not sure how/if it'll work on Windows:



    file, err := os.Open("test")
    if err != nil {
    panic(err)
    }

    stat, err := file.Stat()
    sys := stat.Sys().(*syscall.Stat_t)
    changedTime := time.Unix(sys.Ctim.Unix())

    fmt.Println(stat.ModTime())
    fmt.Println(changedTime)


    When I run



    mv test test1 && mv test1 test && go run main.go


    It gives me:



    2018-11-12 17:31:38.659095951 +0000 GMT
    2018-11-12 17:57:43.042208583 +0000 GMT


    Which seems to correctly reflect the time I changed the dirname, and not the creation time (as in the first date)






    share|improve this answer


























      1














      If you're happy with calling os.Exec maybe you don't mind cross-platform issues. There was some discussion about this on a github issue a while back.



      This code works for me to get the changed time anyway. Not sure how/if it'll work on Windows:



      file, err := os.Open("test")
      if err != nil {
      panic(err)
      }

      stat, err := file.Stat()
      sys := stat.Sys().(*syscall.Stat_t)
      changedTime := time.Unix(sys.Ctim.Unix())

      fmt.Println(stat.ModTime())
      fmt.Println(changedTime)


      When I run



      mv test test1 && mv test1 test && go run main.go


      It gives me:



      2018-11-12 17:31:38.659095951 +0000 GMT
      2018-11-12 17:57:43.042208583 +0000 GMT


      Which seems to correctly reflect the time I changed the dirname, and not the creation time (as in the first date)






      share|improve this answer
























        1












        1








        1






        If you're happy with calling os.Exec maybe you don't mind cross-platform issues. There was some discussion about this on a github issue a while back.



        This code works for me to get the changed time anyway. Not sure how/if it'll work on Windows:



        file, err := os.Open("test")
        if err != nil {
        panic(err)
        }

        stat, err := file.Stat()
        sys := stat.Sys().(*syscall.Stat_t)
        changedTime := time.Unix(sys.Ctim.Unix())

        fmt.Println(stat.ModTime())
        fmt.Println(changedTime)


        When I run



        mv test test1 && mv test1 test && go run main.go


        It gives me:



        2018-11-12 17:31:38.659095951 +0000 GMT
        2018-11-12 17:57:43.042208583 +0000 GMT


        Which seems to correctly reflect the time I changed the dirname, and not the creation time (as in the first date)






        share|improve this answer












        If you're happy with calling os.Exec maybe you don't mind cross-platform issues. There was some discussion about this on a github issue a while back.



        This code works for me to get the changed time anyway. Not sure how/if it'll work on Windows:



        file, err := os.Open("test")
        if err != nil {
        panic(err)
        }

        stat, err := file.Stat()
        sys := stat.Sys().(*syscall.Stat_t)
        changedTime := time.Unix(sys.Ctim.Unix())

        fmt.Println(stat.ModTime())
        fmt.Println(changedTime)


        When I run



        mv test test1 && mv test1 test && go run main.go


        It gives me:



        2018-11-12 17:31:38.659095951 +0000 GMT
        2018-11-12 17:57:43.042208583 +0000 GMT


        Which seems to correctly reflect the time I changed the dirname, and not the creation time (as in the first date)







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 12 '18 at 18:01









        mickadoo

        2,0241429




        2,0241429






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53266940%2fget-change-date-of-a-folder-in-go%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Florida Star v. B. J. F.

            Error while running script in elastic search , gateway timeout

            Adding quotations to stringified JSON object values