Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exclude specific filename from shell globbing

Tags:

shell

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 globbing doesn't use the standard RE set.

Test case : the directory contains

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

and only fubaz.log barbaz.log must be expanded by the glob.

like image 227
Alsciende Avatar asked Apr 15 '10 08:04

Alsciende


People also ask

How do I exclude a specific file in Linux?

Exclude Files and Directories from a List. When you need to exclude a large number of different files and directories, you can use the rsync --exclude-from flag. To do so, create a text file with the name of the files and directories you want to exclude. Then, pass the name of the file to the --exlude-from option.

How do I exclude in terminal?

Exclude Directories and Files To exclude a directory from the search, use the --exclude-dir option. The path to the excluded directory is relative to the search directory.

What is filename globbing?

In filename globbing, just as in MS-DOS wildcarding, the shell attempts to replace metacharacters appearing in arguments in such a way that arguments specify filenames. Filename globbing makes it easier to specify names of files and sets of files.

What is globbing in Bash?

The Bash shell feature that is used for matching or expanding specific types of patterns is called globbing. Globbing is mainly used to match filenames or searching for content in a file. Globbing uses wildcard characters to create the pattern.


1 Answers

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
like image 150
ghostdog74 Avatar answered Sep 30 '22 16:09

ghostdog74