wildcard character `* ` can't match some string?

As an extension to Shaido's answer I'd like to give some explanation why the -a option will not work here:

The ls command does not process wildcards. The command line interpreter does this step:

If you type the command line "mycommand *.c" the command line interpreter will first replace the wildcard string "*.c" by the list of matching files and then execute the resulting command line:

mycommand a.c file.c test.c

The command line interpreter works the same way for all commands so it cannot know that -a means "including hidden files" in the case the ls command (while -a has a totally different meaning for other commands)...


Files which begin with a dot are hidden from file listings by default. The easiest way to match those kind of files would be to put a dot in the beginning of the expression. However, this will onyl match files starting with ..

ls  -al  .*.swo

The alternative is to set the dotglob before running the command, then files starting with . will be included.

shopt -s dotglob

Add it to your ~/.bashrc to set it by default.

Note that setting dotglob can lead to uninteded results when using the bash shell, since both . (current directory) and .. (parent directory) will appear when using *. With dotglob set, never try to use rm -R *.

Tags:

Linux

Bash