DIR and howto filter to find newest file only

Posted on 2005-04-16
Medium Priority
Last Modified: 2012-05-05
Hey all,

I need to perform a dir style command on a directory, which would return only the newest file in the directory.

I need to be able to search based on wildcards also, so using dir and piping the results to another command may be possible (awk maybe?).

directory example:
test.exe 3/3/04
file.exe 2/10/04

dir *.exe | somefilter

Any suggestions?

Question by:Dodger42
  • 3
  • 3
LVL 10

Expert Comment

ID: 13797388
what os ?
LVL 10

Expert Comment

ID: 13797413
ls -t *.exe | head -1

if it's some sort of unix

LVL 10

Expert Comment

ID: 13797455
if its windows, install unix tools for windows and then

dir /o-d *.exe | head 1

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 22

Expert Comment

ID: 13808771
I'm guessing that this is a windows system due to the *.exe extensions.

You could of course go down the unix-tools, uwin, cygwin suites:


route which installs all the tools required for this and more!.....or we can be a little more specific:)...

Lets just use the gawk (gnu awk as you seem to like that) tools package for windows:


and then you can do:

dir /TC

Which sorts according to creation date.

Then pipe it through gawk:

dir /TC | gawk 'NR==3 {print $0}'

Which prints the whole line for the 3rd line in the directory listing (because the 1st and 2nd lines are . and ..)


will give you an idea of the 'classic' 1 line filters.

gawk is equivalent to awk in virtually all respects.

LVL 22

Accepted Solution

pjedmond earned 750 total points
ID: 13808843
..and for the wildcards, theycan be as part of the dir command for say *.exe files:

dir /TC *.exe

or part of the gawk command for lines containing a t.

gawk '/t/ {print $0}'


Expert Comment

ID: 13811381
you pipe in NT based systems with |

so to list some files with a special date use the following example

dir *.exe | findstr 28.01.2005
LVL 22

Expert Comment

ID: 14058729
Hmm - being slightly biased!, I'd say points to me:) I'd say I've provided the fully comprehensive links to unixtools, uwin, and cygwin, but I've also provided the simplest 'minimalistic' solution using the suggested awk approach....and provided ample expansion on the link, not only for wildcards as suggested, but also, direction on the 'classic' oneliners that gives awk the power that it has:)

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many people tend to confuse the function of a virus with the one of adware, this misunderstanding of the basic of what each software is and how it operates causes users and organizations to take the wrong security measures that would protect them ag…
As the title indicates, I have done this before. It chills me everytime I update the OS on my phone, (http://www.experts-exchange.com/articles/18084/Upgrading-to-Android-5-0-Lollipop.html) because one time I did this and I essentially had a bricked …
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.
Suggested Courses

755 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question