bashbzip

How do I bzip archive specific files recursively in a directory without archiving the files themselves in bash?


Just as the title says, I'm trying to create a script that will take a directory as input bzip the files within the directory while preserving the directories themselves into another directory taken as input. So, for example:

$ tree testdir
testdir
├── foo.bar
├── hello.bar
├── hmm
│   ├── down.ov
│   ├── hulk.bar
│   ├── nope
│   │   ├── around.zap
│   │   ├── over.bar
│   │   ├── side.it
│   │   └── under.bar
│   ├── smash.er
│   └── up.bar
├── no.wsx
├── test.bar
└── yes.lol
$ ./script testdir testdir2
$ tree testdir2
testdir2
├── foo.bar.bz2
├── hello.bar.bz2
├── hmm
│   ├── down.ov.bz2
│   ├── hulk.bar.bz2
│   ├── nope
│   │   ├── around.zap.bz2
│   │   ├── over.bar.bz2
│   │   ├── side.it.bz2
│   │   └── under.bar.bz2
│   ├── smash.er.bz2
│   └── up.bar.bz2
├── no.wsx.bz2
├── test.bar.bz2
└── yes.lol.bz2

I have an idea of how to do it, but it involves while loops and cd commands and whatnot, which I consider to be inefficient. So, I was hoping to get some help from you guys here. Even if you guys point me in the right direction that would be fantastic.


Solution

  • cp -R $1 $2
    find $2 -type f -exec bzip2 {} \;