简体   繁体   English

unix 文件按日期搜索,最近创建到最近创建

[英]unix files search by date , most recent created to least recent created

at folder a at time , 6pm, i create file 6 with text abc在文件夹a时间,下午 6 点,我创建了带有文本 abc 的文件6

at folder a/b at time , 7pm, i create file 7 with text abc在文件夹a/b时间,晚上 7 点,我创建了带有文本 abc 的文件7

at folder a at time , 8pm, i create file 8 with text abc在文件夹a时间,晚上 8 点,我创建了带有文本 abc 的文件8

In folder a, I want to search all files with text abc , order them by most-recently created to least- recently created.在文件夹 a 中,我想搜索所有带有文本abc文件,按最近创建到最近创建的顺序对它们进行排序 I do search by ls -hlt | grep -ir abc *我通过ls -hlt | grep -ir abc *进行搜索ls -hlt | grep -ir abc * my results are ls -hlt | grep -ir abc *我的结果是

6:abc
8:abc
b/7:abc

but i want the following.但我想要以下内容。 How do I acheive it?我如何实现它?

8:abc
b:\7:abc
6:abc

Update : ls -lt | grep -ir abc *更新: ls -lt | grep -ir abc * ls -lt | grep -ir abc * works well to return files with abc, oldest to newest, where as ``ls -lt` shows files new to old. ls -lt | grep -ir abc *可以很好地返回带有 abc 的文件,从旧到新,而 ls -lt 显示文件从新到旧。 Not sure why the grep disturbs flow不知道为什么 grep 会干扰流程

I think that is that you want.我认为这就是你想要的。 It'll show the files with the word 'abc'.它将显示带有“abc”字样的文件。 newest first.新的先来。

ls -Rt1 | grep "*abc*"

Big problems are split into small problems.大问题分解成小问题。

  • Find all files that contain 'abc' ignoring the case.查找所有包含 'abc' 的文件,忽略大小写。
  • Print each filename with it's time of creation since epoch.打印每个文件名及其自纪元以来的创建时间。
  • Sort the list of filanames on the date.按日期对文件名列表进行排序。
  • Remove the date of creation from the list leaving the filenames.从列表中删除创建日期,留下文件名。

So it's:所以是:

grep -irl ABC . |
xargs -d$'\n' -n1 sh -c 'printf "%s\t%s\n" "$(stat -c "%W" "$1")" "$1"' -- |
sort -n -k1 |
cut -f2-

Tested with (notice the stat -c "%Y" vs stat -c "%W" ):测试(注意stat -c "%Y"stat -c "%W" ):

rm -rf a
mkdir -p a/b
echo abc > a/6
touch -d@1 a/6
echo abc > a/b/7
touch -d@2 a/b/7
echo abc > a/8
touch -d@3 a/8

grep -irl abc ./a |
xargs -d$'\n' -n1 sh -c 'printf "%s\t%s\n" "$(stat -c "%Y" "$1")" "$1"' -- |
sort -n -k1 |
cut -f2-

on repl outputs :repl 输出上

./a/6
./a/b/7
./a/8

There is a generic problem with parsing commands like ls or find .解析诸如lsfind类的命令存在一个通用问题。 This is because filenames can become really interesting when they have special characters.这是因为文件名在包含特殊字符时会变得非常有趣。 You can read more about it on this Bash Pitfall and BashFAQ 20你可以在这个Bash 陷阱BashFAQ 20上阅读更多关于它的信息

The most accepted approach is to create a filelist, where the filenames are separated by the NULL-character (\\0) .最被接受的方法是创建一个文件列表,其中文件名由 NULL 字符(\\0)分隔。 GNU grep has the possibility to create such a list using the -Z flag: GNU grep 可以使用-Z标志创建这样的列表:

-Z, --null : Output a zero byte (the ASCII NUL character) instead of the character that normally follows a file name. -Z, --null输出一个零字节(ASCII NUL 字符)而不是通常跟在文件名后面的字符。 For example, grep -lZ outputs a zero byte after each file name instead of the usual newline.例如, grep -lZ在每个文件名后输出一个零字节,而不是通常的换行符。 This option makes the output unambiguous, even in the presence of file names containing unusual characters like newlines.此选项使输出明确无误,即使存在包含换行符等异常字符的文件名也是如此。 This option can be used with commands like find -print0 , perl -0 , sort -z , and xargs -0 to process arbitrary file names, even those that contain newline characters.此选项可与find -print0perl -0sort -zxargs -0等命令一起使用,以处理任意文件名,甚至包含换行符的文件名。

source: man grep来源: man grep

We can now use this in the question of the OP:我们现在可以在 OP 的问题中使用它:

$ grep -lZR "abc" a/

will provide us with a null-separated list of files which can now be stored in an array for further processing (See Capturing output of find . -print0 into a bash array )将为我们提供一个空分隔的文件列表,这些文件现在可以存储在一个数组中以供进一步处理(请参阅捕获 find 的输出。 -print0 到一个 bash 数组中

unset a i
while IFS= read -r -d $'\0' file; do
  a[i++]="$file"        # or however you want to process each file
done < <(grep -lZR "abc" a/)

At this point, we have now an array a which we can use to do whatever we want.在这一点上,我们现在有一个数组a我们可以用它来做我们想做的任何事情。 For example, list all files sorted by modification time:例如,列出按修改时间排序的所有文件:

$ ls -lrt "${a[@]}"

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

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