ls grep query

Hi

please can someone help me with a query?

The following command is executed:

$ ls
abc def hij

You execute the command:

ls | grep f*.

Which files will be displayed and why?

thanks

This sounds like homework.

Please read the The UNIX and Linux Forums - Forum Rules

And why not just run the command, do some testing, and see for yourself....

No, not homework.... but I don't agree with one of the answers in SCSAS Unix associate sample questions. I have contacted Sun but they just reply with some crap. I have run the command on a BSD (since I don't have access to SOLARIS) platform and it does appear that their answer is incorrect...I guess I am just looking for confirmation.

All the files will be displayed because the asterisk is matching zero repetitions of the 'f' character and the '.' is matching any single character

And what does their answer say?

"None of the files are displayed as the shell has matched the pattern f*, so the command has no output to check against"

I changed the question a bit but I don't think it would have made any difference...

If globbing is on, the shell will expand f* to all files starting with f. If there is one such file, it will be displayed. If there's more, then the grep will return nothing. If there's none, grep will match 0 or more f's

If globbing is off, then grep will match 0 or more f's.

$ set +f
$ touch abc def hij
$ ls | grep f*
abc
def
hij

$ touch fgh
$ ls | grep f*
fgh

$ touch fgh2
$ ls | grep f*

$ set -f
$ ls | grep f*
abc
def
fgh
fgh2
hij

I'd say their answer is wrong?

The actual question also has a point/dot at the end!...

It makes no difference here

$ ls | grep f*.
abc
def
hij

So what did you mean by "I changed the question a bit but I don't think it would have made any difference..."?

Here you go

The following command is executed:

$ ls
defer dir1 dir3 file2 fileb flower list script1
defer2 dir2 file1 filea filec fruit output veg
You execute the command:

ls | grep f*.

Which files will be displayed?

A. All of the files. B. Only the files that begin with an f. C. All of the files that contain an f. D. None of the files.

The Answer is showing as

Answer A is incorrect. None of the files will be displayed

Answer B is incorrect. None of the files will be displayed

Answer C is incorrect. None of the files will be displayed

Answer D is correct. None of the files are displayed as the shell has matched the pattern f*, so the command has no output to check against

The answer may or may not be correct.

If the dot is actually part of the question, as you said, then the answer is wrong - regardless of globbing.

If the dot is not part of the question and globbing is on, then the answer is correct.

$ set +f # globbing is on
$ ls | grep f*.
defer
defer2
dir1
dir2
dir3
file1
file2
filea
fileb
filec
flower
fruit
list
output
script1
veg

$ ls | grep f*
(no output)

$ set -f

$ ls | grep f*.
defer
defer2
dir1
dir2
dir3
file1
file2
filea
fileb
filec
flower
fruit
list
output
script1
veg

$ ls | grep f*
defer
defer2
dir1
dir2
dir3
file1
file2
filea
fileb
filec
flower
fruit
list
output
script1
veg

thanks for that....I'm going to have a think about it down the pub!

Where there are no files which match the pattern "f*." shell expands this to a null string. Thus:
ls | grep f*.

Expands to
ls | grep ''

Which lists all files.

A file in the directory with a name starting with "f" and ending in "." changes the behaviour.

I just created a file starting with "f" and ending in "." and it does appear to change the behaviour...but can you tell me why that is?

thanks

I just had a look at some other questions and none of them end in a period....so the dot at the end of the above question is intentional!