[英]'find' all .log files under a directory, copy them to another directory named after their parent directory?
Random eg: 随机,例如:
/stack-exchange/
|
|-- stack-overflow/
| |-- questions/
| |-- people/
| |-- site.log
|-- super-user/
| |-- questions/
| |-- people/
| |-- answers.log
|-- ask-ubuntu/
| |-- questions/
| |-- people/
| |-- linux.log
|-- logs/
This is the command I'd use to find
all log files in a given directory: 这是我用来在给定目录中
find
所有日志文件的命令:
find /stack-exchange/ -type f -name "*.log"
Now, going by the example, I'd like to copy all 3 log files (site.log, answers.log & linux.log) to /stack-exchange/logs
directory and have them named after their parent directory, like so: 现在,以该示例为例,我想将所有3个日志文件(site.log,answers.log和linux.log)复制到
/stack-exchange/logs
目录,并以其父目录命名,如下所示:
/stack-exchange/
|
|-- [...]
|
|-- logs/
| |-- stack-overflow.log
| |-- super-user.log
| |-- ask-ubuntu.log
How do I do that? 我怎么做? I've tried this:
我已经试过了:
find /stack-exchange/ -type f -iname "*.log" -exec cp "{}" "../logs/$(basename "$(dirname "{}")").log" \;
I think I am close, but it doesn't work. 我想我很亲近,但是没有用。 All it does is create a
..log
file. 它所做的只是创建一个
..log
文件。 What am I doing wrong, and how do I fix it? 我在做什么错,该如何解决?
Based on this answer to the question Use current filename ("{}") multiple times in "find -exec"? 基于此问题的答案 , 在“ find -exec”中多次使用当前文件名(“ {}”)? , this works:
,这有效:
find /stack-exchange/ -type f -iname "*.log" -exec sh -c 'cp "$0" "./logs/$(basename "$(dirname "$0")").log"' {} \;
Thank you @rdupz ( see comment ), for pointing me in the right direction. 谢谢@rdupz( 请参阅注释 ),为我指明了正确的方向。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.