Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

while reading about linux file searching I got the following...

To search for files using the * wildcard, replace the unknown string with *, e.g. if you remember only that the extension was .out, type ls *.out

and when I tried the following command on system(ubuntu 14.04 LTS) ..I got

anupam@JAZZ:~$ ls  *.bash* 

ls: cannot access  *.bash* : No such file or directory

anupam@JAZZ:~$ ls  .bash*

.bash_history   .bash_logout  .bash_profile  .bashrc

anupam@JAZZ:~$

in the first case why it is showing No such directory ( *.bash* ) and in next case its showing the files ( .bash* )

I am new to this flavor, and according to my automata theory classes (*) means any combination of strings including epsilon(empty),,So why the results are different in both the cases....?

share|improve this question

1 Answer 1

up vote 11 down vote accepted

When you do ls * the * is being expanded before it is passed to ls. That is to say if we have three files (a, b and c) in a directory ls * is actually running ls a b c.

When Bash can't expand, it passes through the raw string¹. That's why you see the wildcards in the error, along with a not found message. ls tried to show the listing for a file literally called *.bash*.

So why didn't that expand? Well by default globbing (what this wildcard expansion is called) won't return hidden files. You can change this with shopt -s dotglob (that won't persist unless you stick it in your .bashrc — it might be disabled by default for a good reason so be careful with it), here's a quick demo:

$ ls  *.bash*
ls: cannot access *.bash*: No such file or directory
$ shopt -s dotglob
$ ls  *.bash*
.bash_aliases  .bash_history  .bash_logout  .bashrc  .bashrc.save

The exception to this is —as you've already shown— when you've already explicitly stated the files will be hidden with a pattern like .bash*. It simply overrides the default dotglob setting:

$ shopt -u dotglob  # unset dotglob
$ ls .bash*
.bash_aliases  .bash_history  .bash_logout  .bashrc  .bashrc.save

Anyway besides that quirk, I hope this helps you understand what's going on under the surface.


There are other shopt flags that alter how globbing works: extglob, failglob, globstar, nocaseglob and nullglob. They and a raft of other shopt flags are documented as part of the Bash manual.

Similarly, the page on Pattern Matching should make for some good reading.

¹ Unless failglob or nullglob are set.

share|improve this answer
    
thanx @Oli ,,I didn't get all of that as I mentioned I am new to this flavor,,but I got 80% of that...It helped me –  jazzz Sep 3 at 10:03

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.