Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matching filename in ls (bash)

Tags:

regex

bash

I have the following files

tcpdump-12
tcpdump-12.delay
tcpdump-24
tcpdump-24.delay

Is there a way to ls only the files

tcpdump-12
tcpdump-24

I can do

ls tcpdump-[[:digit:]][[:digit:]]

but I am looking for something more generic that can take any number of digits, something like tcpdump-[0-9]+ if I was using vim or python regular expressions.

like image 919
Sumit Avatar asked Oct 25 '11 04:10

Sumit


2 Answers

One needs to turn on extended glob functionality of bash to be able to use the advanced pattern matching.

$ ls
tcpdump-12  tcpdump-12.delay  tcpdump-24  tcpdump-24.delay
$ shopt -s extglob
$ ls tcpdump-+([[:digit:]])
tcpdump-12  tcpdump-24
like image 52
Ignacio Vazquez-Abrams Avatar answered Sep 26 '22 15:09

Ignacio Vazquez-Abrams


If you're sure that all the unwanted files end with '.delay' you can do this:

ls --ignore '*.delay'

like image 26
Andrea Carron Avatar answered Sep 26 '22 15:09

Andrea Carron