-1

I'm trying to copy files by mask with preserving folder structure(using --parents), I can't use cp -r --parents or rsync directly because of argument list too long error.

ls folder1/folder2/ | head | xargs -I {} cp -r --parents folder1/folder2/{}/neutral* neutral_data/

but seems asterix symbols don't work here as expected, instead I get few errors like:

cp: cannot stat 'folder1/folder2/folder3/neutral*': No such file or directory

What is the proper way of using asterix symbol in this context or maybe any other method to solve this problem?

Update:

Based on this answer https://unix.stackexchange.com/a/5247/221416 I tried

ls folder1/folder2/ | head | xargs -I {} sh -c cp -r --parents folder1/folder2/{}/neutral* neutral_data/

but it gives error:

cp: missing file operand
mrgloom
  • 20,061
  • 36
  • 171
  • 301
  • 1
    Use `rsync` instead of `ls`, `xargs`, `cp`. – nwinkler May 18 '20 at 14:19
  • I can't use `cp -r --parents` or `rsync` directly because of `argument list too long` error. – mrgloom May 18 '20 at 14:21
  • 1
    [Why *not* parse `ls`?](http://unix.stackexchange.com/questions/128985/why-not-parse-ls) – Cyrus May 18 '20 at 16:35
  • 1
    Using `ls` is almost always a bad strategy, because of spaces and other special characters in file/folder names, etc. There should be no problem at all doing that with `rsync`. What exactly are your criteria? Which files/folders should be included or excluded? How much of the source directory structure do you want to reproduce in the destination (the whole source path, or only the last part(s) of it)? You can use `--relative` with rsync to determine exactly what part of the source path needs to be preserved. – mivk May 21 '20 at 15:00

1 Answers1

-1

Here is a solution:

ls folder1/folder2/ | xargs -I {} bash -c "cp -r --parents folder1/folder2/{}/neutral* neutral_data/"
mrgloom
  • 20,061
  • 36
  • 171
  • 301