How to find newest file with given name?

This post will probably be boring for you, but this is mostly just a reminder to myself, written in form of a blog post.

So, I have a directory structure: /some/path/imported/DATE/TIME/file, where DATE is date of importing, in format YYYY-MM-DD, and TIME is time of importing, in format HHMMSS.

So, example paths look like this:


As you can see some of the files were imported many times.

Now, I need to find the latest import of given file.

So, I need a way to convert above list into:


Of course – with 10 imports, it's simple. But what if I had 10000 of them?

Luckily, it is rather simple:

find . -mindepth 3 -maxdepth 3 -exec basename {} \; | \
    sort -u | \
    while read DIR; \
    do \
        find . -name "$DIR" | \
        sort | \
        tail -n 1; \

Of course I typed it originally as one-liner 🙂

While writing the post I realized I could do better:

find . -mindepth 3 -maxdepth 3 | \
    sort -r -t/ -k4,4 -k2,2 | \
    awk -F/ 'BEGIN{prev="/"} ($4!=prev) {print $0; prev=$4}'

Well. I understand the code, and what it does, but it doesn't change the fact that I'm not really fan of shell programming.

6 thoughts on “How to find newest file with given name?”

  1. I don’t really like the term “shell programming”. You’d rather say “makeing your life easier with shell”.

  2. @pulczynski:
    to learn new things. Using perl is possible, but there are other tools that can be used to get the thing I want. And learning them now, can (and usually is) beneficial in the future.

  3. @jolsz:

    According to Merriam-Webster dictionary:

    1. programming: the planning, scheduling, or performing of a program
    2. program: a sequence of coded instructions that can be inserted into a mechanism (as a computer)

  4. Well, if we stick to these definitions you are right. Anyway after using Linux quite a long time opening shell window (zsh in my case :-)) and typing in some sequence of commands (usually some combination of fidn, awk, sed or some other common commands) is such natural thing as using screwdriver. Why do I use shell? Because it make my every workday simplier and easies a lot of activities.
    In my understanding programming (to be strict “computer programming”) is more complex activity and it implies usage of some programming language (which bash is not, is it?).

Comments are closed.