Linux 查找早于 X 天的文件,不包括其他一些文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4488910/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Find files older than X days excluding some other files
提问by Morfic
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.
我正在尝试为 linux 和 solaris 编写一个 shell 脚本,它可以找到一些早于 X 天的特定文件,然后将其删除。诀窍是在此过程中,有几个文件不能删除。
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
例如,从以下文件列表中,我需要删除 *.zip 并保留 *.log 和 *.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.
找到文件并将它们提供给 rm 很容易,但我很难从删除列表中排除这些文件。
采纳答案by Morfic
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
干杯,
G
回答by Andrew
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].
我需要找到一种方法来提供不删除的排除文件的硬编码列表,但删除超过 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;
回答by Andrew
or you could do this:
或者你可以这样做:
find /appl/ftp -type f -mtime +30 |grep -vf [exclude_file] | xargs rm -rf;