如何将整个嵌套目录中的头文件仅复制到另一个目录,在复制到新文件夹后保持相同的层次结构

时间:2022-04-03 23:57:48

I have a directory which has a lot of header files(.h) and other .o and .c files and other files. This directory has many nested directories inside. I want to copy only header files to a separate directory preserving the same structure in the new directory.

我有一个目录,其中包含许多头文件(.h)和其他.o和.c文件以及其他文件。该目录里面有许多嵌套目录。我想只将头文件复制到一个单独的目录,在新目录中保留相同的结构。

cp -rf oldDirectory newDirectory will copy all files. I want to copy only header files.

cp -rf oldDirectory newDirectory将复制所有文件。我想只复制头文件。

3 个解决方案

#1


7  

(cd src && find . -name '*.h' -print | tar --create --files-from -) | (cd dst && tar xvfp -)

You can do something similar with cpio if you just want to hard link the files instead of copying them, but it's likely to require a little mv'ing afterward. If you have lots of data and don't mind (or need!) sharing, this can be much faster. It gets confused if dst needs to have a src in it - this is, if it isn't just a side effect:

你可以用cpio做类似的事情,如果你只想硬链接文件而不是复制它们,但是之后可能需要一点点mv'ing。如果您有大量数据并且不介意(或需要!)共享,这可能会更快。如果dst需要有一个src,它就会变得混乱 - 如果它不仅仅是副作用:

  1. find src -name '*.h' -print | cpio -pdlv dst
  2. 找到src -name'* .h'-print | cpio -pdlv dst

  3. mv dst/src/* dst/.
  4. mv dst / src / * dst /。

  5. rmdir dst/src

#2


1  

cp -rf oldDirectory/*.h newDirectory

or something like (depending on the actual paths)

或类似的东西(取决于实际路径)

find oldDirectory -type f -name "*.h" -print0 | xargs -file cp file newDirectory

#3


1  

this one worked for me:

这个对我有用:

rsync -avm --include='*.h' -f 'hide,! */' . /destination_dir

from here

#1


7  

(cd src && find . -name '*.h' -print | tar --create --files-from -) | (cd dst && tar xvfp -)

You can do something similar with cpio if you just want to hard link the files instead of copying them, but it's likely to require a little mv'ing afterward. If you have lots of data and don't mind (or need!) sharing, this can be much faster. It gets confused if dst needs to have a src in it - this is, if it isn't just a side effect:

你可以用cpio做类似的事情,如果你只想硬链接文件而不是复制它们,但是之后可能需要一点点mv'ing。如果您有大量数据并且不介意(或需要!)共享,这可能会更快。如果dst需要有一个src,它就会变得混乱 - 如果它不仅仅是副作用:

  1. find src -name '*.h' -print | cpio -pdlv dst
  2. 找到src -name'* .h'-print | cpio -pdlv dst

  3. mv dst/src/* dst/.
  4. mv dst / src / * dst /。

  5. rmdir dst/src

#2


1  

cp -rf oldDirectory/*.h newDirectory

or something like (depending on the actual paths)

或类似的东西(取决于实际路径)

find oldDirectory -type f -name "*.h" -print0 | xargs -file cp file newDirectory

#3


1  

this one worked for me:

这个对我有用:

rsync -avm --include='*.h' -f 'hide,! */' . /destination_dir

from here