linux, macos, windows,

Find and list modified files of the last N days in Bash and PowerShell

Jan 15, 2023 · 1 min read · Post a comment

Recently, as I was troubleshooting some static files sync, I wanted to know which files were changed, updated in the last 2 weeks. As with most things, if you are not doing them routinely, you forgot them, it’s a normal, human nature right?! So, here’s a neat command that will list files older than certain period of days.

Modified, changed and updated are used interchangeably, although they differ from a filesystem standpoint.

  • Modified = data modification.
  • Changed = file metadata changed.

Prerequisites

  • Shell environment

Solution

Linux / macOS

Find all modified files in the last 14 days from /home/staticfiles directory and save the files full directory path in a tmp file:

find /home/staticfiles -type f -mtime -14 -ls > tmp

Note:

  • -mtime n: find files and directories modified exactly N days ago.
  • -mtime +n: find files and directories modified more than N days ago.
  • -mtime -n: find files and directories modified less than N days ago.

Related post: mtime timestamp and the 24h period simplified.

Windows

Find the last 5 recently modified files:

Get-ChildItem -Path "C:\Users\devcoops\staticfiles" -Recurse | Sort LastWriteTime -Descending | Select -First 5 | Get-Item | foreach { "$_" }

The parameters themselves are mostly clear, unambiguous.

Conclusion

Feel free to leave a comment below and if you find this tutorial useful, follow our official channel on Telegram.