首页 > 解决方案 > 复制所有子目录的递归文件

问题描述

我想从一个不包含日志文件的目录中复制所有日志文件,但它包含其他带有日志文件的子目录。这些子目录还包含其他子目录,所以我需要一些递归的东西。

我试过了

cp -R *.log /destination

但它不起作用,因为第一个目录不包含日志文件。响应也可以是 bash 中的循环。

标签: linuxbashshell

解决方案


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

解释:

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

另一个没有 xargs 的版本:

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

推荐阅读