【问题标题】:Comparing output of "find" command with dictionary words将“查找”命令的输出与字典单词进行比较
【发布时间】:2013-06-18 23:47:13
【问题描述】:

我正在尝试将“查找”文件搜索的输出与字典单词进行比较,以查找特定的隐藏文件。如果隐藏的文件名是字典词,我想被提示删除它,当且仅当它是字典词。

这是我目前所拥有的......

find . \( -iname '*~' -o -iname '.*' \) -type f -exec sudo rm -i '{}' \;

我想我可以使用 /usr/share/dict/words 中的字典单词,但我不确定这是否是最好/最简单的选择。

我对上述代码的性能非常满意。它唯一缺少的是与字典单词的比较。

提前感谢您的帮助!

加分:我不完全确定为什么需要 '*~'。

【问题讨论】:

    标签: shell dictionary find hidden-files


    【解决方案1】:

    我不确定您是否可以与find 进行此类比较。但是,您可以尝试以下方法:

    第 1 步:通过重定向 find 命令的输出来创建文件。

    $ ls -lart
    total 0
    drwxr-xr-x  12 jaypalsingh  staff  408 Jun 19 00:53 ..
    drwxr-xr-x   2 jaypalsingh  staff   68 Jun 19 00:53 .
    
    $ touch .help .yeah .notinmydictionary
    
    $ ls -lart
    total 0
    drwxr-xr-x  12 jaypalsingh  staff  408 Jun 19 00:53 ..
    -rw-r--r--   1 jaypalsingh  staff    0 Jun 19 00:54 .yeah
    -rw-r--r--   1 jaypalsingh  staff    0 Jun 19 00:54 .notinmydictionary
    -rw-r--r--   1 jaypalsingh  staff    0 Jun 19 00:54 .help
    drwxr-xr-x   5 jaypalsingh  staff  170 Jun 19 00:54 .
    
    $ find . \( -iname '*~' -o -iname '.*' \) -type f > myhiddenfile.list
    
    $ cat myhiddenfile.list
    ./.help
    ./.notinmydictionary
    ./.yeah
    

    第 2 步:运行以下awk 命令

    $ awk -F'/' 'NR==FNR{a[substr($NF,2)]=$0;next}{for(x in a){if(x == $1)system("rm \-i "a[x])}}' myhiddenfile.list  /usr/share/dict/words
    remove ./.help? y
    remove ./.yeah? y
    $ ls -lart
    total 8
    drwxr-xr-x  12 jaypalsingh  staff  408 Jun 19 00:53 ..
    -rw-r--r--   1 jaypalsingh  staff    0 Jun 19 00:54 .notinmydictionary
    -rw-r--r--   1 jaypalsingh  staff   37 Jun 19 00:54 myhiddenfile.list
    drwxr-xr-x   4 jaypalsingh  staff  136 Jun 19 01:07 .
    

    awk命令说明:

    # Set the field separator to "/"
    
    awk -F'/' ' 
    
    NR==FNR {   
    
    # Slurp your hidden file list in to an array (index is filename;value is complete path)
    
        a[substr($NF,2)]=$0
    
    # Keep doing this action until all files are stored in array
    
        next 
    }
    {
    
    # Iterate over the array
    
        for(x in a) { 
    
    # If the filename matches a word in your dictionary
    
            if(x == $1) { 
    
    # execute the rm command to delete it
    
                system("rm \-i "a[x]) 
            }
        }
    }' myhiddenfile.list  /usr/share/dict/words
    

    注意:这适用于没有扩展名的文件名。对于扩展名为.help.txt 的文件名,需要额外的步骤来解析文件名,以便对字典进行查找。

    【讨论】:

    • 哇。谢谢!通过一些改动和改进,这正是我想要的。
    • @user2499103 不客气。如果有帮助,请随意投票并标记答案。您随时可以提出新问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-17
    • 1970-01-01
    相关资源
    最近更新 更多