bash 仅列出当前目录中超过 x 天的所有文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34021732/
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
List all files older than x days only in current directory
提问by Umesh Patil
I am new to unix and couldn't get appropriate outcome in other questions.
我是 unix 新手,无法在其他问题中获得适当的结果。
I want to list only files in current directory which are older than x days. I have below restriction
我只想列出当前目录中超过 x 天的文件。我有以下限制
- List only files in current folder which are older than 30 days
- Output shouldn't include directories and subdirectories
- This should list files similar as "ls" command does
- Output should look like
file1 file2 file3 ..
- 仅列出当前文件夹中超过 30 天的文件
- 输出不应包含目录和子目录
- 这应该列出类似于“ls”命令的文件
- 输出应该看起来像
file1 file2 file3 ..
I used find . -mtime +30
. but this gives files and files in sub-directories as well. I would like to restrict doing search recursively and not to search inside directories.
我用过find . -mtime +30
。但这也提供了子目录中的文件和文件。我想限制递归搜索而不是在目录内搜索。
Thanks a lot in advance !
非常感谢!
采纳答案by Andreas Wederbrand
You can use find . -maxdepth 1
to exclude subdirectories.
您可以使用find . -maxdepth 1
排除子目录。
回答by Richasantos
You can do this:
你可以这样做:
find ./ -maxdepth 1 -type f -mtime +30 -print
If having problems, do:
如果遇到问题,请执行以下操作:
find ./ -depth 1 -type f -mtime +30 -print
回答by A1rPun
To add on @Richasantos's answer:
添加@Richasantos的回答:
This works perfectly fine
这工作得很好
$ find . -maxdepth 1 -type f -mtime +30
Prints:
印刷:
./file1
./file2
./file3
You can now pipe this to anything you want. Let's say you want to remove all those old files:
您现在可以将其通过管道传输到您想要的任何内容。假设您要删除所有这些旧文件:
$ find . -maxdepth 1 -type f -mtime +30 -print | xargs /bin/rm -f
From man find
: ``
来自man find
:``
If you are piping the output of find into another program and there is the faintest possibility that the files which you are searching for might contain a newline, then you should seriously consider using the
-print0
option instead of
如果您将 find 的输出传送到另一个程序中,并且您正在搜索的文件可能包含换行符的可能性很小,那么您应该认真考虑使用该
-print0
选项而不是
So using -print0
所以使用 -print0
$ find . -maxdepth 1 -type f -mtime +30 -print0
Prints (with null characters in between):
打印(中间有空字符):
./file1./file2./file3
And is used like this to remove those old files:
并像这样用来删除那些旧文件:
$ find . -maxdepth 1 -type f -mtime +30 -print0 | xargs -0 /bin/rm -f
回答by Lefty G Balogh
A slightly different spin on this: find
is incredibly versatile, you can specify size and time as follows:
一个稍微不同的旋转:find
非常通用,您可以指定大小和时间如下:
This finds you all the logs that are 4 months or older and bigger than 1 meg. If you remove the + sign, it finds files that are roughly that size.
这会为您找到 4 个月或更早且大于 1 兆的所有日志。如果删除 + 号,它会找到大致相同大小的文件。
find /var/log -type f -mtime +120 -size +1M
/var/log/anaconda/journal.log
/var/log/ambari-agent/ambari-alerts.log.23
/var/log/ambari-agent/ambari-alerts.log.22
/var/log/ambari-agent/ambari-alerts.log.24
/var/log/ambari-agent/ambari-alerts.log.25
/var/log/ambari-agent/ambari-alerts.log.21
/var/log/ambari-agent/ambari-alerts.log.20
/var/log/ambari-agent/ambari-alerts.log.19
What's even better, you can feed this into an ls
:
更好的是,您可以将其输入到ls
:
find /var/log -type f -mtime +120 -size +1M -print0 | xargs -0 ls -lh
-rw-r--r--. 1 root root 9.6M Oct 1 13:24 /var/log/ambari-agent/ambari-alerts.log.19
-rw-r--r--. 1 root root 9.6M Sep 27 07:44 /var/log/ambari-agent/ambari-alerts.log.20
-rw-r--r--. 1 root root 9.6M Sep 22 03:32 /var/log/ambari-agent/ambari-alerts.log.21
-rw-r--r--. 1 root root 9.6M Sep 16 23:23 /var/log/ambari-agent/ambari-alerts.log.22
-rw-r--r--. 1 root root 9.6M Sep 11 19:12 /var/log/ambari-agent/ambari-alerts.log.23
-rw-r--r--. 1 root root 9.6M Sep 6 15:02 /var/log/ambari-agent/ambari-alerts.log.24
-rw-r--r--. 1 root root 9.6M Sep 1 10:51 /var/log/ambari-agent/ambari-alerts.log.25
-rw-------. 1 root root 1.8M Mar 11 2019 /var/log/anaconda/journal.log