-1

I've got a public folder that includes .html and non .html files in its root and subfolders. I need to move all non .html files to a subfolder of the public folder, e.g. to public/assets, preserving their full path.

So far I got something like this:

mkdir -p public/assets
find public -type f -not -name "*.html" -print0 | xargs -0I{} mv {} public/assets/

It doesn't work correctly, because it doesn't move files with full path, e.g. if there's a file public/foo/bar.js, it will be moved to public/assets/bar.js and not into public/assets/foo/bar.js.

I also tried another version:

mkdir -p public/assets
find public -type f -not -name "*.html" | sed 's#\(public\)\(.*\)#\1\2 public/assets\2#' | xargs -I% mv %

but it complains about incorrect syntax of mv command. I pass only one parameter to it, but after running sed it consists of 2 paths separated with one space, so I hoped it will treat it as 2 parameters.

It would be also great, if there was a way to remove empty folders after moving all files, but it's not necessary.

szimek
  • 6,404
  • 5
  • 32
  • 36

1 Answers1

1

I suggest using rsync for this. Also, I would use a folder in a different location as destination. That folder can then be copied back to your public folder.

mkdir /tmp/assets

rsync -r --include="*/" --exclude="*.html" --include="*" --prune-empty-dirs public/* /tmp/assets

mv /tmp/assets public/assets
Mihai
  • 9,526
  • 2
  • 18
  • 40
  • Thanks! I had to add `--remove-source-files` to move files. If I add `--exclude=public/assets/*` would it be possible to not create the temp folder? – szimek Mar 31 '19 at 12:24
  • 1
    Yes, it could work. However, I would recommend to work with a different directory. It is an anti-pattern to change the source while you are still analyzing it. It will save you trouble later on – Mihai Mar 31 '19 at 13:21