如何在保留原始文件夹结构的同时复制带有扩展名X的每个文件? (类Unix系统) [英] How to copy every file with extension X while keeping the original folder structure? (Unix-like systems)
问题描述
我正在尝试将每个HTML文件从src文件夹复制到dist文件夹.但是,我想保留原始文件夹结构,如果dist文件夹不存在,我想创建一个新文件夹.
I am trying to copy every HTML file from an src folder to a dist folder. However, I should like to preserve the original folder structure and if the dist folder does not exist I should like to create a new one.
创建文件夹(如果该文件夹不存在):
[ -d _dist/ ] || mkdir _dist/
复制每个文件:
cp -R _src/**/*.html _dist/
一起:
[ -d _dist/ ] || mkdir _dist/ && cp -R _src/**/*.html _dist/
但是,如果我使用**,则仅复制文件夹中的文件;如果删除**,则仅将复制根文件.这甚至可以实现吗?
However, if I use ** only the files inside a folder will get copied and if I remove the ** only the root files will get copied. Is this even accomplishable?
推荐答案
find _src -type f -name "*.html" -exec cp -t _dist --parents {} ";"
- cp -t:目标目录
- -父母:将父母目录附加到目标
这将省略空的(没有html文件)目录.但是,如果需要它们,请在不使用-name"* .html"的情况下使用-type d重复.
This will omit empty (no html-files) dirs. But if you need them, repeat without -name "*.html" but with -type d.
这篇关于如何在保留原始文件夹结构的同时复制带有扩展名X的每个文件? (类Unix系统)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!