Bash: loop through files that DO NOT match extension

后端 未结 6 401
眼角桃花
眼角桃花 2021-01-13 17:32

I\'m writing a bash script that needs to loop files inside a directory that do not match a specific extension. So far, I\'ve found that the following code loops all files th

6条回答
  •  甜味超标
    2021-01-13 17:57

    to loop files inside a directory that do not match a specific extension

    You can use extglob:

    shopt -s extglob
    
    for f in *.!(txt); do
        echo "$f"
    done
    

    pattern *.!(txt) will match all entries with a dot and no txt after the dot.


    EDIT: Please see comments below. Here is a find version to loop through files in current directory that don't match a particular extension:

    while IFS= read -d '' -r f; do
        echo "$f"
    done < <(find . -maxdepth 1 -type f -not -name '*.txt' -print0)
    

提交回复
热议问题