Exclude specific filename from shell globbing

后端 未结 2 1134
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-10 13:33

I want to excluse a specific filename (say, fubar.log) from a shell (bash) globbing string, *.log. Nothing of what I tried seems to work, because g

相关标签:
2条回答
  • 2020-12-10 14:12

    if you are using bash

    #!/bin/bash
    shopt -s extglob
    ls !(fubar).log
    

    or without extglob

    shopt -u extglob
    for file in !(fubar).log
    do
      echo "$file"
    done
    

    or

    for file in *log
    do
       case "$file" in
         fubar* ) continue;;
         * ) echo "do your stuff with $file";;
       esac 
    done
    
    0 讨论(0)
  • 2020-12-10 14:19

    Why don't you use grep? For example:

    ls |grep -v fubar|while read line; do echo "reading $line"; done;

    And here is the output:

    reading barbaz.log reading fubaz.log reading text.txt

    0 讨论(0)
提交回复
热议问题