2

Is there a way to copy folders and its contents with specific modification date in Linux, for example I have this folder A and B it contains files with modified date 2015-01-01 to 2015-01-18, is it possible to copy folder A and B containing only files modified on 2015-01-01 to 2015-01-08?

I did some research and came up with this

find ./ -type d -exec mkdir -p {} $target{} \;
find $source -mtime +30 -exec cp -p "{}" $target \;

but after executing the 2nd line, files will be copied to the root directory on target location, not in the same structure as the source

for example i have this source directory to be copied on the target

/storage/subdir1/* (modified date range - 2015-01-01 to 2015-01-18)
/storage/subdir2/* (modified date range - 2015-01-01 to 2015-01-18)
/storage/subdir3/* (modified date range - 2015-01-01 to 2015-01-18)
/storage/subdir4/* (modified date range - 2015-01-01 to 2015-01-18)

would it be possible that in the target directory (/targetdir/) all sub directories will be created automatically and it contains only files with modified date (2015-01-01 to 2015-01-08)

John

EnglandYOW
  • 23
  • 1
  • 1
  • 4

2 Answers2

2

What you need is find.

As mentioned here, you can copy files using find like this:

find /home/shantanu/processed/ -name '*2011*.xml' -exec cp {} /home/shantanu/tosend  \;

Now what you need is to specify dates instead of a pattern in your find command, you can do it as:

find /path/to/source -newermt "Jan 1 2015" -and \! -newermt "Jan 10 2015" -exec cp {} /path/to/dest  \;
Community
  • 1
  • 1
adrin
  • 4,511
  • 3
  • 34
  • 50
2

It will work.

find A -mtime -18 -mtime +1 -exec cp \{\} B/ \;
Karthikeyan.R.S
  • 3,991
  • 1
  • 19
  • 31
  • thanks for sharing this, though i use rsync to ahieve my goal `find A/ -mtime -18 -mtime +1 -exec rsync -auvrp \{\} B/ \;` this will copy its parent directory and its sub directory containing files with specified date within its original directory. thumbs up @Karthikeyan.R.S – EnglandYOW Jan 20 '15 at 03:25