Combine file modified date and “grep” results through “find”, in one line

后端 未结 1 837
陌清茗
陌清茗 2020-12-22 02:21

We want to show each file\'s modified date and time when applying grep to selected files by the find command. The final result should look like:

相关标签:
1条回答
  • 2020-12-22 02:47

    You can use this find+grep combination to get the formatted result:

    while IFS=$'\06' read -r -d '' t f; do
       sed "s/^/$t /" <(grep -HTni 'σχόλια' "$f")
    done < <(find . -type f -mmin -10 -not \( -path ./admin -prune \) \
             -not \( -path ./people/languages -prune \) \
             -not \( -path ./include -prune \) \
             -printf '%TY-%Tm-%Td %Ta %TH:%TM:%.2TS\06%p\0')
    
    • Note use of \06 as field delimiter to address filenames/paths with whitespaces/newlines etc.
    • \0 (NULL) is used as line terminator for the same reason.
    • %.2TS is used to trip fractional part of the second value.
    • sed is used to insert date/time at line start of grep output.

    PHP Code:

    $cmd = <<<'EOF'
    export TZ=":Europe/Athens"; \
    find . -type f -mmin -10 -not \( -path ./admin -prune \) \
           -not \( -path ./people/languages -prune \) \
           -not \( -path ./include -prune \) \
           -printf '%TY-%Tm-%Td %Ta %TH:%TM:%.2TS\06%p\0' |
    while IFS=$'\06' read -r -d '' t f; do grep -HTni 'σχόλια' "$f" | sed "s/^/$t /"; done
    EOF;
    
    // var_dump( $cmd );
    
    echo shell_exec($cmd) . "\n";
    
    0 讨论(0)
提交回复
热议问题