如何grep与模式中的文件夹不匹配的新文件夹

时间:2012-03-09 10:53:45

标签: bash

我需要在bash中执行以下操作:

转到该文件夹​​并递归检查当前文件夹中的新文件,新文件的上次更新时间为> = D - 7天,记住每个新文件的名称

我会这样做:find ./ -type f -ctime -7 -exec ls {} \; > new.files

然后我需要转到上一个目录cd ../并查找创建的新文件夹,这些文件夹不是来自我的白名单(文件夹列表),并且在过去7天内进行了更新。如果我找到任何目录,我需要去这个目录并检查新文件,就像我之前做的那样(find ./ -type f -ctime -7 -exec ls {} \; > new.files.from.new.dir

结果我应该用新文件发送新文件列表和新目录列表(不是白名单)。

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

这是完成的,精致的脚本!请注意重定向的很酷用法。 :)

#!/bin/bash

#####################
#
# File change finder
#
####################

#export for use in shells!
export CURR_DIR=`pwd`

RESULTS_LOC=$CURR_DIR
WHITELISTS_LOC=$CURR_DIR
PERIOD=0.1
#Before X number of days... can be fractional
#also, needed for shells, hence source
export FTIME=-$PERIOD

#Make list of new files in current dir.
if [ -e $WHITELISTS_LOC/whitelist.files ]; then

    # What's going on here:
    #
    # 1. Inner shells change to current dir., then search for change files, w
    #    all three diff. types of mods.
    #
    # 2. The output is then sorted, and piped to a uniq -d to give only single 
    #    items for all duplicates.  This is then merged with a duplicate set 
    #    of calls that gives all uniq items via uniq -u.
    #
    # 3. Lastly, we apply any pertinent grep file filters to remove (-v -f)
    #    as in the case of the whitelist, or to select (as in the case of 
    #    our new dir.

    ((sort <(cd $CURR_DIR && find . -type f -ctime $FTIME) \
    <(cd $CURR_DIR && find . -type f -atime $FTIME) \
    <(cd $CURR_DIR && find . -type f -mtime $FTIME) | uniq -d) && \
    (sort <(cd $CURR_DIR && find . -type f -ctime $FTIME) \
    <(cd $CURR_DIR && find . -type f -atime $FTIME) \
    <(cd $CURR_DIR && find . -type f -mtime $FTIME) | uniq -u))| \
    grep -v -f $WHITELISTS_LOC/whitelist.files > \
    $CURR_DIR/new.files
else
    ((sort <(cd $CURR_DIR && find . -type f -ctime $FTIME) \
        <(cd $CURR_DIR && find . -type f -atime $FTIME) \
        <(cd $CURR_DIR && find . -type f -mtime $FTIME) | uniq -d) && \
        (sort <(cd $CURR_DIR && find . -type f -ctime $FTIME) \
        <(cd $CURR_DIR && find . -type f -atime $FTIME) \
        <(cd $CURR_DIR && find . -type f -mtime $FTIME) | uniq -u)) > \
    $CURR_DIR/new.files
fi

#Go down a dir, as requested
cd ../
WORKING_DIR=`pwd`

#Store list of new dirs (can be removed later, if desired.
((sort <(cd $WORKING_DIR && find . -type d -ctime $FTIME) \
    <(cd $WORKING_DIR && find . -type d -atime $FTIME) \
    <(cd $WORKING_DIR && find . -type d -mtime $FTIME) | uniq -d) && \
    (sort <(cd $WORKING_DIR && find . -type d -ctime $FTIME) \
    <(cd $WORKING_DIR && find . -type d -atime $FTIME) \
    <(cd $WORKING_DIR && find . -type d -mtime $FTIME) | uniq -u)) > \
    $RESULTS_LOC/new.dir

#Safely select for conditions based on whether whitelist file and new.dir
#file exist.
#
#This gives the result you want -- new files in new dirs.
if [ -e $WHITELISTS_LOC/whitelist.dir ]; then
    if [ -e $RESULTS_LOC/new.dir ]; then
    echo "1"
    ((sort <(cd $WORKING_DIR && find . -type f -ctime $FTIME) \
        <(cd $WORKING_DIR && find . -type f -atime $FTIME) \
        <(cd $WORKING_DIR && find . -type f -mtime $FTIME) | uniq -d) && \
        (sort <(cd $WORKING_DIR && find . -type f -ctime $FTIME) \
        <(cd $WORKING_DIR && find . -type f -atime $FTIME) \
        <(cd $WORKING_DIR && find . -type f -mtime $FTIME) | uniq -u)) |
    grep -f $RESULTS_LOC/new.dir \
        grep -v -f $WHITELISTS_LOC/whitelist.files > \
        $RESULTS_LOC/new.files.from.new.dir
    else
    echo "2"    
    ((sort <(cd $WORKING_DIR && find . -type f -ctime $FTIME) \
        <(cd $WORKING_DIR && find . -type f -atime $FTIME) \
        <(cd $WORKING_DIR && find . -type f -mtime $FTIME) | uniq -d) && \
        (sort <(cd $WORKING_DIR && find . -type f -ctime $FTIME) \
        <(cd $WORKING_DIR && find . -type f -atime $FTIME) \
        <(cd $WORKING_DIR && find . -type f -mtime $FTIME) | uniq -u)) |\
        grep -v -f $WHITELISTS_LOC/whitelist.files > \
        $RESULTS_LOC/new.files.from.new.dir
    fi
else
    if [ -e $RESULTS_LOC/new.dir ]; then
    echo "3"
    ((sort <(cd $WORKING_DIR && find . -type f -ctime $FTIME) \
        <(cd $WORKING_DIR && find . -type f -atime $FTIME) \
        <(cd $WORKING_DIR && find . -type f -mtime $FTIME) | uniq -d) && \
        (sort <(cd $WORKING_DIR && find . -type f -ctime $FTIME) \
        <(cd $WORKING_DIR && find . -type f -atime $FTIME) \
        <(cd $WORKING_DIR && find . -type f -mtime $FTIME) | uniq -u)) | \
        grep -f $RESULTS_LOC/new.dir > \
        $RESULTS_LOC/new.files.from.new.dir
    else
    echo "4"
    ((sort <(cd $WORKING_DIR && find . -type f -ctime $FTIME) \
        <(cd $WORKING_DIR && find . -type f -atime $FTIME) \
        <(cd $WORKING_DIR && find . -type f -mtime $FTIME) | uniq -d) && \
        (sort <(cd $WORKING_DIR && find . -type f -ctime $FTIME) \
        <(cd $WORKING_DIR && find . -type f -atime $FTIME) \
        <(cd $WORKING_DIR && find . -type f -mtime $FTIME) | uniq -u)) > \
        $RESULTS_LOC/new.files.from.new.dir
    fi
fi

...对不起我之前发布的版本是不工作的,但我用超酷的新版本弥补了它,建立在我原来的基本想法之上。