Posts Tagged ‘tempfile’

Find files from the future and touch them

Friday, July 29th, 2011

Moving files off my old laptop I somehow managed to corrupt the Last Modified dates. Many had their dates set to Epoch, but others were set to May 2021. This was very annoying. When I would sort by Last Modified I would get all these bogus files coming up on time. So, here’s a simple bash script to find all files with last modified date’s greater than the current time and touch them so that their last modified date is rest to the current time.


#!/bin/bash

USAGE="Usage: find_and_touch_future_files [directory]"
if test -z "$1" 
then
  echo "$USAGE"
  exit 1
fi

if [ ! -d $1 ]; then
  echo "find_and_touch_future_files: $1 is not a directory"
  echo "$USAGE"
  exit 1
fi

# make a temporary file (timestamped with right now)
tmp=`tempfile`
touch $tmp
# find all files modified after temp file (right now) and touch them 
find $1 -newer $tmp -exec touch {} \;
# clean up temp file
rm $tmp