Copy directories and their sub directories created day ago

Viewed 12

I tried to copy Copy directories and their subdirectories created a day ago as follows:

find /application/work/   -type d -mtime -1  -exec cp -r {} /tmp/backup \;

But it is copying all directories (Not only the ones created a day ago).

Would you please advise?

1 Answers

find is also finding the working directory /application/work/ and is copying it, see How to exclude this / current / dot folder from find "type d". Since you're executing cp -r, it recursively copies everything in . before also copying the subset of directories you've found via -mtime. You need to set the -mindepth to exclude the working directory from the paths on which find will operate.

Modify your command to:

find /application/work -mindepth 1 -type d -mtime -1 -exec cp -r {} /tmp/backup \;
Related