在 BASH 中,在特定目录中,查找早于 X 天的子文件夹

In BASH, in specific directories, find sub-folders older than a X days

使用 bash 命令我想找到特定目录中早于 X 天的所有子文件夹

这是我的目录结构:

/usr/my_folder

在 my_folder 里面我有不同的子文件夹,比如:

AAAA1
AAAA2
AAAA3
aaaa1
aaaa2
aaaa3
BBBB1
BBBB2    

这些文件夹中的每一个都包含不同的子文件夹,其中包括 "MY_DATA"。例如:

AAAA1/TEST
AAAA2/MY_DATA
BBBB1/TEST
BBBB2/MY_DATA

my_folder开始,我需要搜索AAA*的所有子文件夹aaa* 早于例如 3 天。此外,这些子文件夹必须仅位于 MY_DATA 文件夹中 AAA*aaa*.

我试过这个命令,但似乎不起作用

  find aaa* AAA* -type d -regextype sed -regex \".*/MY_DATA/*\" -mtime +3 -print0

观察以下复制器(仅针对 datetouch 的 GNU 版本进行测试):

# create a directory that should match, and two that should not
mkdir -p AAA1/MY_DATA/TARGET AAA1/MY_DATA/BAD AAA1/ALSO/BAD

# find an adequately conservative date for content that should match
target=$(date -d 'now - 5 days' '+%Y-%m-%d %H:%M')

# update the directory that should match to have that date as its mtime
# ...and also the one that should fail to match due to its name even if mtime is good
touch -d "$target" AAA1/MY_DATA/TARGET AAA1/ALSO/BAD

# Actually run the find command
find AAA* -type d -path '*/MY_DATA/*' -mtime +3 -print

这正确地仅在其输出中发出 AAA1/MY_DATA/TARGET

注意:

    使用
  • -path '*/MY_DATA/*' 而不是 -regex,允许 glob 样式模式。
  • 只使用句法引号(shell 的指令),不使用文字引号(传递给 find 的数据)(文字引号,如 \",将成为文件名匹配的模式)。
  • 转载者使用touch生成一个正确日期的目录,任何人都可以复制粘贴上面的代码进行测试。