首页 > 解决方案 > 从特定目录开始复制文件路径 - Bash

问题描述

如何使用 Bash 从特定目录开始复制文件(包括它们的文件夹层次结构)?

例如,假设我有以下文件:

我想复制/first/ 目录的文件(1.txt、2.txt、3.txt)路径,意思是结果应该是:

使用以下命令,但不幸的是它包含了包括“第一个”目录在内的整个路径,因此需要进行一些调整:

find . -type f -name "*.txt" -exec cp --parent {} destination_folder/

有什么建议么?

标签: linuxbashdirectorycopyfilesystems

解决方案


cd first如果以前无法做到find,请尝试在以下情况下进行-exec

find . -type f -name "*.txt" -exec bash -c 'cd first; echo cp --parent "${1#./*/}" ../destination_folder/' _ {} \;

推荐阅读