Shell 脚本删除不成对文件的最佳方法

发布于 2024-12-13 00:46:37 字数 715 浏览 1 评论 0原文

我有一组成对的文件:

/var/log/messages-20111001
/var/log/messages-20111001.hash

我已经将其中几个文件轮换掉了,现在只剩下大量 /var/log/messages-201110xx.hash 文件,其中没有关联日志。我想清理混乱,但我不确定如何删除不属于“对”的文件。我可以使用 bash 或 zsh(或者任何 LSB 工具,真的)。我需要删除所有没有关联日志的 .hash 文件。

示例

/var/log/messages-20111001.hash
/var/log/messages-20111002.hash
/var/log/messages-20111003.hash
/var/log/messages-20111004.hash
/var/log/messages-20111005
/var/log/messages-20111005.hash
/var/log/messages-20111006
/var/log/messages-20111006.hash

应简化为:

/var/log/messages-20111005
/var/log/messages-20111005.hash
/var/log/messages-20111006
/var/log/messages-20111006.hash

I have a set of files that come in pairs:

/var/log/messages-20111001
/var/log/messages-20111001.hash

I've had several of these rotate away and now I'm left with a ton of /var/log/messages-201110xx.hash files with no associated log. I'd like to clean up the mess, but I'm uncertain how to remove a file that isn't part of a "pair". I can use bash or zsh (or any LSB tool, really). I need to remove all the .hash files that don't have an associated log.

Example

/var/log/messages-20111001.hash
/var/log/messages-20111002.hash
/var/log/messages-20111003.hash
/var/log/messages-20111004.hash
/var/log/messages-20111005
/var/log/messages-20111005.hash
/var/log/messages-20111006
/var/log/messages-20111006.hash

Should be reduced to:

/var/log/messages-20111005
/var/log/messages-20111005.hash
/var/log/messages-20111006
/var/log/messages-20111006.hash

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

牵你的手,一向走下去 2024-12-20 00:46:37
for file in *.hash; do test -f "${file%.hash}" || rm -- "$file"; done
for file in *.hash; do test -f "${file%.hash}" || rm -- "$file"; done
百合的盛世恋 2024-12-20 00:46:37

像这样的东西吗?

for f in /var/log/messages-????????.hash ; do
    [[ -e "${f%.hash}" ]] || rm "$f"
done

Something like this?

for f in /var/log/messages-????????.hash ; do
    [[ -e "${f%.hash}" ]] || rm "$f"
done
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文