1

trying to grep files from ls with the first two characters as pattern, thus

$ ls Downloads | grep ^ub*

u6xYlgKYQOajUYzjmGQ5.pdf
ubuntu 16.04 base
ubuntu-16-04-base.iso
ubuntu-base-16.04-core-amd64.manifest
ubuntu-base-16.04-core-amd64.tar.gz
ubuntu-base-16.04-core-amd64.tar.gz.zsync
ubuntu-mate-16.04.2-desktop-armhf-raspberry-pi.img.xz
ubuntu-mate-16.04.2-desktop-armhf-raspberry-pi.img.xz.torrent
usbguard-master.zip

So as you can see the first and last two results are the same as I would expect from a pattern saying first character = "u", not "ub".

Using

$ ls Downloads | grep ^u*

just dumps out the whole Downloads contents.

Why does ^ub* return a list of files beginning u..., ^u* seems to make grep promiscuous, but then

ls Downloads | grep ^ubu*

ubuntu 16.04 base
ubuntu-16-04-base.iso
ubuntu-base-16.04-core-amd64.manifest
ubuntu-base-16.04-core-amd64.tar.gz
ubuntu-base-16.04-core-amd64.tar.gz.zsync
ubuntu-mate-16.04.2-desktop-armhf-raspberry-pi.img.xz
ubuntu-mate-16.04.2-desktop-armhf-raspberry-pi.img.xz.torrent

seems to work as I'd expect (and I placed a testubu file in ~/Downloads to check the adherence to ^; its apparently strict, here).

Quotes for the PATTERN make no difference either, which surprised me, but, ok.

And just now by accident, I find that the simplest approach is best

ls Downloads | grep ^ub

returns only files with names starting ub... Of course, removing the ^ returns filenames containing ...ub... only.

I can appreciate that using ^ AND *, e.g. ^ub* is redundant - grep looks at the start and doesn't need to care about what follows in the string.

But why

ls Downloads | grep ub*

bleachbit_1.12_all_ubuntu1604.deb
blue_firefox.svg
DokumentWed.pdf
Firefox_multicolour.svg
u6xYlgKYQOajUYzjmGQ5.pdf
ubuntu 16.04 base
[excerpt]

I can't replicate the colouring used in terminal within the code format on this forum, (and the autoeditor is fighting me to put this back into code form...), but look at the bolded characters

bleachbit_1.12_all_ubuntu1604.deb - blue_firefox.svg - DokumentWed.pdf - Firefox_multicolo***u***r.svg - u6xYlgKYQOajUYzjmGQ5.pdf - ubuntu 16.04 base

Its only searching u OR ub, not b.

So * isn't acting like the wildcard I've come to expect, but I am still pretty wet behind the ears. Also, | grep *ub* returned nothing but the Public directory, even though there are heaps of filenames with ...ub....

What am I misunderstanding about * in grep?

0 Answers0