从shell globbing中排除特定文件名

时间:2010-04-15 08:57:39

标签: shell

我想从shell(bash)通配字符串fubar.log中删除特定的文件名(例如,*.log)。我尝试的任何东西似乎都没有用,因为globbing不使用标准RE集。

测试用例:目录包含

fubar.log
fubaz.log
barbaz.log
text.txt

只有fubaz.log barbaz.log必须由glob扩展。

2 个答案:

答案 0 :(得分:18)

如果你使用bash

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

或没有extglob

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

for file in *log
do
   case "$file" in
     fubar* ) continue;;
     * ) echo "do your stuff with $file";;
   esac 
done

答案 1 :(得分:2)

你为什么不用grep?例如:

ls | grep -v fubar | while read line;回声“阅读$ line”;完成的;

这是输出:

阅读barbaz.log 阅读fubaz.log 阅读text.txt