Use extended globbing to list files but not directories in bash
3
votes
4
answers
5101
views
Let' say I have the following directories tree:
.
├── 11
├── 111
├── 112
├── 1121
├── 113
├── 11a
├── 11a1
├── 1a1
├── 1a2
├── 1aa1.png
├── 2a1
├── a.
├── a1a
├── a1a.jpg
├── a2a
├── aa
├── -aa
├── aa.gif
├── aa.jpg
├── aa.png
├── aa.tiff
├── a.exe
├── a.gif
├── a.html
├── a.jpg
├── a.png
├── a.tiff
├── b2
├── b2a
├── ba.gif
├── ba.jpg
├── ba.png
├── ba.tiff
├── b.html
├── cb1.png
├── d.gif
└── sub1
├── d.gif
└── sub2
So, if I want to match everything in . ($PWD) but files with ".jpg" extension, I do the following:
ls !(*.jpg)
But it outputs:
11 1121 11a1 1aa1.png a1a aa.gif a.exe a.png b2a ba.tiff d.gif
111 113 1a1 2a1 a2a aa.png a.gif a.tiff ba.gif b.html .gif
112 11a 1a2 a. aa aa.tiff a.html b2 ba.png cb1.png
sub1:
. .. d.gif sub2
I'd like to exclude both sub1 and all of its contents (even directories), ie:
> sub1:
> . .. d.gif sub2
I can exclude using:
GLOBIGNORE='sub1'
before of ls, but what happens if I have more directories with differents names?
Is there a way to do that?
Asked by sebelk
(4669 rep)
Jun 26, 2017, 06:04 PM
Last activity: May 31, 2025, 01:07 PM
Last activity: May 31, 2025, 01:07 PM