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).
Asked
Active
Viewed 3,651 times
2 Answers
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.

user2835098
- 11
- 2
-
If you find a typo in a question or an answer feel free to suggest an edit. There is no need to post the same answer again. – Gerald Schneider May 04 '22 at 07:28
-
I cannot add a comment due to having to have a reputation of at least 50, unfortunately. – user2835098 May 04 '22 at 08:30