0

I cd to my home directory and typed

ls *bash*

expecting it will show a list of files contains "bash". However it is not.

While, typing

ls .bash*

works.

According to the documentation, * stands for any character, right? But it seems that it doesn't represent . in this case. What is wrong?

user15964
  • 713

3 Answers3

8

The dotglob shell option controls this:

$ shopt -s dotglob
$ ls *bash*
.bash_history  .bash_logout  .bashrc

It is not enabled by default, probably as a usability/safety measure, since most end users don't have to worry about dotfiles and could very easily delete critical home directory files (such as .config, .ssh) by accident.

l0b0
  • 51,350
1

Most shells will not make "*" character match the initial "." character for historical reasons ( eg files starting with "." are considered hidden , and "." & ".." refer to directories )

Hence ls will not show .bashrc , but ls -a will show all files. Similarly , ls * will expand into all files except those that start with a "." character.

Change this behaviour with shopt -s dotglob which informs bash that "*" must also match the initial "." character. After this , ls * will show .bashrc too.

Prem
  • 3,342
1

The reason is that a leading dot is the convention for "hidden file", and file globbing (which is the name for the use of * as "any number of any characters" and ? as "any one character" and a few others) specifically excludes a leading dot because it means "hidden file".

If you write ls -l .* you will match .bashrc, but you will also match the special hidden files . and .. which are directories, so to show hidden files you will want something along these lines:

  • ls -ld .*
  • ls -l .[^.]*
  • ls -A | grep bash

ls -A *bash* will not show you .bashrc because the file globbing is done before the -A option takes effect.

If you want to change the way file globbing works with hidden files you can execute shopt -s dotglob, either just in the shell to test, or put in your .bash_profile or .bashrc to have it always effective.

Law29
  • 1,156