Maria1995
Maria1995

Reputation: 491

Copy recursive files of all the subdirectories

I want to copy all the log files from a directory which does not contain log files, but it contains other subdirectories with log files. These subdirectories also contain other subdirectories, so I need something recursive.

I tried

cp -R *.log /destination

But it doesn't work because the first directory does not contains log files. The response can be also a loop in bash.

Upvotes: 0

Views: 69

Answers (1)

Slawomir Dziuba
Slawomir Dziuba

Reputation: 1325

find /path/to/logdir  -type f -name "*.log"  |xargs -I {}  cp {} /path/to/destinationdir

Explanation:

find searches recursively
-type f tells you to search for files
-name specifies the name pattern
xargs executes commands
-I {} indicates an argument substitution symbol

Another version without xargs:

find /path/to/logdir -type f -name '* .log' -exec cp '{}' /path/to/destinationdir \; 

Upvotes: 1

Related Questions