How to copy files of particular date from one path to another path in Unix
Solution 1
The best way to do this kind of operation is using the find
command. It has lots of approaches; I will explain two of these:
1) Relative Time Intervals
In this approach, you can specify exactly the begin and end timestamps and than filter out every file not modified/accessed/changed
during that period. You can do that using the touch
command, saving a file with a particular timestamp.
In the next example we create two markers (timestamp_begin_file
and timestamp_end_file
), set to 3 HOURS AGO
and 1 HOUR AGO
. Then we user the newerct
and ! newerct
options to select files newer then 3 hours and not newer then 1 hour (we just take a look at change
time in this example). All files selected will be copied in $DESTINATION_PATH
folder.
touch -t $(date -d '3 HOUR AGO' +%Y%m%d%H%M.%S) timestamp_begin_file
touch -t $(date -d '1 HOUR AGO' +%Y%m%d%H%M.%S) timestamp_end_file
find "$FOLDER" -newerct timestamp_begin_file ! -newermt timestamp_end_file -exec cp {} $DESTINATION_PATH \;
2) Absolute Time Intervals
You can use newermt
options to filter out files you don't need in a specific interval of dates. Here we select only files modified during the day of 2007-06-07
:
find . -type f -newermt 2007-06-07 ! -newermt 2007-06-08 -exec cp {} $DESTINATION_PATH \;
As I said, lots of other alternatives exist (as @Jaleks suggested, -a/m/ctime
, too, but due to lack of information, we cannot know which case fits for you).
For more info about time filtering in find
, take a look at the manpage of find
Solution 2
Have a look at the manpage of find
, which has parameters like -atime
, -mtime
or -ctime
to find files which got accessed, modified or changed at some given time, then you can further use the -exec
option to copy these files.
Related videos on Youtube
Srinivas
Updated on September 18, 2022Comments
-
Srinivas almost 2 years
I want to copy a files of particular date from one directory to another directory. Can anyone suggest me the Unix command for that ?
-
Jeff Schaller over 7 yearsCan you narrow down your question with example dates and directories?
-