1

Is there a command to only copy files before a certain date, say 20120901? Furthermore, I would like to do it with cp -p capability (e.g. the original timestamp is preserved).

user788171
  • 279
  • 1
  • 5
  • 13

2 Answers2

6

Yes, you should be able to do it with a combination of touch and find.

# Create a file with the desired timestamp
touch -d 20120901 /tmp/timefile

# Find all files older than that and act on them
find $PATH -type -f -and -not -newer /tmp/timefile -print0 | xargs -0 -i % cp -p % /new/location

So what this does is find all files underneath the directory $PATH that have been modified before September 1st, 2012 at 00:00:00 hours and copy them all to the directory /new/location

Scott Pack
  • 14,907
  • 10
  • 53
  • 83
0

There's a typo in the accepted answer. The -type should be followed by f and not -f. Also that expression didn't work for me.

This did work for me:

find <path> -type f -and -not -newer /tmp/timefile | xargs -I  '{}' cp -p '{}' <path/to/destination/directory>

To find files newer than the timestamped file, do:

find <path> -type f -and -newer /tmp/timefile | xargs -I  '{}' cp -p '{}' <path/to/destination/directory>

Additionally, if you want to filter based on a specific hour, minute and second, modify touch part as follows

touch -t [[CC]YY]MMDDhhmm[.SS] /tmp/timefile

e.g.

touch -t 202205040930.00 /tmp/timefile

Then do the find part.