Home > Software design >  Copy all files of a particular extension into a separate folder, retaining folder structure (recursi
Copy all files of a particular extension into a separate folder, retaining folder structure (recursi

Time:11-20

Simple task: I have a nested folder of arbitrary code, containing CSS files in any particular folder. I want a command that copies over those files (and only those files) into a new folder, reproducing the same folder structure that they were in.

Either a shell command or node command would be fine.

e.g. given this:

src/folder1/file1.css
src/folder1/... other files...
src/folder2/subfolder/file2.css
src/folder2/subfolder/X/Y/Z/file.css
src/folder3/whatever.html
src/folder4/blah.html

the command would generate the following:

dist/folder1/file1.css
dist/folder2/subfolder/file2.css
dist/folder2/subfolder/X/Y/Z/file.css

Any idea?

CodePudding user response:

You can use rsync:

rsync -avm --include '*/' --include '*.css' --exclude '*' src/ dist
  • -a is archive mode: recurse, preserve attributes, permissions etc
  • -m is prune: remove directories not required for a .css path
  • -v is verbose logging

CodePudding user response:

I hope that this helps you.

cd src && find . -name \*\.css -or -name \*\.html | while read src; do srcdir=$(echo "$src" | rev | cut -d'/' -f2- | rev); dstdir="../dist/$srcdir"; [ -d "$dstdir" ] || mkdir -p "$dstdir"; cp "$src" "../dist/$src"; done
  • Related