Find files from the future and touch them

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

Tags: , , ,

7 Responses to “Find files from the future and touch them”

  1. James says:

    Well done!

    I’m searching for a windows compatible script that will do this.

    Do you know of one?

    Many thanks!

    James

  2. ajx says:

    No idea.

    You could always install cygwin and run this script.

  3. Ilya Evseev says:

    Cool. Works. Thanx!

  4. boz says:

    Ran into a similar need, under similar circumstances. atime is corrupt, but ctime appears to be accurate. As these are pictures, would be useful to set atime to ctime for each ‘file from the future'; instead of just touching the file to now.

    I’m too tired right now to look at modding your script , but wanted to thank you for pointing me in the right direction.

  5. Jason says:

    This was very helpful! Thanks for sharing.

  6. Teemu says:

    Great, first link from Google search and exactly what I needed. Thanks.

  7. David says:

    I have been overlooking the ‘-newer’ switch. I just now fixed my file system to my satisfaction (no future dates), which had been bugging me for a while now. Thank you!

Leave a Reply