Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Say I have the following file:

test_file-1234-master.tar.gz

I have tried to un tar using the following commands

 tar -xf test_file-[0-9]+-master.tar.gz
 tar -xf test_file-[:digit]-master.tar.gz

But no luck. How to match this pattern ?

NOTE: There will be always one file. I'm not trying to open multiple files.

share|improve this question
    
shells do globbing, not regex. see unix.stackexchange.com/questions/64067/regex-match-in-cli –  cas yesterday
    
to answer your question, you want: test_file-[0-9]*-master.tar.gz –  cas yesterday
    
@cas This will match one digit followed by anything. –  chaos yesterday
    
yes, so it will. test_file-[0-9][0-9][0-9][0-9]-master.tar.gz then. –  cas yesterday

2 Answers 2

up vote 3 down vote accepted

With ksh, bash -O extglob and zsh -o kshglob only:

test_file-+([[:digit:]])-master.tar.gz

In bash, you have to set the extglob option first. This +(...) matches one or more occurrences of the given patterns. [:digit:] when inside a [...] bracket expression is a POSIX defined character class which includes Indo-Arabic decimal digits ([[:digit:]] is the same as [0123456789] or [0-9]).

It will match:

test_file-1234-master.tar.gz
test_file-1-master.tar.gz
test_file-123456789-master.tar.gz

It will not match:

test_file-1b-master.tar.gz
test_file--master.tar.gz
test_file-a1-master.tar.gz
test_file-abcd-master.tar.gz
test_file-Ⅵ-master.tar.gz # roman numeral
test_file-٨-master.tar.gz  # Eastern Arabic decimal digit

The tar command in your question should then be done like this (with a loop):

shopt -s extglob # bash
# setopt kshglob # zsh
for f in test_file-+([[:digit:]])-master.tar.gz; do
    tar xf "$f"
done

The more idiomatic short syntax in zsh is:

setopt extendedglob
for f (test_file-[0-9]##-master.tar.gz) tar xf $f

(# being the extendedglob equivalent of regexp *, and ## or +).

share|improve this answer

You're trying to use extended regular expression metacharacters and POSIX character classes (+ and [:digit:] respectively) in a globbing pattern;

Assuming bash or similiar, "basic" globbing only supports a handful of wildcards such as:

  • ?: single charater
  • *: zero or more characters
  • []: character class
  • {}: list
  • [!]: negated character class

Differently from metacharacters in extended regular expressions, in "basic" globbing there's no way to modify a wildcard's behavior to match a fixed number of occurences;

So, using "basic" globbing, the shortest and tightest pattern would be:

tar -xf test_file-[0-9][0-9][0-9][0-9]-master.tar.gz
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.