Over time, I've encountered the same pattern again and again: I have some kind of directory structure:
example/
├── a
│ └── c
│ ├── d.txt (120k)
│ └── e.txt (60k)
└── b
└── f.txt (280k)
And I want to "copy" the files over to another directory, say, example_grepped
, applying a command to each as if in place of cp
- say, grep ERROR
so that say, I end up with a folder with the same structure but with files filtered through the grep
.
example_grepped/
├── a
│ └── c
│ ├── d.txt (1k)
│ └── e.txt (0b)
└── b
└── f.txt (12k)
Same pattern for converting media files (FLACs to MP3s, PNGs to JPGs), and this time when converting different schema formats as part of a build process.
Is there a generic command that I could use? Something like foobar example example_grepped --command 'grep ERROR'
or foobar flacs mp3s --command 'ffmpeg -i {} {}.mp3'
?
An obscure xargs
flag perhaps? (a find
piped through xargs
would almost suffice, but most if not all commands expect the directory structure to already exist.)
find
withcpio
.cd /path/to/example && find . -type d|cpio -pdv /path/to/example_grepped
. (Omit-v
if you don't want the directories to be listed while processing.) After this you can usefind
withxargs
as proposed in the question.