Copy Specific Files Recursively

Is it possible to only copy selected files+its directories when you are copying recursively?

find /OriginalFolder/* -type -d \{ -mtime 1 -o -mtime 2 \ } -exec cp -R {} /CopyTo/'hostname'__CopyTo/ \; -print

From the above line, I want to only copy *txt and ini files from /OriginalFolder/

Regards

Did you consider the -name or -iname test?

Yeah. I am probably doing it wrong.

I tried the following command, which didn't work:

find /OriginalFolder/* -type -d \{ -mtime 1 -o -mtime 2 -o -name *.ini -o -name *.txt \ } -exec cp -R {} /CopyTo/'hostname'__CopyTo/ \; -print

So I am trying to see which folders were modified 1 or 2 days ago and only copy over the files *.ini and *.txt (including the folder which contains them).

I'm not sure (and can't test it right now) if the -type d gets in the way as it only allows for directories and you want files...

On top, the (logical connection of the) -mtime s might not deliver what you expect it to.

The find primary -mtime 1 looks for file that were modified exactly 24 hours ago to the finest resolution of timestamps available on the filesystem you're traversing. If you want to look for files that were modified sometime up to 48 hours ago, you would do that with:

find ... -mtime -2

And, if you need to group primaries together in an expression, you need to surround them with escaped parentheses such as:

\( primary -o primary \)

not with braces such as:

\{ primary -o primary \}

and there can never be any space between the backslash character and the escaped parenthesis following it.

Maybe you want something more like:

find $(find /OriginalFolder -type -d -mtime -2) -depth \( -name '*.ini' -o -name '*.txt' \)

to get a list of absolute pathnames of the files you want to copy as long as the files you want to copy only appear in leaf directories. You'll have to do some post-processing of the output to recreate the directory hierarchy using something like cpio , pax , or tar to keep the original ownership and modes.

But, of course, this assumes that your list of directories updated in the last two days is not large enough to cause a command-line too long error as it constructs the outermost find command.

And, as RudiC noted, if a file in a directory is updated without creating a new file or deleting an old file, the directory timestamp won't be updated.

None of this has been tested, but hopefully it will give you a way to start moving forward. Note that if directories other than those that are leaves on the tree are updated, some of the files in the list produced might not actually be in a directory whose leaf directory changed in the last two days AND if non-leaf directories are updated some files may appear in the output list more than one time so you may need to weed out duplicates.