您目前的位置: 消息与通知 > 行业资讯

linux清理垃圾的脚本有哪些?

发布于 2023-07-25 14:04:09  来源:衡天主机  作者:衡天编辑组

在Linux中,可以使用脚本来清理垃圾文件。以下是一些常见的清理垃圾的脚本示例:


清理临时目录:清理/tmp和/var/tmp目录中的临时文件。

shell<button class="flex ml-auto gap-2" style="--tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; --tw-rotate: 0; --tw-skew-x: 0; --tw-skew-y: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-ring-offset-width: 0px; --tw-ring-offset-color: #fff; --tw-ring-color: rgba(59,130,246,0.5); --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-shadow: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-shadow-colored: 0 0 transparent; touch-action: manipulation; margin: 0px 0px 0px auto; color: #e8e8e8; font-size: 12px; font-family: inherit; line-height: inherit; overflow: visible; appearance: button; font-weight: inherit; padding: 0px; background-image: none; cursor: pointer; display: flex; gap: 0.5rem; background-color: rgba(255, 0, 0, 0); border: 0px solid #d9d9e3;"><textarea style="box-sizing: border-box; border-style: initial; border-color: white; --tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; --tw-rotate: 0; --tw-skew-x: 0; --tw-skew-y: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-ring-offset-width: 0px; --tw-ring-offset-color: #fff; --tw-ring-color: rgba(59,130,246,0.5); --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-shadow: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-shadow-colored: 0 0 transparent; appearance: none; touch-action: manipulation; margin: 0px; color: rgba(255, 255, 255, 0); font-size: 1rem; font-family: inherit; line-height: 1.5rem; overflow: hidden; resize: none; font-weight: inherit; padding: 0.5rem 0.75rem; background-color: rgba(255, 0, 0, 0); border-radius: 0px; z-index: -9; width: 0px; height: 0px; position: absolute;"></textarea>复制</button>
#!/bin/bash
rm -rf /tmp/*
rm -rf /var/tmp/*

清理日志文件:清理/var/log目录下的旧日志文件。

shell<button class="flex ml-auto gap-2" style="--tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; --tw-rotate: 0; --tw-skew-x: 0; --tw-skew-y: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-ring-offset-width: 0px; --tw-ring-offset-color: #fff; --tw-ring-color: rgba(59,130,246,0.5); --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-shadow: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-shadow-colored: 0 0 transparent; touch-action: manipulation; margin: 0px 0px 0px auto; color: #e8e8e8; font-size: 12px; font-family: inherit; line-height: inherit; overflow: visible; appearance: button; font-weight: inherit; padding: 0px; background-image: none; cursor: pointer; display: flex; gap: 0.5rem; background-color: rgba(255, 0, 0, 0); border: 0px solid #d9d9e3;"><textarea style="box-sizing: border-box; border-style: initial; border-color: white; --tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; --tw-rotate: 0; --tw-skew-x: 0; --tw-skew-y: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-ring-offset-width: 0px; --tw-ring-offset-color: #fff; --tw-ring-color: rgba(59,130,246,0.5); --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-shadow: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-shadow-colored: 0 0 transparent; appearance: none; touch-action: manipulation; margin: 0px; color: rgba(255, 255, 255, 0); font-size: 1rem; font-family: inherit; line-height: 1.5rem; overflow: hidden; resize: none; font-weight: inherit; padding: 0.5rem 0.75rem; background-color: rgba(255, 0, 0, 0); border-radius: 0px; z-index: -9; width: 0px; height: 0px; position: absolute;"></textarea>复制</button>
#!/bin/bash
find /var/log -type f -name "*.log" -mtime +7 -exec rm -f {} \;

清理回收站:清理特定用户的回收站文件。

shell<button class="flex ml-auto gap-2" style="--tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; --tw-rotate: 0; --tw-skew-x: 0; --tw-skew-y: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-ring-offset-width: 0px; --tw-ring-offset-color: #fff; --tw-ring-color: rgba(59,130,246,0.5); --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-shadow: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-shadow-colored: 0 0 transparent; touch-action: manipulation; margin: 0px 0px 0px auto; color: #e8e8e8; font-size: 12px; font-family: inherit; line-height: inherit; overflow: visible; appearance: button; font-weight: inherit; padding: 0px; background-image: none; cursor: pointer; display: flex; gap: 0.5rem; background-color: rgba(255, 0, 0, 0); border: 0px solid #d9d9e3;"><textarea style="box-sizing: border-box; border-style: initial; border-color: white; --tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; --tw-rotate: 0; --tw-skew-x: 0; --tw-skew-y: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-ring-offset-width: 0px; --tw-ring-offset-color: #fff; --tw-ring-color: rgba(59,130,246,0.5); --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-shadow: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-shadow-colored: 0 0 transparent; appearance: none; touch-action: manipulation; margin: 0px; color: rgba(255, 255, 255, 0); font-size: 1rem; font-family: inherit; line-height: 1.5rem; overflow: hidden; resize: none; font-weight: inherit; padding: 0.5rem 0.75rem; background-color: rgba(255, 0, 0, 0); border-radius: 0px; z-index: -9; width: 0px; height: 0px; position: absolute;"></textarea>复制</button>
#!/bin/bash
# 指定要清理的用户
USER="username"

# 清理回收站
rm -rf /home/$USER/.local/share/Trash/*

清理Yum缓存:清理Yum包管理器的缓存文件。

shell<button class="flex ml-auto gap-2" style="--tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; --tw-rotate: 0; --tw-skew-x: 0; --tw-skew-y: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-ring-offset-width: 0px; --tw-ring-offset-color: #fff; --tw-ring-color: rgba(59,130,246,0.5); --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-shadow: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-shadow-colored: 0 0 transparent; touch-action: manipulation; margin: 0px 0px 0px auto; color: #e8e8e8; font-size: 12px; font-family: inherit; line-height: inherit; overflow: visible; appearance: button; font-weight: inherit; padding: 0px; background-image: none; cursor: pointer; display: flex; gap: 0.5rem; background-color: rgba(255, 0, 0, 0); border: 0px solid #d9d9e3;"><textarea style="box-sizing: border-box; border-style: initial; border-color: white; --tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; --tw-rotate: 0; --tw-skew-x: 0; --tw-skew-y: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-ring-offset-width: 0px; --tw-ring-offset-color: #fff; --tw-ring-color: rgba(59,130,246,0.5); --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-shadow: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-shadow-colored: 0 0 transparent; appearance: none; touch-action: manipulation; margin: 0px; color: rgba(255, 255, 255, 0); font-size: 1rem; font-family: inherit; line-height: 1.5rem; overflow: hidden; resize: none; font-weight: inherit; padding: 0.5rem 0.75rem; background-color: rgba(255, 0, 0, 0); border-radius: 0px; z-index: -9; width: 0px; height: 0px; position: absolute;"></textarea>复制</button>
#!/bin/bash
yum clean all

清理旧的备份文件:清理指定目录下的旧备份文件。

shell<button class="flex ml-auto gap-2" style="--tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; --tw-rotate: 0; --tw-skew-x: 0; --tw-skew-y: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-ring-offset-width: 0px; --tw-ring-offset-color: #fff; --tw-ring-color: rgba(59,130,246,0.5); --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-shadow: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-shadow-colored: 0 0 transparent; touch-action: manipulation; margin: 0px 0px 0px auto; color: #e8e8e8; font-size: 12px; font-family: inherit; line-height: inherit; overflow: visible; appearance: button; font-weight: inherit; padding: 0px; background-image: none; cursor: pointer; display: flex; gap: 0.5rem; background-color: rgba(255, 0, 0, 0); border: 0px solid #d9d9e3;"><textarea style="box-sizing: border-box; border-style: initial; border-color: white; --tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; --tw-rotate: 0; --tw-skew-x: 0; --tw-skew-y: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-ring-offset-width: 0px; --tw-ring-offset-color: #fff; --tw-ring-color: rgba(59,130,246,0.5); --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-shadow: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-shadow-colored: 0 0 transparent; appearance: none; touch-action: manipulation; margin: 0px; color: rgba(255, 255, 255, 0); font-size: 1rem; font-family: inherit; line-height: 1.5rem; overflow: hidden; resize: none; font-weight: inherit; padding: 0.5rem 0.75rem; background-color: rgba(255, 0, 0, 0); border-radius: 0px; z-index: -9; width: 0px; height: 0px; position: absolute;"></textarea>复制</button>
#!/bin/bash
# 指定备份文件所在目录
BACKUP_DIR="/path/to/backup"

# 清理90天之前的备份文件
find $BACKUP_DIR -type f -name "*.tar.gz" -mtime +90 -exec rm -f {} \;

请注意,在运行脚本之前,请仔细检查和确认脚本中的目录和文件名是否正确,并确保你有足够的权限来执行清理操作。另外,对于脚本的调度和执行,你可以使用cron任务或其他计划任务工具来自动运行清理脚本。