【问题标题】:Searching through files except regex expression Shell [duplicate]搜索除正则表达式 Shell 之外的文件 [重复]
【发布时间】:2020-03-13 17:31:46
【问题描述】:

我在 shell 编程和正则表达式方面并不是很先进。我需要忽略名称与正则表达式匹配的文件。我已经尝试了很多东西。这是我的功能:

for file in $1/*  <-------- This matches every file, i need it to ignore something stored in variable like: ARG="file.txt"
    do
        if [ -d "$file" ]; then
            dir_count=$((dir_count+1))
            dir_file_counter $file
        fi
            #if file is directory then increment DIRCOUNT and recursively look into that directory
        if [ -f "$file" ]; then
            det_size $file
            file_count=$((file_count+1))
        fi
    done

我尝试过消极的环顾四周,但这并没有帮助: 当我使用如下参数时:arg="(!? text.txt).*" 它只是将其添加到地址中并且不匹配任何内容。 有人可以帮忙吗? 谢谢

【问题讨论】:

  • 如果您需要做的只是在排除模式的同时递归地计算文件数,您可以使用find、grep -v 和wc 的组合。类似find path/to/search -type f | grep -v 'pattern_to_exclude' | wc -l。

标签: regex bash shell


【解决方案1】:

你可以这样做:

find . ! -name '*.csv' ! -name '*.jpg'

它会给你所有的文件,除了那些以你选择的结尾的文件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-12
    • 1970-01-01
    • 1970-01-01
    • 2016-04-21
    相关资源
    最近更新 更多