简体   繁体   English

Find命令中的Linux LS -T输出

[英]Linux LS -T output in Find command

I have prepared a script with ls -t to fetch latest file and compare with duplicates i use below 我已经准备了一个带有ls -t的脚本来获取最新文件,并与我在下面使用的重复项进行比较

ls -t *xml |awk 'BEGIN{FS="_"}{if (++dup[$1] >= 2) print}'

However for large size folder ls command not working. 但是对于大尺寸文件夹ls命令不起作用。 so i tried with 所以我尝试了

find ./ -type f \( -iname "*.xml" \) | sort |awk 'BEGIN{FS="_"}{if (++dup[$1] >= 2) print}'

but newly created files is not extracted first so i am unable to retain newly created file. 但是不会先提取新创建的文件,因此我无法保留新创建的文件。

i need to change find command in similiar way output of ls -t command. 我需要以类似的方式更改find​​命令输出ls -t命令。

If your files are guaranteed not to have newlines in their names, try: 如果您的文件保证名称中没有换行符,请尝试:

find . -type f -printf '%T@ %p\n' | sort -rg | sed -E 's/[^ ]* //' | awk -F_ '{if (++dup[$1] >= 2) print}'

For a more robust solution that accepts all filenames, try (GNU tools required): 对于接受所有文件名的更强大的解决方案,请尝试(需要GNU工具):

find . -type f -printf '%T@ %p\0' | sort -rgz | sed -Ez 's/[^ ]* //' | awk -v RS="\0" -F_ '{if (++dup[$1] >= 2) print}'

How it works 这个怎么运作

So that we have an example, let's create three files: 所以我们有一个例子,让我们创建三个文件:

$ touch b_1
$ touch b_2
$ touch b_3

We use find to print out the file's timestamp followed by the file's name: 我们使用find打印文件的时间戳,后跟文件名:

$ find . -type f -printf '%T@ %p\n'
1511234577.7454717760 ./b_3
1511234574.9814419470 ./b_1
1511234576.1054540780 ./b_2

We want the files sorted by timestamp, newest file first, so we use sort -rg to do a numeric reverse sort on the timestamp (expressed as seconds since epoch): 我们希望文件按时间戳排序,最新文件排在第一位,因此我们使用sort -rg对时间戳进行数字反向排序(表示为自纪元以来的秒数):

$ find . -type f -printf '%T@ %p\n' | sort -rg
1511234577.7454717760 ./b_3
1511234576.1054540780 ./b_2
1511234574.9814419470 ./b_1

The next step is to get rid of the timestamps. 下一步是摆脱时间戳。 So, we use sed: 所以,我们使用sed:

$ find . -type f -printf '%T@ %p\n' | sort -rg | sed -E 's/[^ ]* //'
./b_3
./b_2
./b_1

Now, we can use your awk script to identify the older files: 现在,我们可以使用您的awk脚本来识别旧文件:

$ find . -type f -printf '%T@ %p\n' | sort -rg | sed -E 's/[^ ]* //' | awk -F_ '{if (++dup[$1] >= 2) print}'
./b_2
./b_1

Compatibility 兼容性

Very old GNU systems don't support the -E option to sed. 很老的GNU系统不支持sed的-E选项。 On such systems, one may replace -E with -r like: 在这样的系统上,可以用-r替换-E ,如:

sed -r 's/[^ ]* //'

Or, for the more robust version: 或者,对于更强大的版本:

sed -rz 's/[^ ]* //'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM