Find files with wrong date given filename

I have a directory with files whose names are common with a suffix of increasing date.

An example is:

REUTERS.FH_lbm_dump.20120905 

representing the 5th of September.

These files are supposed to be created in the first few minutes of the next day, e.g. the above file should've been created at 00:01 on the 6th of September.

However, due to some bug with how the filenames are generated, an application has been saving them incorrectly. So files created at 00:16 on the 5th of September have the suffix 20120905, i.e. the filename is 1 day too late.

Output from ls -h showing the wrong naming:

2012-09-05 00:16 FH_lbm_dump.20120905

So logic to detect this would look at the file name, extract the date, and if its equal to the file timestamp it would be positive.

How can we do this in Bash / Perl / Python?

Answers


I will not solve your entire problem, but I'll give you a place to start. The rest is really up to you.

#!/bin/bash

# loop through all filenames in current dir
for filename in *; do

    # How to extract the date from the file name
    date_from_file=${filename:(-8)}

    # How to get the file's modification date in the same format
    date_modified=$(stat -c %y "$filename" | cut -d ' ' -f1 | sed 's/-//g')

    # test for inequality
    if [ $date_from_file -ne $date_modified ]; then

        ... # do your thing

    fi

done

Need Your Help

Jasmine Test Consecutive Promise Calls

angularjs unit-testing jasmine karma-runner

I have the following test that does the following:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.