简体   繁体   中英

How to search for string in files whose name contains another string and are created in last 7 days in linux?

I'd like to search for string in files whose name contains another string and are created in last 7 days,

I tried:

find . -type f -name '*name_string*' -mtime -7 | grep -ir '*mytext*'

But it didn't work,

Please help

You were really close but just missed the xargs , otherwise the output from find is just a bunch of text for grep.

find . -type f -name '*name_string*' -mtime -7 | xargs grep -i 'mytext'

By using xargs you pass the list of files as the set of files that grep should be searching for the string mytext .

BTW, you can just use mytext instead of *mytext*

If you want to search for multiple patterns say pattern1 and pattern2 in the list of file names containing name_string :

find . -type f -name '*name_string*' -mtime -7 -print0 | while read -d $'\0' f; do
    grep -qi pattern1 "$f" && grep -li pattern2 "$f"
done

This should work even with file names containing spaces.

不需要xargs或其他非标准扩展来获得良好的文件名处理:

find . -type f -name '*name_string*' -mtime -7 -exec grep -i 'mytext' {} \;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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