修剪目录和当前目录的文件以供 tar 使用

Pruning directories and current directory's files for use with tar

我看了很多文章,我似乎无法弄清楚,我想我是一个菜鸟。

无论如何,我有一个我想要的目录 tar,但是我想排除浅目录的文件,以及排除文件夹 "plugins"、"backups" 和 "logs" 位于浅目录中。

->

\#!/bin/bash
mkdir -p /path/to/backup/directory/`date +%d%m%y`
cd /path/to/backup/directory/`date +%d%m%y`
cd .. | find . -not \( -path plugins -prune -o -path backups -prune -o -path logs -prune \) -mindepth 1 -print0 | xargs -0 tar cpj --directory=$(cd -) -f `date +%H`.tar.gz

查找部分出了什么问题,它没有排除任何东西。这是我第 30 次(不是字面上的意思,但可能比实际 xD 更高)尝试修剪什么的,每次尝试看起来都比上一次更荒谬。

如果有人可以向我展示查找部分的解决方案,那就太好了 - 谢谢

('`' 字符在日期周围,当我尝试将它们放在那里时它只会破坏代码视图)

关于查找排除目录,试试

find . -type d \( -path plugins -o -path backups -o -path logs \) -prune -o -print

使用 --exclude=PATTERN 和 */,因为它只会捕获目录:

tar --exclude=plugins/ --exclude=logs/ --exclude=backups/ -cf /path/to/backup/directory/`date +%d%m%y`/whatever.tar [other options] */