Change last modified date of files based on date mentioned in file

A clients older search routines used the last modified date of files as the actual date the file was created. This became problematic when we had to rebuild the collection of files. The last modified date became the date we touched the files.

A simple Bash script to the rescue. For this script we are using an email header in the file called X-Date. Your use may vary:

#!/bin/bash
#
# Change date on mail archives to match that in the header X-Date
# Arguments: path
if [ $# -ne 1 ]
then
    echo "Supply path to where files to convert live."
    exit 2
fi
FIND='/bin/find'
AWK='/bin/awk'
TOUCH='/bin/touch'
GREP='/usr/bin/grep'
PATH=$1
for i in $($FIND $PATH -name "*.php");
do
    touchdate=$($GREP 'X-Date' --binary-files=text $i | $AWK '{print $3" "$4" "$5}');
    if [ "$touchdate" != "" ];
    then
        echo "$i - $touchdate";
        $TOUCH -d "$touchdate" $i;
    fi;
done

Improvements are welcome in the comments below.

Read more…

Comments