Posted on

Recursively list files and sort by date modified

In my research project, I often have to quickly navigate through a large directory of study subjects subdirectories containing various files (e.g. PDFs). Finding the files you’ve recently worked on can be ‘challenging’ when the subject names and directories are randomized strings / integers. The command line interface is often the fastest and easiest way of quickly finding this information.

When I want to quickly find the files I’ve added / modified to the study subjects directory, I use the following:


# Use the following snippet of code in a directory to list files, recursively, and then sort them by date modified.

gci -file -recurse | select LastWriteTime, Fullname | sort lastwritetime -Descending

When you are confined to a Windows environment and you do not have access to Windows Subsystem for Linux (WSL) or GitBash, PowerShell may be helpful. The code snippet above, which you may find at my GitHub Gist, goes through a directory, finds all files, and sorts them by most recently modified to least recently modified.

License – Share freely and widely

Unless mentioned otherwise, the code above is free for you to use and licensed with the Unlicense License. If you do use it, please drop by my GitHub, and drop the repository a star. Any suggestions or bugs? Create a new Issue on GitHub.

Dr. Pranav Mishra

Dr. Pranav Mishra

Dr. Pranav Mishra is a physician-scientist scientist in training. He is currently a post-doctoral research fellow in the Departments of Surgery and Orthopedic Surgery at Rush University. As a hobby, he enjoys learning about programming, self-hosting, and open-source software through personal projects. This sparked his interest in data science / bioinformatics, developing reproducible and auditable environments for his research projects. You can find him listed on his website and connect with him on most socials @drpranavmishra.