7
ls  -al  .functions*
-rw-r--r-- 1 root root 471040 Mar  2 10:01 .functions.php.swo
-rw-r--r-- 1 root root 438272 Mar  2 09:57 .functions.php.swp

* can match .php.swo and .php.swp
Why * can't match .functions.php ?

ls  -al  *.swo
ls: cannot access '*.swo': No such file or directory
ls  -al  *swo
ls: cannot access '*swo': No such file or directory
10

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 *.

  • 8
    Warning: dotglob can have unexpected consequences, since * will include . (the current directory) and .. (the parent of the current directory). Try setting dotglob, then doing ls *. Do not try rm -R *. – Gordon Davisson Mar 2 '18 at 8:27
  • @GordonDavisson: Added the warning to the answer so it is not lost/removed. – Shaido - Reinstate Monica Mar 2 '18 at 8:33
  • 4
    please note that other shells like zsh actually implement dotglob in a way that matches neither . nor .. -- a much more useful behavior in my opinion – mnagel Mar 2 '18 at 10:18
  • @Shaido Perhaps bold would be a good idea. This is a very unexpected and dangerous feature, and I know people who always use -f. – wizzwizz4 Mar 2 '18 at 12:36
  • FYI, these patterns are not regular expressions. They're called either globs or wildcards. – Barmar Mar 2 '18 at 18:53
11

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)...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.