2

How do I create a cron job to copy all files that are new within the 1 day span (each day at midnight)? So essentially all files created from the day of to the new folder with all permissions, date attributes, owner/group, in tact.

All files from /var/www/folder1/ to /var/www/folder2/

using crontab -e

Essentially, I am looking for the parameters that can be used to copy files daily from one folder to the other keeping all attributes intact.

Linux: UBUNTU 10.04 lts

2 Answers 2

11

cron only executes commands at a given time. In order to do what you want, you need to figure out a command that will do what you want, and then execute it with cron at a given time.

For example, to simply copy the files from one location to another, you could use

rsync -a /origin /destination

and then schedule it to run with cron by running crontab -e and specifying

0 0 * * * /usr/bin/rsync -a /origin /destination

in the file. That will cause your rsync to run at midnight each day.

Doing that each day will keep the two directories in sync. If you want to only copy files that have been created in the last day, that's a bit more difficult, but can be done with find with the --newer and -exec option to run a cp to copy the files.

3
  • By keeping them in sync, will this copy all the files or does it know to only copy files that are not existing? Apr 16, 2011 at 21:29
  • @Ubuntu User - by "keeping them in sync" I mean it will make the destination directory an exact copy of the source. Rsync will only copy the files that have changed, including new ones.
    – malcolmpdx
    Apr 18, 2011 at 13:46
  • One quick modification to the code: is you need to place / after the origin so you only move the files under the origin and not the entire folder. So it would look like /origin/ /destination Apr 24, 2011 at 5:39
0

So, something like this, bear in mind this is find on linux [find . -ctime 1 -print | xargs] will get you files dated within last day, basically 1*24hrs. You can do as you please with this filelist. Of course, rsync is a way to go, as suggested earlier. That's what rsync is for, maintaining mirrored directory structures.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .