我正在尝试以下内容:
find /dir1/dir2/dir3 -name '*.txt' -type f
我想做的是在dir3文件夹中递归地搜索文件。这意味着在dir3下有dir4和dir5文件夹,我希望与*txt扩展名匹配的文件应该从dir4和dir5目录返回。另外,我如何才能获得仅在今天创建的文件?
假设您使用bash,我想出了这个:
#!/bin/bash
# This depends on your systems locale I think.
# for me, `date` returns `So 25. Jul 12:32:27 CEST 2021`.
# Therefore I want the $2 for DAY and $3 for MONTH, yours might be different.
DAY="$(date | awk '{print $2}')"
MONTH="$(date | awk '{print $3}')"
FINDIN="/dir1/dir2/dir3"
# This prints only `.txt` files which were created today along with all their details.
# If you only want the path, you could pipe it into
# `awk '{print $11}'`
find "$FINDIN" -type f -ls | grep -E "*.txt$" | grep "$MONTH $DAY"
使用bashisms,您可以在技术上使其成为(冗长的)一行代码,但如果将其放在脚本中,则可以将路径($FINDIN
)替换为作为参数传递的动态值($1
, $2
,…)或调用者目录(从$0
隐含/解析)。