查找早于 X 天的文件,不包括其他一些文件

发布于 2024-10-08 03:07:24 字数 243 浏览 6 评论 0原文

我正在尝试为linux和solaris编写一个shell脚本,它会找到一些早于X天的特定文件,然后删除它们。诀窍是,在此过程中,有几个文件不能删除。

例如,从以下文件列表中,我需要删除 *.zip 并保留 *.log 和 *.something.*
1.zip
2.zip
3.日志
prefix.something.suffix

查找文件并将它们提供给 rm 很容易,但我在从删除列表中排除文件时遇到困难。

i'm trying to write a shell script, for linux and solaris, that finds some specific files older than X days and then deletes them. the trick is that during this process there are a couple of files that must not be deleted.

for example from the following list of files i need to delete *.zip and keep *.log and *.something.*
1.zip
2.zip
3.log
prefix.something.suffix

finding the files and feeding them to rm was easy, but i'm having difficulties in excluding the files from the deletion list.

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

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

发布评论

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

评论(3

过期以后 2024-10-15 03:07:24

通过实验,我发现可以从与逻辑运算符分组的多个复杂表达式中受益,如下所示:

find -L path -type f \( -name '*.log' \) -a ! \( -name '*.zip' -o -name '*something*' \) -mtime +3

干杯,
G

experimenting around i discovered one can benefit from multiple complex expressions grouped with logical operators like this:

find -L path -type f \( -name '*.log' \) -a ! \( -name '*.zip' -o -name '*something*' \) -mtime +3

cheers,
G

生来就爱笑 2024-10-15 03:07:24

或者你可以这样做:

find /appl/ftp -type f -mtime +30 |grep -vf [exclude_file] | xargs rm -rf;

or you could do this:

find /appl/ftp -type f -mtime +30 |grep -vf [exclude_file] | xargs rm -rf;
淡写薰衣草的香 2024-10-15 03:07:24

我需要找到一种方法来提供排除文件的硬编码列表,以便不删除,但删除超过 30 天的其他所有内容。下面是一个小脚本,用于删除 30 天之前的所有文件,[exclude_file] 中列出的文件除外。

EXCL_FILES=`/bin/cat [exclude_file]`;
RM_FILE=`/usr/bin/find [path] -type f -mtime +30`;

for I in $RM_FILES;
do
     for J in $EXCL_FILES; 
     do
          grep $J $I;
          if [[ $? == 0 ]]; then
               /bin/rm $I;
               if [[ $? != 0 ]]; then echo "PROBLEM: Could not remove $I"; exit 1; fi;
          fi;
     done;
done;

I needed to find a way to provide a hard coded list of exclude files to not remove, but remove everything else that was older than 30 days. Here is a little script to perform a remove of all files older that 30 days, except files that are listed in the [exclude_file].

EXCL_FILES=`/bin/cat [exclude_file]`;
RM_FILE=`/usr/bin/find [path] -type f -mtime +30`;

for I in $RM_FILES;
do
     for J in $EXCL_FILES; 
     do
          grep $J $I;
          if [[ $? == 0 ]]; then
               /bin/rm $I;
               if [[ $? != 0 ]]; then echo "PROBLEM: Could not remove $I"; exit 1; fi;
          fi;
     done;
done;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文