如何搜索在特定日期之间修改的 Linux 个文件的内容
How to search contents of Linux files modified between certain dates
我想搜索一个目录(不包括包含任何特定词的路径,最好是正则表达式模式)并找到内容与我的查询匹配的所有文件(最好是正则表达式模式,我会使其不区分大小写)并在 2 个特定日期之间进行了修改。
基于this answer,我当前的命令是:
find /mnt/c/code -type f -mtime -100 -mtime +5 -print0 |
xargs -0 grep -l -v "firstUnwantedTerm" 'mySearchTerm'
显然这个查询不排除所有包含“firstUnwantedTerm”的路径。
此外,如果 结果可以按修改后的日期时间降序排序,显示: 他们的修改时间、完整文件名和搜索查询(可能在控制台中的不同颜色)被看到它的某些上下文所包围。
grep -rnwl --exclude='*firstUnwantedTerm*' '/mnt/c/code' -e "mySearchTerm"
来自 here 似乎也是朝着正确方向迈出的一步,因为它似乎正确地排除了我的排除项,但它不会按修改后的日期时间进行过滤,也不会'输出所有需要的字段,当然。
这只是快速和脏,没有按日期排序,但有 3 行上下文 before/after 每个匹配项和彩色匹配项:
find ~/mnt/c/code -type f -mtime -100 -mtime +5 | grep -v 'someUnwantedPath' | xargs -I '{}' sh -c "ls -l '{}' && grep --color -C 3 -h 'mySearchTerm' '{}'"
经过一些解释分解成多个部分:
# Find regular files between 100 and 5 days old (modification time)
find ~/mnt/c/code -type f -mtime -100 -mtime +5 |
# Remove unwanted files from list
grep -v 'someUnwantedPath' |
# List each file, then find search term in each file,
# highlighting matches and
# showing 3 lines of context above and below each match
xargs -I '{}' sh -c "ls -l '{}' && grep --color -C 3 -h 'mySearchTerm' '{}'"
我想你可以从这里开始。当然,这可以做得更漂亮并满足您的所有要求,但我只有几分钟的时间,让 UNIX 专家来打败我,让整个事情变得更好 200%。
更新: 版本 2 没有 xargs
,只有一个 grep
命令:
find ~/mnt/c/code -type f -mtime -30 -mtime +25 ! -path '*someUnwantedPath*' -exec stat -c "%y %s %n" {} \; -exec grep --color -C 3 -h 'mySearchTerm' {} \;
! -path '*someUnwantedPath*'
过滤掉不需要的路径,两个 -exec
子命令列出候选文件,然后显示 grep
结果(也可能为空),就像以前一样。请注意,我从使用 ls -l
更改为 stat -c "%y %s %n"
以列出文件日期、大小和名称(只需根据需要修改)。
同样,为了便于阅读,增加了换行符:
find ~/mnt/c/code
-type f
-mtime -30 -mtime +25
! -path '*someUnwantedPath*'
-exec stat -c "%y %s %n" {} \;
-exec grep --color -C 3 -h 'mySearchTerm' {} \;
我想搜索一个目录(不包括包含任何特定词的路径,最好是正则表达式模式)并找到内容与我的查询匹配的所有文件(最好是正则表达式模式,我会使其不区分大小写)并在 2 个特定日期之间进行了修改。
基于this answer,我当前的命令是:
find /mnt/c/code -type f -mtime -100 -mtime +5 -print0 |
xargs -0 grep -l -v "firstUnwantedTerm" 'mySearchTerm'
显然这个查询不排除所有包含“firstUnwantedTerm”的路径。
此外,如果 结果可以按修改后的日期时间降序排序,显示: 他们的修改时间、完整文件名和搜索查询(可能在控制台中的不同颜色)被看到它的某些上下文所包围。
grep -rnwl --exclude='*firstUnwantedTerm*' '/mnt/c/code' -e "mySearchTerm"
来自 here 似乎也是朝着正确方向迈出的一步,因为它似乎正确地排除了我的排除项,但它不会按修改后的日期时间进行过滤,也不会'输出所有需要的字段,当然。
这只是快速和脏,没有按日期排序,但有 3 行上下文 before/after 每个匹配项和彩色匹配项:
find ~/mnt/c/code -type f -mtime -100 -mtime +5 | grep -v 'someUnwantedPath' | xargs -I '{}' sh -c "ls -l '{}' && grep --color -C 3 -h 'mySearchTerm' '{}'"
经过一些解释分解成多个部分:
# Find regular files between 100 and 5 days old (modification time)
find ~/mnt/c/code -type f -mtime -100 -mtime +5 |
# Remove unwanted files from list
grep -v 'someUnwantedPath' |
# List each file, then find search term in each file,
# highlighting matches and
# showing 3 lines of context above and below each match
xargs -I '{}' sh -c "ls -l '{}' && grep --color -C 3 -h 'mySearchTerm' '{}'"
我想你可以从这里开始。当然,这可以做得更漂亮并满足您的所有要求,但我只有几分钟的时间,让 UNIX 专家来打败我,让整个事情变得更好 200%。
更新: 版本 2 没有 xargs
,只有一个 grep
命令:
find ~/mnt/c/code -type f -mtime -30 -mtime +25 ! -path '*someUnwantedPath*' -exec stat -c "%y %s %n" {} \; -exec grep --color -C 3 -h 'mySearchTerm' {} \;
! -path '*someUnwantedPath*'
过滤掉不需要的路径,两个 -exec
子命令列出候选文件,然后显示 grep
结果(也可能为空),就像以前一样。请注意,我从使用 ls -l
更改为 stat -c "%y %s %n"
以列出文件日期、大小和名称(只需根据需要修改)。
同样,为了便于阅读,增加了换行符:
find ~/mnt/c/code
-type f
-mtime -30 -mtime +25
! -path '*someUnwantedPath*'
-exec stat -c "%y %s %n" {} \;
-exec grep --color -C 3 -h 'mySearchTerm' {} \;