Search code examples
linuxlistdatels

Linux: List file names, if last modified between a date interval


I have 2 variables, which contains dates like this: 2001.10.10

And i want to use ls with a filter, that only list files if last modified were between the first and second date


Solution

  • Sorry, it is not the simplest. I just now developed it, only for you. :-)

    ls -l --full-time|awk '{s=$6;gsub(/[-\.]/,"",s);if ((s>="'"$from_variable"'") && (s<="'"$to_variable"'")) {print $0}}';
    

    The problem is, that these simple commandline tools doesn't handle date type. So first we convert them to integers removing the separating "-" and "." characters (by you is it ".", by me a "-" so I remove both, this can you see in

    gsub(/[-\.]/,"",s)
    

    After the removal, we can already compare them with integers. In this example, we compare them with the integers $from_variable and with $to_variable. So, this will list files modified between $from_variable and $to_variable .

    Both of "from_variable" and "to_variable" need to be environment variables in the form 20070707 (for 7. July, 2007).