按包含特定文本的扩展名查找文件并显示文件路径和修改日期
find files by extension containing certain text and display the file path and modified date
我需要找到所有内容中包含 "abc" 的 .txt 文件并显示文件路径和修改日期:
可能是这样的:
> find /folder -iname '*.txt' -print0 | xargs -0 grep -i -n -l "abc" | xargs echo <filepath> <modified_date>
预期输出:
/folder/hello.txt 2017-12-12 32:56:23
/folder/d1/d2/hey.txt 2017-12-12 32:56:23
您似乎有 GNU find
,因此您可以使用 -printf
谓词。这应该会让您走上正轨:
find /folder -iname '*.txt' -type f -exec grep -q 'abc' {} \; -printf '%p %Tx %Tr\n'
-printf
谓词的 %T
修饰符正是您要找的。有趣的 x
和 r
是日期和时间。来自 man find
的更多信息:
@ seconds since Jan. 1, 1970, 00:00 GMT, with frac‐
tional part.
Time fields:
H hour (00..23)
I hour (01..12)
k hour ( 0..23)
l hour ( 1..12)
M minute (00..59)
p locale's AM or PM
r time, 12-hour (hh:mm:ss [AP]M)
S Second (00.00 .. 61.00). There is a fractional
part.
T time, 24-hour (hh:mm:ss.xxxxxxxxxx)
+ Date and time, separated by `+', for example
`2004-04-28+22:22:05.0'. This is a GNU extension.
The time is given in the current timezone (which
may be affected by setting the TZ environment
variable). The seconds field includes a frac‐
tional part.
X locale's time representation (H:M:S). The seconds
field includes a fractional part.
Z time zone (e.g., EDT), or nothing if no time zone
is determinable
Date fields:
a locale's abbreviated weekday name (Sun..Sat)
A locale's full weekday name, variable length (Sun‐
day..Saturday)
b locale's abbreviated month name (Jan..Dec)
B locale's full month name, variable length (Janu‐
ary..December)
c locale's date and time (Sat Nov 04 12:02:33 EST
1989). The format is the same as for ctime(3) and
so to preserve compatibility with that format,
there is no fractional part in the seconds field.
d day of month (01..31)
D date (mm/dd/yy)
h same as b
j day of year (001..366)
m month (01..12)
U week number of year with Sunday as first day of
week (00..53)
w day of week (0..6)
W week number of year with Monday as first day of
week (00..53)
x locale's date representation (mm/dd/yy)
y last two digits of year (00..99)
Y year (1970...)
我需要找到所有内容中包含 "abc" 的 .txt 文件并显示文件路径和修改日期:
可能是这样的:
> find /folder -iname '*.txt' -print0 | xargs -0 grep -i -n -l "abc" | xargs echo <filepath> <modified_date>
预期输出:
/folder/hello.txt 2017-12-12 32:56:23
/folder/d1/d2/hey.txt 2017-12-12 32:56:23
您似乎有 GNU find
,因此您可以使用 -printf
谓词。这应该会让您走上正轨:
find /folder -iname '*.txt' -type f -exec grep -q 'abc' {} \; -printf '%p %Tx %Tr\n'
-printf
谓词的 %T
修饰符正是您要找的。有趣的 x
和 r
是日期和时间。来自 man find
的更多信息:
@ seconds since Jan. 1, 1970, 00:00 GMT, with frac‐
tional part.
Time fields:
H hour (00..23)
I hour (01..12)
k hour ( 0..23)
l hour ( 1..12)
M minute (00..59)
p locale's AM or PM
r time, 12-hour (hh:mm:ss [AP]M)
S Second (00.00 .. 61.00). There is a fractional
part.
T time, 24-hour (hh:mm:ss.xxxxxxxxxx)
+ Date and time, separated by `+', for example
`2004-04-28+22:22:05.0'. This is a GNU extension.
The time is given in the current timezone (which
may be affected by setting the TZ environment
variable). The seconds field includes a frac‐
tional part.
X locale's time representation (H:M:S). The seconds
field includes a fractional part.
Z time zone (e.g., EDT), or nothing if no time zone
is determinable
Date fields:
a locale's abbreviated weekday name (Sun..Sat)
A locale's full weekday name, variable length (Sun‐
day..Saturday)
b locale's abbreviated month name (Jan..Dec)
B locale's full month name, variable length (Janu‐
ary..December)
c locale's date and time (Sat Nov 04 12:02:33 EST
1989). The format is the same as for ctime(3) and
so to preserve compatibility with that format,
there is no fractional part in the seconds field.
d day of month (01..31)
D date (mm/dd/yy)
h same as b
j day of year (001..366)
m month (01..12)
U week number of year with Sunday as first day of
week (00..53)
w day of week (0..6)
W week number of year with Monday as first day of
week (00..53)
x locale's date representation (mm/dd/yy)
y last two digits of year (00..99)
Y year (1970...)