简体   繁体   English

“查找”目录下的所有.log文件,将它们复制到以其父目录命名的另一个目录?

[英]'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.

相关问题 如果文件不在另一个目录中,则将文件复制到目录 - Copy files to directory if they are not in another directory 如何在父目录linux的子目录中查找和复制文件 - how to find and copy files in a sub directory from parent directory linux 查找目录中包含的所有文件 - Find all files contained into directory named 在linux中,我怎样才能把zip具体的子目录改成自己的zip文件名,把父目录名和Z78E6221F6393D1356DZF2Z文件全部改成a393D1356DZF21的单目录? - In linux, how can I zip specific sub directories into their own zip files named parent directory name and output them all into a single directory? 如何查找一个用户拥有的所有文件并将它们复制到 RHEL 8 中的另一个目录? - How to find all file owned by one user and copy them to another directory in RHEL 8? 查找所有扩展名为.sh .cpp .c的文件,并将其复制到我的桌面目录中,如果存在同名文件,则将其重命名 - find all files with .sh .cpp .c extension and copy them to a directory in my desktop, if there is a file with the same name, rename it 如何在列表中查找包含使用部分名称的目录和子目录中的所有文件,然后将它们复制到新文件夹 - How to find all files in a directory and subdirectories that contain using partial names within a list then copy them to a new folder 如何按文件类型递归查找文件并将它们复制到目录? - How to find files recursively by file type and copy them to a directory? linux查找文件并复制到目录 - linux find files and copy to directory 找到一个文件并将其复制到另一个目录 - find a file and copy it to another directory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM