0

I'm looking for a command that will compress every folder within a directory separately with the name of each archive reflecting the original folder name. I know that

tar czvf example.tar.gz example/

will compress an entire folder. However, I have not found a command to bulk archive. Is there such a command?

4

3 回答 3

1
find -mindepth 1 -maxdepth 1 -type d -exec tar czf {}.tar.gz {} \;

Note that I used -maxdepth 1.

Consider the directory structure:

.
|-- a
|   `-- x
|-- b
`-- c

Without -maxdepth 1 you would get a.tar.gz and a/x.tar.gz. a.tar.gz would contain x and all of the files within; and a/x.tar.gz would contain x and its files. But this stores the items within x twice, assuming that isn't the goal.

Updated to use -mindepth 1 as well, because when run outside of example/ an example.tar.gz would be created as well.

Update ... and for bzip2:

find -mindepth 1 -maxdepth 1 -type d -exec tar cjf {}.tar.bz2 {} \;
于 2009-09-20T20:14:20.320 回答
1
for f in `find -mindepth 1 -maxdepth 1 -type d`; do
    tar -czf $f.tar.gz $f
done
于 2009-09-20T20:16:53.610 回答
0

好的,这是我想出的最后一个命令来存档然后用 bzip2 压缩以获得最大压缩

查找 -mindepth 1 -maxdepth 1 -type d -exec tar cjf '{}'.tar.bz2 '{}' \;

感谢大家!

于 2009-09-21T00:34:41.987 回答