0

I have a few .html files that I need to copy from one place to another.

The source folder has files other than .html, so I need to first find only the .html files and then copy them to the other directory while I maintain the same structure.

Something like this:

cp --parents `find ./src -name '*.html'` ./dist

The problem is that the files are placed inside ./dist/src/**/*.js and not ./dist/**/*.js.

So to summarize, I want to copy all of the files (while maintaining folder structure) from one folder to another without the source parent folder.

For example:

src/
  folder1/
    a.html
    a.x
  folder2/
    folder3/
      b.html

to

dist/
  folder1/
    a.html
  folder2/
    folder3/
      b.html

1 Answer 1

1
rsync -rmv --include='*/' --include='*.html' --exclude='*' src/ dest

-r to recurse, -m to prune empty directories, -v verbose. By default, rsync copies everything, so we include only files with .html suffix and exclude everything else. --include='*/' is present to force rsync to descent into directories, without it it does not copy anything. The trailing / after src is important so that src itself is not copied into dest.

Look at man rsync if you want mode, ownership, etc to be copied as well and add relevant option.

2
  • 1
    You are right. Edited. Why would I want to use -a? -r is sufficient.
    – yarl
    Jan 30, 2022 at 14:15
  • 1
    Well, it preserves much of the meta-data associated with the files that are copied, and also won't skip symbolic links and other non-regular files. It may not always be needed.
    – Kusalananda
    Jan 30, 2022 at 15:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .