bash 如何查找大于某个大小的文件,并按上次修改日期对它们进行排序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13891662/
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
How to find files bigger then some size, and sort them by last modification date?
提问by Mateusz Rogulski
I need to write script which write down each files in selected catalog, which are bigger then some size. Also I need to sort them by size, name and last modification date.
我需要编写脚本来写下所选目录中的每个文件,这些文件比某个大小要大。我还需要按大小、名称和最后修改日期对它们进行排序。
So I have made the first two cases:
所以我做了前两种情况:
Sort by size
按尺寸排序
RESULTS=`find $CATALOG -size +$SIZE | sort -n -r | sed 's_.*/__'`
Sort by name
按名称分类
RESULTS=`find $CATALOG -size +$SIZE | sed 's_.*/__' | sort -n `
But I have no idea how to sort results by last modification date.
但我不知道如何按上次修改日期对结果进行排序。
Any help would be appreciated.
任何帮助,将不胜感激。
回答by gniourf_gniourf
One of the best approaches, provided you don't have too many files, is to use lsto do the sorting itself.
如果您没有太多文件,最好的方法之一是使用ls自己进行排序。
Sort by name and print one file per line:
按名称排序并每行打印一个文件:
find $CATALOG -size +$SIZE -exec ls -1 {} +
Sort by size and print one file per line:
按大小排序并每行打印一个文件:
find $CATALOG -size +$SIZE -exec ls -S1 {} +
Sort by modification time and print one file per line:
按修改时间排序,每行打印一个文件:
find $CATALOG -size +$SIZE -exec ls -t1 {} +
You can also play with the lsswitches: Sort by modification time (small first) with long listing format, with human-readable sizes:
您还可以使用ls开关:使用长列表格式按修改时间(小的优先)排序,具有人类可读的大小:
find $CATALOG -size +$SIZE -exec ls -hlrt {} +
Oh, you might want to only findthe files (and ignore the directories):
哦,您可能只想要find文件(并忽略目录):
find $CATALOG -size +$SIZE -type f -exec ls -hlrt {} +
Finally, some remarks: Avoid capitalized variable names in bash (it's considered bad practice) and avoid back ticks, use $(...)instead. E.g.,
最后,一些评论:避免在 bash 中使用大写的变量名(这被认为是不好的做法)并避免反勾号,$(...)而是使用。例如,
results=$(find "$catalog" -size +$size -type f -exec ls -1rt {} +)
Also, you probably don't want to put all the results in a string like the previous line. You probably want to put the results in an array. In that case, use mapfilelike this:
此外,您可能不希望像前一行那样将所有结果放在一个字符串中。您可能希望将结果放入数组中。在这种情况下,请mapfile像这样使用:
mapfile -t results < <(find "$catalog" -size +$size -type f -exec ls -1rt {} +)
回答by digitalronin
Try xargs(do whatever, treating STDIN as a list of arguments) and the -tand -rflags to ls.
i.e. something like this:
尝试xargs(做任何事情,将 STDIN 视为参数列表)并将-t和-r标志标记为ls。即这样的事情:
find $CATALOG -size +$SIZE | xargs ls -ltr
That will give you the files sorted by last modification date.
这将为您提供按上次修改日期排序的文件。
Sorting by multiple attributes at once is going to be really awkward to do with shell utilities and pipes though — I think you'll need to use a scripting language (ruby, perl, php, whatever), unless your shell fu is strong.
一次按多个属性排序对于 shell 实用程序和管道来说真的很尴尬——我认为你需要使用脚本语言(ruby、perl、php 等等),除非你的 shell fu 很强大。

