[Golang] Find Files Older Than One Day

Find files of which modified time is older than one day in Go.

  1. Use ioutil.ReadDir or filepath.Walk to find files: if you want to find all files including sub-directories, use filepath.Walk. If you just want files in a directory but not in sub-directories, use ioutil.ReadDir. Here we use ioutil.ReadDir in our example.
  2. use time.Now() to get current time and use func (Time) Sub to perform subtraction of current time and modified time of file. If the duration is greater than 24 hours, than it's more than one day.

The following code is the implementation of above idea:

import (

func isOlderThanOneDay(t time.Time) bool {
      return time.Now().Sub(t) > 24*time.Hour

func findFilesOlderThanOneDay(dir string) (files []os.FileInfo, err error) {
      tmpfiles, err := ioutil.ReadDir(dir)
      if err != nil {

      for _, file := range tmpfiles {
              if file.Mode().IsRegular() {
                      if isOlderThanOneDay(file.ModTime()) {
                              files = append(files, file)

Tested on: Ubuntu Linux 17.10, Go 1.10


[2]How To Find And Delete Files Older Than X Days In Linux - OSTechNix
[3]10 Examples of find command in Unix and Linux | System Code Geeks - 2018
[4]Mocking system time in tests : golang